39
39
#include <google/protobuf/stubs/common.h>
40
40
#include <google/protobuf/compiler/java/java_helpers.h>
41
41
#include <google/protobuf/io/printer.h>
42
#include <google/protobuf/wire_format_inl.h>
42
#include <google/protobuf/wire_format.h>
43
43
#include <google/protobuf/stubs/strutil.h>
44
#include <google/protobuf/stubs/substitute.h>
47
46
namespace protobuf {
124
bool AllPrintableAscii(const string& text) {
125
// Cannot use isprint() because it's locale-specific. :(
126
for (int i = 0; i < text.size(); i++) {
127
if ((text[i] < 0x20) || text[i] >= 0x7F) {
134
124
// For encodings with fixed sizes, returns that size in bytes. Otherwise
136
126
int FixedSize(FieldDescriptor::Type type) {
141
131
case FieldDescriptor::TYPE_UINT64 : return -1;
142
132
case FieldDescriptor::TYPE_SINT32 : return -1;
143
133
case FieldDescriptor::TYPE_SINT64 : return -1;
144
case FieldDescriptor::TYPE_FIXED32 : return WireFormat::kFixed32Size;
145
case FieldDescriptor::TYPE_FIXED64 : return WireFormat::kFixed64Size;
146
case FieldDescriptor::TYPE_SFIXED32: return WireFormat::kSFixed32Size;
147
case FieldDescriptor::TYPE_SFIXED64: return WireFormat::kSFixed64Size;
148
case FieldDescriptor::TYPE_FLOAT : return WireFormat::kFloatSize;
149
case FieldDescriptor::TYPE_DOUBLE : return WireFormat::kDoubleSize;
134
case FieldDescriptor::TYPE_FIXED32 : return WireFormatLite::kFixed32Size;
135
case FieldDescriptor::TYPE_FIXED64 : return WireFormatLite::kFixed64Size;
136
case FieldDescriptor::TYPE_SFIXED32: return WireFormatLite::kSFixed32Size;
137
case FieldDescriptor::TYPE_SFIXED64: return WireFormatLite::kSFixed64Size;
138
case FieldDescriptor::TYPE_FLOAT : return WireFormatLite::kFloatSize;
139
case FieldDescriptor::TYPE_DOUBLE : return WireFormatLite::kDoubleSize;
151
case FieldDescriptor::TYPE_BOOL : return WireFormat::kBoolSize;
141
case FieldDescriptor::TYPE_BOOL : return WireFormatLite::kBoolSize;
152
142
case FieldDescriptor::TYPE_ENUM : return -1;
154
144
case FieldDescriptor::TYPE_STRING : return -1;
166
string DefaultValue(const FieldDescriptor* field) {
167
// Switch on cpp_type since we need to know which default_value_* method
168
// of FieldDescriptor to call.
169
switch (field->cpp_type()) {
170
case FieldDescriptor::CPPTYPE_INT32:
171
return SimpleItoa(field->default_value_int32());
172
case FieldDescriptor::CPPTYPE_UINT32:
173
// Need to print as a signed int since Java has no unsigned.
174
return SimpleItoa(static_cast<int32>(field->default_value_uint32()));
175
case FieldDescriptor::CPPTYPE_INT64:
176
return SimpleItoa(field->default_value_int64()) + "L";
177
case FieldDescriptor::CPPTYPE_UINT64:
178
return SimpleItoa(static_cast<int64>(field->default_value_uint64())) +
180
case FieldDescriptor::CPPTYPE_DOUBLE:
181
return SimpleDtoa(field->default_value_double()) + "D";
182
case FieldDescriptor::CPPTYPE_FLOAT:
183
return SimpleFtoa(field->default_value_float()) + "F";
184
case FieldDescriptor::CPPTYPE_BOOL:
185
return field->default_value_bool() ? "true" : "false";
186
case FieldDescriptor::CPPTYPE_STRING: {
187
bool isBytes = field->type() == FieldDescriptor::TYPE_BYTES;
189
if (!isBytes && AllPrintableAscii(field->default_value_string())) {
190
// All chars are ASCII and printable. In this case CEscape() works
191
// fine (it will only escape quotes and backslashes).
192
// Note: If this "optimization" is removed, DescriptorProtos will
193
// no longer be able to initialize itself due to bootstrapping
195
return "\"" + CEscape(field->default_value_string()) + "\"";
198
if (isBytes && !field->has_default_value()) {
199
return "com.google.protobuf.ByteString.EMPTY";
202
// Escaping strings correctly for Java and generating efficient
203
// initializers for ByteStrings are both tricky. We can sidestep the
204
// whole problem by just grabbing the default value from the descriptor.
205
return strings::Substitute(
206
"(($0) $1.getDescriptor().getFields().get($2).getDefaultValue())",
207
isBytes ? "com.google.protobuf.ByteString" : "java.lang.String",
208
ClassName(field->containing_type()), field->index());
211
case FieldDescriptor::CPPTYPE_ENUM:
212
case FieldDescriptor::CPPTYPE_MESSAGE:
213
GOOGLE_LOG(FATAL) << "Can't get here.";
216
// No default because we want the compiler to complain if any new
220
GOOGLE_LOG(FATAL) << "Can't get here.";
224
156
void SetPrimitiveVariables(const FieldDescriptor* descriptor,
225
157
map<string, string>* variables) {
226
158
(*variables)["name"] =
285
217
" return this;\n"
287
219
"public Builder clear$capitalized_name$() {\n"
288
" result.has$capitalized_name$ = false;\n"
289
" result.$name$_ = $default$;\n"
220
" result.has$capitalized_name$ = false;\n");
221
if (descriptor_->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
222
// The default value is not a simple literal so we want to avoid executing
223
// it multiple times. Instead, get the default out of the default instance.
224
printer->Print(variables_,
225
" result.$name$_ = getDefaultInstance().get$capitalized_name$();\n");
227
printer->Print(variables_,
228
" result.$name$_ = $default$;\n");
230
printer->Print(variables_,
290
231
" return this;\n"