OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // http://code.google.com/p/protobuf/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
(...skipping 16 matching lines...) Expand all Loading... |
30 | 30 |
31 // Author: kenton@google.com (Kenton Varda) | 31 // Author: kenton@google.com (Kenton Varda) |
32 // Based on original Protocol Buffers design by | 32 // Based on original Protocol Buffers design by |
33 // Sanjay Ghemawat, Jeff Dean, and others. | 33 // Sanjay Ghemawat, Jeff Dean, and others. |
34 | 34 |
35 #include <google/protobuf/compiler/java/java_message.h> | 35 #include <google/protobuf/compiler/java/java_message.h> |
36 | 36 |
37 #include <algorithm> | 37 #include <algorithm> |
38 #include <google/protobuf/stubs/hash.h> | 38 #include <google/protobuf/stubs/hash.h> |
39 #include <map> | 39 #include <map> |
40 #include <memory> | |
41 #ifndef _SHARED_PTR_H | |
42 #include <google/protobuf/stubs/shared_ptr.h> | |
43 #endif | |
44 #include <vector> | 40 #include <vector> |
45 | 41 |
46 #include <google/protobuf/compiler/java/java_context.h> | |
47 #include <google/protobuf/compiler/java/java_doc_comment.h> | 42 #include <google/protobuf/compiler/java/java_doc_comment.h> |
48 #include <google/protobuf/compiler/java/java_enum.h> | 43 #include <google/protobuf/compiler/java/java_enum.h> |
49 #include <google/protobuf/compiler/java/java_extension.h> | 44 #include <google/protobuf/compiler/java/java_extension.h> |
50 #include <google/protobuf/compiler/java/java_generator_factory.h> | |
51 #include <google/protobuf/compiler/java/java_helpers.h> | 45 #include <google/protobuf/compiler/java/java_helpers.h> |
52 #include <google/protobuf/compiler/java/java_message_builder.h> | |
53 #include <google/protobuf/compiler/java/java_message_builder_lite.h> | |
54 #include <google/protobuf/compiler/java/java_name_resolver.h> | |
55 #include <google/protobuf/io/coded_stream.h> | 46 #include <google/protobuf/io/coded_stream.h> |
56 #include <google/protobuf/io/printer.h> | 47 #include <google/protobuf/io/printer.h> |
57 #include <google/protobuf/descriptor.pb.h> | 48 #include <google/protobuf/descriptor.pb.h> |
58 #include <google/protobuf/wire_format.h> | 49 #include <google/protobuf/wire_format.h> |
59 #include <google/protobuf/stubs/strutil.h> | 50 #include <google/protobuf/stubs/strutil.h> |
60 #include <google/protobuf/stubs/substitute.h> | 51 #include <google/protobuf/stubs/substitute.h> |
61 | 52 |
62 namespace google { | 53 namespace google { |
63 namespace protobuf { | 54 namespace protobuf { |
64 namespace compiler { | 55 namespace compiler { |
65 namespace java { | 56 namespace java { |
66 | 57 |
67 using internal::WireFormat; | 58 using internal::WireFormat; |
68 using internal::WireFormatLite; | 59 using internal::WireFormatLite; |
69 | 60 |
70 namespace { | 61 namespace { |
71 bool GenerateHasBits(const Descriptor* descriptor) { | 62 |
72 return SupportFieldPresence(descriptor->file()) || | 63 void PrintFieldComment(io::Printer* printer, const FieldDescriptor* field) { |
73 HasRepeatedFields(descriptor); | 64 // Print the field's proto-syntax definition as a comment. We don't want to |
74 } | 65 // print group bodies so we cut off after the first line. |
75 | 66 string def = field->DebugString(); |
76 string MapValueImmutableClassdName(const Descriptor* descriptor, | 67 printer->Print("// $def$\n", |
77 ClassNameResolver* name_resolver) { | 68 "def", def.substr(0, def.find_first_of('\n'))); |
78 const FieldDescriptor* value_field = descriptor->FindFieldByName("value"); | 69 } |
79 GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, value_field->type()); | 70 |
80 return name_resolver->GetImmutableClassName(value_field->message_type()); | 71 struct FieldOrderingByNumber { |
81 } | 72 inline bool operator()(const FieldDescriptor* a, |
| 73 const FieldDescriptor* b) const { |
| 74 return a->number() < b->number(); |
| 75 } |
| 76 }; |
| 77 |
| 78 struct ExtensionRangeOrdering { |
| 79 bool operator()(const Descriptor::ExtensionRange* a, |
| 80 const Descriptor::ExtensionRange* b) const { |
| 81 return a->start < b->start; |
| 82 } |
| 83 }; |
| 84 |
| 85 // Sort the fields of the given Descriptor by number into a new[]'d array |
| 86 // and return it. |
| 87 const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor) { |
| 88 const FieldDescriptor** fields = |
| 89 new const FieldDescriptor*[descriptor->field_count()]; |
| 90 for (int i = 0; i < descriptor->field_count(); i++) { |
| 91 fields[i] = descriptor->field(i); |
| 92 } |
| 93 sort(fields, fields + descriptor->field_count(), |
| 94 FieldOrderingByNumber()); |
| 95 return fields; |
| 96 } |
| 97 |
| 98 // Get an identifier that uniquely identifies this type within the file. |
| 99 // This is used to declare static variables related to this type at the |
| 100 // outermost file scope. |
| 101 string UniqueFileScopeIdentifier(const Descriptor* descriptor) { |
| 102 return "static_" + StringReplace(descriptor->full_name(), ".", "_", true); |
| 103 } |
| 104 |
| 105 // Returns true if the message type has any required fields. If it doesn't, |
| 106 // we can optimize out calls to its isInitialized() method. |
| 107 // |
| 108 // already_seen is used to avoid checking the same type multiple times |
| 109 // (and also to protect against recursion). |
| 110 static bool HasRequiredFields( |
| 111 const Descriptor* type, |
| 112 hash_set<const Descriptor*>* already_seen) { |
| 113 if (already_seen->count(type) > 0) { |
| 114 // The type is already in cache. This means that either: |
| 115 // a. The type has no required fields. |
| 116 // b. We are in the midst of checking if the type has required fields, |
| 117 // somewhere up the stack. In this case, we know that if the type |
| 118 // has any required fields, they'll be found when we return to it, |
| 119 // and the whole call to HasRequiredFields() will return true. |
| 120 // Therefore, we don't have to check if this type has required fields |
| 121 // here. |
| 122 return false; |
| 123 } |
| 124 already_seen->insert(type); |
| 125 |
| 126 // If the type has extensions, an extension with message type could contain |
| 127 // required fields, so we have to be conservative and assume such an |
| 128 // extension exists. |
| 129 if (type->extension_range_count() > 0) return true; |
| 130 |
| 131 for (int i = 0; i < type->field_count(); i++) { |
| 132 const FieldDescriptor* field = type->field(i); |
| 133 if (field->is_required()) { |
| 134 return true; |
| 135 } |
| 136 if (GetJavaType(field) == JAVATYPE_MESSAGE) { |
| 137 if (HasRequiredFields(field->message_type(), already_seen)) { |
| 138 return true; |
| 139 } |
| 140 } |
| 141 } |
| 142 |
| 143 return false; |
| 144 } |
| 145 |
| 146 static bool HasRequiredFields(const Descriptor* type) { |
| 147 hash_set<const Descriptor*> already_seen; |
| 148 return HasRequiredFields(type, &already_seen); |
| 149 } |
| 150 |
82 } // namespace | 151 } // namespace |
83 | 152 |
84 // =================================================================== | 153 // =================================================================== |
85 | 154 |
86 MessageGenerator::MessageGenerator(const Descriptor* descriptor) | 155 MessageGenerator::MessageGenerator(const Descriptor* descriptor) |
87 : descriptor_(descriptor) {} | 156 : descriptor_(descriptor), |
| 157 field_generators_(descriptor) { |
| 158 } |
88 | 159 |
89 MessageGenerator::~MessageGenerator() {} | 160 MessageGenerator::~MessageGenerator() {} |
90 | 161 |
91 // =================================================================== | 162 void MessageGenerator::GenerateStaticVariables(io::Printer* printer) { |
92 // TODO(api): Move this class to a separate immutable_message.cc file. | 163 if (HasDescriptorMethods(descriptor_)) { |
93 ImmutableMessageGenerator::ImmutableMessageGenerator( | 164 // Because descriptor.proto (com.google.protobuf.DescriptorProtos) is |
94 const Descriptor* descriptor, Context* context) | 165 // used in the construction of descriptors, we have a tricky bootstrapping |
95 : MessageGenerator(descriptor), context_(context), | 166 // problem. To help control static initialization order, we make sure all |
96 name_resolver_(context->GetNameResolver()), | 167 // descriptors and other static data that depends on them are members of |
97 field_generators_(descriptor, context_) { | 168 // the outermost class in the file. This way, they will be initialized in |
98 GOOGLE_CHECK_NE( | 169 // a deterministic order. |
99 FileOptions::LITE_RUNTIME, descriptor->file()->options().optimize_for()); | 170 |
100 } | 171 map<string, string> vars; |
101 | 172 vars["identifier"] = UniqueFileScopeIdentifier(descriptor_); |
102 ImmutableMessageGenerator::~ImmutableMessageGenerator() {} | 173 vars["index"] = SimpleItoa(descriptor_->index()); |
103 | 174 vars["classname"] = ClassName(descriptor_); |
104 void ImmutableMessageGenerator::GenerateStaticVariables(io::Printer* printer) { | 175 if (descriptor_->containing_type() != NULL) { |
105 // Because descriptor.proto (com.google.protobuf.DescriptorProtos) is | 176 vars["parent"] = UniqueFileScopeIdentifier( |
106 // used in the construction of descriptors, we have a tricky bootstrapping | 177 descriptor_->containing_type()); |
107 // problem. To help control static initialization order, we make sure all | 178 } |
108 // descriptors and other static data that depends on them are members of | 179 if (descriptor_->file()->options().java_multiple_files()) { |
109 // the outermost class in the file. This way, they will be initialized in | 180 // We can only make these package-private since the classes that use them |
110 // a deterministic order. | 181 // are in separate files. |
111 | 182 vars["private"] = ""; |
112 map<string, string> vars; | 183 } else { |
113 vars["identifier"] = UniqueFileScopeIdentifier(descriptor_); | 184 vars["private"] = "private "; |
114 vars["index"] = SimpleItoa(descriptor_->index()); | 185 } |
115 vars["classname"] = name_resolver_->GetImmutableClassName(descriptor_); | 186 |
116 if (descriptor_->containing_type() != NULL) { | 187 // The descriptor for this type. |
117 vars["parent"] = UniqueFileScopeIdentifier( | 188 printer->Print(vars, |
118 descriptor_->containing_type()); | 189 "$private$static com.google.protobuf.Descriptors.Descriptor\n" |
119 } | 190 " internal_$identifier$_descriptor;\n"); |
120 if (MultipleJavaFiles(descriptor_->file(), /* immutable = */ true)) { | 191 |
121 // We can only make these package-private since the classes that use them | 192 // And the FieldAccessorTable. |
122 // are in separate files. | 193 printer->Print(vars, |
123 vars["private"] = ""; | 194 "$private$static\n" |
124 } else { | 195 " com.google.protobuf.GeneratedMessage.FieldAccessorTable\n" |
125 vars["private"] = "private "; | 196 " internal_$identifier$_fieldAccessorTable;\n"); |
126 } | 197 } |
127 | |
128 // The descriptor for this type. | |
129 printer->Print(vars, | |
130 // TODO(teboring): final needs to be added back. The way to fix it is to | |
131 // generate methods that can construct the types, and then still declare the | |
132 // types, and then init them in clinit with the new method calls. | |
133 "$private$static com.google.protobuf.Descriptors.Descriptor\n" | |
134 " internal_$identifier$_descriptor;\n"); | |
135 | |
136 // And the FieldAccessorTable. | |
137 GenerateFieldAccessorTable(printer); | |
138 | 198 |
139 // Generate static members for all nested types. | 199 // Generate static members for all nested types. |
140 for (int i = 0; i < descriptor_->nested_type_count(); i++) { | 200 for (int i = 0; i < descriptor_->nested_type_count(); i++) { |
141 // TODO(kenton): Reuse MessageGenerator objects? | 201 // TODO(kenton): Reuse MessageGenerator objects? |
142 ImmutableMessageGenerator(descriptor_->nested_type(i), context_) | 202 MessageGenerator(descriptor_->nested_type(i)) |
143 .GenerateStaticVariables(printer); | 203 .GenerateStaticVariables(printer); |
144 } | 204 } |
145 } | 205 } |
146 | 206 |
147 int ImmutableMessageGenerator::GenerateStaticVariableInitializers( | 207 void MessageGenerator::GenerateStaticVariableInitializers( |
148 io::Printer* printer) { | 208 io::Printer* printer) { |
149 int bytecode_estimate = 0; | 209 if (HasDescriptorMethods(descriptor_)) { |
150 map<string, string> vars; | 210 map<string, string> vars; |
151 vars["identifier"] = UniqueFileScopeIdentifier(descriptor_); | 211 vars["identifier"] = UniqueFileScopeIdentifier(descriptor_); |
152 vars["index"] = SimpleItoa(descriptor_->index()); | 212 vars["index"] = SimpleItoa(descriptor_->index()); |
153 vars["classname"] = name_resolver_->GetImmutableClassName(descriptor_); | 213 vars["classname"] = ClassName(descriptor_); |
154 if (descriptor_->containing_type() != NULL) { | 214 if (descriptor_->containing_type() != NULL) { |
155 vars["parent"] = UniqueFileScopeIdentifier( | 215 vars["parent"] = UniqueFileScopeIdentifier( |
156 descriptor_->containing_type()); | 216 descriptor_->containing_type()); |
157 } | 217 } |
158 | 218 |
159 // The descriptor for this type. | 219 // The descriptor for this type. |
160 if (descriptor_->containing_type() == NULL) { | 220 if (descriptor_->containing_type() == NULL) { |
| 221 printer->Print(vars, |
| 222 "internal_$identifier$_descriptor =\n" |
| 223 " getDescriptor().getMessageTypes().get($index$);\n"); |
| 224 } else { |
| 225 printer->Print(vars, |
| 226 "internal_$identifier$_descriptor =\n" |
| 227 " internal_$parent$_descriptor.getNestedTypes().get($index$);\n"); |
| 228 } |
| 229 |
| 230 // And the FieldAccessorTable. |
161 printer->Print(vars, | 231 printer->Print(vars, |
162 "internal_$identifier$_descriptor =\n" | 232 "internal_$identifier$_fieldAccessorTable = new\n" |
163 " getDescriptor().getMessageTypes().get($index$);\n"); | 233 " com.google.protobuf.GeneratedMessage.FieldAccessorTable(\n" |
164 bytecode_estimate += 30; | 234 " internal_$identifier$_descriptor,\n" |
165 } else { | 235 " new java.lang.String[] { "); |
166 printer->Print(vars, | 236 for (int i = 0; i < descriptor_->field_count(); i++) { |
167 "internal_$identifier$_descriptor =\n" | 237 printer->Print( |
168 " internal_$parent$_descriptor.getNestedTypes().get($index$);\n"); | 238 "\"$field_name$\", ", |
169 bytecode_estimate += 30; | 239 "field_name", |
170 } | 240 UnderscoresToCapitalizedCamelCase(descriptor_->field(i))); |
171 | 241 } |
172 // And the FieldAccessorTable. | 242 printer->Print( |
173 bytecode_estimate += GenerateFieldAccessorTableInitializer(printer); | 243 "});\n"); |
| 244 } |
174 | 245 |
175 // Generate static member initializers for all nested types. | 246 // Generate static member initializers for all nested types. |
176 for (int i = 0; i < descriptor_->nested_type_count(); i++) { | 247 for (int i = 0; i < descriptor_->nested_type_count(); i++) { |
177 // TODO(kenton): Reuse MessageGenerator objects? | 248 // TODO(kenton): Reuse MessageGenerator objects? |
178 bytecode_estimate += | 249 MessageGenerator(descriptor_->nested_type(i)) |
179 ImmutableMessageGenerator(descriptor_->nested_type(i), context_) | 250 .GenerateStaticVariableInitializers(printer); |
180 .GenerateStaticVariableInitializers(printer); | 251 } |
181 } | 252 } |
182 return bytecode_estimate; | 253 |
183 } | 254 // =================================================================== |
184 | 255 |
185 void ImmutableMessageGenerator:: | 256 void MessageGenerator::GenerateInterface(io::Printer* printer) { |
186 GenerateFieldAccessorTable(io::Printer* printer) { | 257 if (descriptor_->extension_range_count() > 0) { |
187 map<string, string> vars; | 258 if (HasDescriptorMethods(descriptor_)) { |
188 vars["identifier"] = UniqueFileScopeIdentifier(descriptor_); | 259 printer->Print( |
189 if (MultipleJavaFiles(descriptor_->file(), /* immutable = */ true)) { | 260 "public interface $classname$OrBuilder extends\n" |
190 // We can only make these package-private since the classes that use them | 261 " com.google.protobuf.GeneratedMessage.\n" |
191 // are in separate files. | 262 " ExtendableMessageOrBuilder<$classname$> {\n", |
192 vars["private"] = ""; | 263 "classname", descriptor_->name()); |
| 264 } else { |
| 265 printer->Print( |
| 266 "public interface $classname$OrBuilder extends \n" |
| 267 " com.google.protobuf.GeneratedMessageLite.\n" |
| 268 " ExtendableMessageOrBuilder<$classname$> {\n", |
| 269 "classname", descriptor_->name()); |
| 270 } |
193 } else { | 271 } else { |
194 vars["private"] = "private "; | 272 if (HasDescriptorMethods(descriptor_)) { |
195 } | 273 printer->Print( |
196 printer->Print(vars, | 274 "public interface $classname$OrBuilder\n" |
197 "$private$static\n" | 275 " extends com.google.protobuf.MessageOrBuilder {\n", |
198 " com.google.protobuf.GeneratedMessage.FieldAccessorTable\n" | 276 "classname", descriptor_->name()); |
199 " internal_$identifier$_fieldAccessorTable;\n"); | 277 } else { |
200 } | 278 printer->Print( |
201 | 279 "public interface $classname$OrBuilder\n" |
202 int ImmutableMessageGenerator:: | 280 " extends com.google.protobuf.MessageLiteOrBuilder {\n", |
203 GenerateFieldAccessorTableInitializer(io::Printer* printer) { | 281 "classname", descriptor_->name()); |
204 int bytecode_estimate = 10; | 282 } |
205 printer->Print( | |
206 "internal_$identifier$_fieldAccessorTable = new\n" | |
207 " com.google.protobuf.GeneratedMessage.FieldAccessorTable(\n" | |
208 " internal_$identifier$_descriptor,\n" | |
209 " new java.lang.String[] { ", | |
210 "identifier", | |
211 UniqueFileScopeIdentifier(descriptor_)); | |
212 for (int i = 0; i < descriptor_->field_count(); i++) { | |
213 const FieldDescriptor* field = descriptor_->field(i); | |
214 const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); | |
215 bytecode_estimate += 6; | |
216 printer->Print( | |
217 "\"$field_name$\", ", | |
218 "field_name", info->capitalized_name); | |
219 } | |
220 for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { | |
221 const OneofDescriptor* oneof = descriptor_->oneof_decl(i); | |
222 const OneofGeneratorInfo* info = context_->GetOneofGeneratorInfo(oneof); | |
223 bytecode_estimate += 6; | |
224 printer->Print( | |
225 "\"$oneof_name$\", ", | |
226 "oneof_name", info->capitalized_name); | |
227 } | |
228 printer->Print("});\n"); | |
229 return bytecode_estimate; | |
230 } | |
231 | |
232 // =================================================================== | |
233 | |
234 void ImmutableMessageGenerator::GenerateInterface(io::Printer* printer) { | |
235 if (descriptor_->extension_range_count() > 0) { | |
236 printer->Print( | |
237 "public interface $classname$OrBuilder extends\n" | |
238 " $extra_interfaces$\n" | |
239 " com.google.protobuf.GeneratedMessage.\n" | |
240 " ExtendableMessageOrBuilder<$classname$> {\n", | |
241 "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), | |
242 "classname", descriptor_->name()); | |
243 } else { | |
244 printer->Print( | |
245 "public interface $classname$OrBuilder extends\n" | |
246 " $extra_interfaces$\n" | |
247 " com.google.protobuf.MessageOrBuilder {\n", | |
248 "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), | |
249 "classname", descriptor_->name()); | |
250 } | 283 } |
251 | 284 |
252 printer->Indent(); | 285 printer->Indent(); |
253 for (int i = 0; i < descriptor_->field_count(); i++) { | 286 for (int i = 0; i < descriptor_->field_count(); i++) { |
254 printer->Print("\n"); | 287 printer->Print("\n"); |
| 288 PrintFieldComment(printer, descriptor_->field(i)); |
255 field_generators_.get(descriptor_->field(i)) | 289 field_generators_.get(descriptor_->field(i)) |
256 .GenerateInterfaceMembers(printer); | 290 .GenerateInterfaceMembers(printer); |
257 } | 291 } |
258 printer->Outdent(); | 292 printer->Outdent(); |
259 | 293 |
260 printer->Print("}\n"); | 294 printer->Print("}\n"); |
261 } | 295 } |
262 | 296 |
263 // =================================================================== | 297 // =================================================================== |
264 | 298 |
265 void ImmutableMessageGenerator::Generate(io::Printer* printer) { | 299 void MessageGenerator::Generate(io::Printer* printer) { |
266 bool is_own_file = | 300 bool is_own_file = |
267 descriptor_->containing_type() == NULL && | 301 descriptor_->containing_type() == NULL && |
268 MultipleJavaFiles(descriptor_->file(), /* immutable = */ true); | 302 descriptor_->file()->options().java_multiple_files(); |
269 | |
270 map<string, string> variables; | |
271 variables["static"] = is_own_file ? " " : " static "; | |
272 variables["classname"] = descriptor_->name(); | |
273 variables["extra_interfaces"] = ExtraMessageInterfaces(descriptor_); | |
274 | 303 |
275 WriteMessageDocComment(printer, descriptor_); | 304 WriteMessageDocComment(printer, descriptor_); |
276 | 305 |
277 // The builder_type stores the super type name of the nested Builder class. | 306 // The builder_type stores the super type name of the nested Builder class. |
278 string builder_type; | 307 string builder_type; |
279 if (descriptor_->extension_range_count() > 0) { | 308 if (descriptor_->extension_range_count() > 0) { |
280 printer->Print(variables, | 309 if (HasDescriptorMethods(descriptor_)) { |
281 "public $static$final class $classname$ extends\n" | 310 printer->Print( |
282 " com.google.protobuf.GeneratedMessage.ExtendableMessage<\n" | 311 "public $static$ final class $classname$ extends\n" |
283 " $classname$> implements\n" | 312 " com.google.protobuf.GeneratedMessage.ExtendableMessage<\n" |
284 " $extra_interfaces$\n" | 313 " $classname$> implements $classname$OrBuilder {\n", |
285 " $classname$OrBuilder {\n"); | 314 "static", is_own_file ? "" : "static", |
286 builder_type = strings::Substitute( | 315 "classname", descriptor_->name()); |
287 "com.google.protobuf.GeneratedMessage.ExtendableBuilder<$0, ?>", | 316 builder_type = strings::Substitute( |
288 name_resolver_->GetImmutableClassName(descriptor_)); | 317 "com.google.protobuf.GeneratedMessage.ExtendableBuilder<$0, ?>", |
| 318 ClassName(descriptor_)); |
| 319 } else { |
| 320 printer->Print( |
| 321 "public $static$ final class $classname$ extends\n" |
| 322 " com.google.protobuf.GeneratedMessageLite.ExtendableMessage<\n" |
| 323 " $classname$> implements $classname$OrBuilder {\n", |
| 324 "static", is_own_file ? "" : "static", |
| 325 "classname", descriptor_->name()); |
| 326 builder_type = strings::Substitute( |
| 327 "com.google.protobuf.GeneratedMessageLite.ExtendableBuilder<$0, ?>", |
| 328 ClassName(descriptor_)); |
| 329 } |
289 } else { | 330 } else { |
290 printer->Print(variables, | 331 if (HasDescriptorMethods(descriptor_)) { |
291 "public $static$final class $classname$ extends\n" | 332 printer->Print( |
292 " com.google.protobuf.GeneratedMessage implements\n" | 333 "public $static$ final class $classname$ extends\n" |
293 " $extra_interfaces$\n" | 334 " com.google.protobuf.GeneratedMessage\n" |
294 " $classname$OrBuilder {\n"); | 335 " implements $classname$OrBuilder {\n", |
295 | 336 "static", is_own_file ? "" : "static", |
296 builder_type = "com.google.protobuf.GeneratedMessage.Builder"; | 337 "classname", descriptor_->name()); |
| 338 builder_type = "com.google.protobuf.GeneratedMessage.Builder<?>"; |
| 339 } else { |
| 340 printer->Print( |
| 341 "public $static$ final class $classname$ extends\n" |
| 342 " com.google.protobuf.GeneratedMessageLite\n" |
| 343 " implements $classname$OrBuilder {\n", |
| 344 "static", is_own_file ? "" : "static", |
| 345 "classname", descriptor_->name()); |
| 346 builder_type = "com.google.protobuf.GeneratedMessageLite.Builder"; |
| 347 } |
297 } | 348 } |
298 printer->Indent(); | 349 printer->Indent(); |
299 // Using builder_type, instead of Builder, prevents the Builder class from | 350 // Using builder_type, instead of Builder, prevents the Builder class from |
300 // being loaded into PermGen space when the default instance is created. | 351 // being loaded into PermGen space when the default instance is created. |
301 // This optimizes the PermGen space usage for clients that do not modify | 352 // This optimizes the PermGen space usage for clients that do not modify |
302 // messages. | 353 // messages. |
303 printer->Print( | 354 printer->Print( |
304 "// Use $classname$.newBuilder() to construct.\n" | 355 "// Use $classname$.newBuilder() to construct.\n" |
305 "private $classname$($buildertype$ builder) {\n" | 356 "private $classname$($buildertype$ builder) {\n" |
306 " super(builder);\n" | 357 " super(builder);\n" |
| 358 "$set_unknown_fields$\n" |
307 "}\n", | 359 "}\n", |
308 "classname", descriptor_->name(), | 360 "classname", descriptor_->name(), |
309 "buildertype", builder_type); | 361 "buildertype", builder_type, |
| 362 "set_unknown_fields", HasUnknownFields(descriptor_) |
| 363 ? " this.unknownFields = builder.getUnknownFields();" : ""); |
310 printer->Print( | 364 printer->Print( |
311 "private $classname$() {\n", | 365 // Used when constructing the default instance, which cannot be initialized |
312 "classname", descriptor_->name()); | 366 // immediately because it may cyclically refer to other default instances. |
313 printer->Indent(); | 367 "private $classname$(boolean noInit) {$set_default_unknown_fields$}\n" |
314 GenerateInitializers(printer); | 368 "\n" |
315 printer->Outdent(); | 369 "private static final $classname$ defaultInstance;\n" |
316 printer->Print( | 370 "public static $classname$ getDefaultInstance() {\n" |
| 371 " return defaultInstance;\n" |
317 "}\n" | 372 "}\n" |
318 "\n"); | 373 "\n" |
| 374 "public $classname$ getDefaultInstanceForType() {\n" |
| 375 " return defaultInstance;\n" |
| 376 "}\n" |
| 377 "\n", |
| 378 "classname", descriptor_->name(), |
| 379 "set_default_unknown_fields", HasUnknownFields(descriptor_) |
| 380 ? " this.unknownFields =" |
| 381 " com.google.protobuf.UnknownFieldSet.getDefaultInstance(); " : ""); |
319 | 382 |
320 printer->Print( | 383 if (HasUnknownFields(descriptor_)) { |
321 "@java.lang.Override\n" | |
322 "public final com.google.protobuf.UnknownFieldSet\n" | |
323 "getUnknownFields() {\n"); | |
324 if (PreserveUnknownFields(descriptor_)) { | |
325 printer->Print( | 384 printer->Print( |
326 " return this.unknownFields;\n"); | 385 "private final com.google.protobuf.UnknownFieldSet unknownFields;\n" |
327 } else { | 386 "" |
328 printer->Print( | 387 "@java.lang.Override\n" |
329 " return com.google.protobuf.UnknownFieldSet.getDefaultInstance();\n"); | 388 "public final com.google.protobuf.UnknownFieldSet\n" |
| 389 " getUnknownFields() {\n" |
| 390 " return this.unknownFields;\n" |
| 391 "}\n"); |
330 } | 392 } |
331 printer->Print( | |
332 "}\n"); | |
333 | 393 |
334 if (HasGeneratedMethods(descriptor_)) { | 394 if (HasGeneratedMethods(descriptor_)) { |
335 GenerateParsingConstructor(printer); | 395 GenerateParsingConstructor(printer); |
336 } | 396 } |
337 | 397 |
338 GenerateDescriptorMethods(printer); | 398 GenerateDescriptorMethods(printer); |
| 399 GenerateParser(printer); |
339 | 400 |
340 // Nested types | 401 // Nested types |
341 for (int i = 0; i < descriptor_->enum_type_count(); i++) { | 402 for (int i = 0; i < descriptor_->enum_type_count(); i++) { |
342 EnumGenerator(descriptor_->enum_type(i), true, context_) | 403 EnumGenerator(descriptor_->enum_type(i)).Generate(printer); |
343 .Generate(printer); | |
344 } | 404 } |
345 | 405 |
346 for (int i = 0; i < descriptor_->nested_type_count(); i++) { | 406 for (int i = 0; i < descriptor_->nested_type_count(); i++) { |
347 // Don't generate Java classes for map entry messages. | 407 MessageGenerator messageGenerator(descriptor_->nested_type(i)); |
348 if (IsMapEntry(descriptor_->nested_type(i))) continue; | |
349 ImmutableMessageGenerator messageGenerator( | |
350 descriptor_->nested_type(i), context_); | |
351 messageGenerator.GenerateInterface(printer); | 408 messageGenerator.GenerateInterface(printer); |
352 messageGenerator.Generate(printer); | 409 messageGenerator.Generate(printer); |
353 } | 410 } |
354 | 411 |
355 if (GenerateHasBits(descriptor_)) { | 412 // Integers for bit fields. |
356 // Integers for bit fields. | 413 int totalBits = 0; |
357 int totalBits = 0; | 414 for (int i = 0; i < descriptor_->field_count(); i++) { |
358 for (int i = 0; i < descriptor_->field_count(); i++) { | 415 totalBits += field_generators_.get(descriptor_->field(i)) |
359 totalBits += field_generators_.get(descriptor_->field(i)) | 416 .GetNumBitsForMessage(); |
360 .GetNumBitsForMessage(); | |
361 } | |
362 int totalInts = (totalBits + 31) / 32; | |
363 for (int i = 0; i < totalInts; i++) { | |
364 printer->Print("private int $bit_field_name$;\n", | |
365 "bit_field_name", GetBitFieldName(i)); | |
366 } | |
367 } | 417 } |
368 | 418 int totalInts = (totalBits + 31) / 32; |
369 // oneof | 419 for (int i = 0; i < totalInts; i++) { |
370 map<string, string> vars; | 420 printer->Print("private int $bit_field_name$;\n", |
371 for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { | 421 "bit_field_name", GetBitFieldName(i)); |
372 vars["oneof_name"] = context_->GetOneofGeneratorInfo( | |
373 descriptor_->oneof_decl(i))->name; | |
374 vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo( | |
375 descriptor_->oneof_decl(i))->capitalized_name; | |
376 vars["oneof_index"] = SimpleItoa(descriptor_->oneof_decl(i)->index()); | |
377 // oneofCase_ and oneof_ | |
378 printer->Print(vars, | |
379 "private int $oneof_name$Case_ = 0;\n" | |
380 "private java.lang.Object $oneof_name$_;\n"); | |
381 // OneofCase enum | |
382 printer->Print(vars, | |
383 "public enum $oneof_capitalized_name$Case\n" | |
384 " implements com.google.protobuf.Internal.EnumLite {\n"); | |
385 printer->Indent(); | |
386 for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { | |
387 const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); | |
388 printer->Print( | |
389 "$field_name$($field_number$),\n", | |
390 "field_name", | |
391 ToUpper(field->name()), | |
392 "field_number", | |
393 SimpleItoa(field->number())); | |
394 } | |
395 printer->Print( | |
396 "$cap_oneof_name$_NOT_SET(0);\n", | |
397 "cap_oneof_name", | |
398 ToUpper(vars["oneof_name"])); | |
399 printer->Print(vars, | |
400 "private int value = 0;\n" | |
401 "private $oneof_capitalized_name$Case(int value) {\n" | |
402 " this.value = value;\n" | |
403 "}\n"); | |
404 printer->Print(vars, | |
405 "public static $oneof_capitalized_name$Case valueOf(int value) {\n" | |
406 " switch (value) {\n"); | |
407 for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) { | |
408 const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j); | |
409 printer->Print( | |
410 " case $field_number$: return $field_name$;\n", | |
411 "field_number", | |
412 SimpleItoa(field->number()), | |
413 "field_name", | |
414 ToUpper(field->name())); | |
415 } | |
416 printer->Print( | |
417 " case 0: return $cap_oneof_name$_NOT_SET;\n" | |
418 " default: throw new java.lang.IllegalArgumentException(\n" | |
419 " \"Value is undefined for this oneof enum.\");\n" | |
420 " }\n" | |
421 "}\n" | |
422 "public int getNumber() {\n" | |
423 " return this.value;\n" | |
424 "}\n", | |
425 "cap_oneof_name", ToUpper(vars["oneof_name"])); | |
426 printer->Outdent(); | |
427 printer->Print("};\n\n"); | |
428 // oneofCase() | |
429 printer->Print(vars, | |
430 "public $oneof_capitalized_name$Case\n" | |
431 "get$oneof_capitalized_name$Case() {\n" | |
432 " return $oneof_capitalized_name$Case.valueOf(\n" | |
433 " $oneof_name$Case_);\n" | |
434 "}\n" | |
435 "\n"); | |
436 } | 422 } |
437 | 423 |
438 // Fields | 424 // Fields |
439 for (int i = 0; i < descriptor_->field_count(); i++) { | 425 for (int i = 0; i < descriptor_->field_count(); i++) { |
| 426 PrintFieldComment(printer, descriptor_->field(i)); |
440 printer->Print("public static final int $constant_name$ = $number$;\n", | 427 printer->Print("public static final int $constant_name$ = $number$;\n", |
441 "constant_name", FieldConstantName(descriptor_->field(i)), | 428 "constant_name", FieldConstantName(descriptor_->field(i)), |
442 "number", SimpleItoa(descriptor_->field(i)->number())); | 429 "number", SimpleItoa(descriptor_->field(i)->number())); |
443 field_generators_.get(descriptor_->field(i)).GenerateMembers(printer); | 430 field_generators_.get(descriptor_->field(i)).GenerateMembers(printer); |
444 printer->Print("\n"); | 431 printer->Print("\n"); |
445 } | 432 } |
446 | 433 |
| 434 // Called by the constructor, except in the case of the default instance, |
| 435 // in which case this is called by static init code later on. |
| 436 printer->Print("private void initFields() {\n"); |
| 437 printer->Indent(); |
| 438 for (int i = 0; i < descriptor_->field_count(); i++) { |
| 439 field_generators_.get(descriptor_->field(i)) |
| 440 .GenerateInitializationCode(printer); |
| 441 } |
| 442 printer->Outdent(); |
| 443 printer->Print("}\n"); |
| 444 |
447 if (HasGeneratedMethods(descriptor_)) { | 445 if (HasGeneratedMethods(descriptor_)) { |
448 GenerateIsInitialized(printer); | 446 GenerateIsInitialized(printer, MEMOIZE); |
449 GenerateMessageSerializationMethods(printer); | 447 GenerateMessageSerializationMethods(printer); |
450 } | 448 } |
451 | 449 |
452 if (HasEqualsAndHashCode(descriptor_)) { | 450 if (HasEqualsAndHashCode(descriptor_)) { |
453 GenerateEqualsAndHashCode(printer); | 451 GenerateEqualsAndHashCode(printer); |
454 } | 452 } |
455 | 453 |
456 | |
457 GenerateParseFromMethods(printer); | 454 GenerateParseFromMethods(printer); |
458 GenerateBuilder(printer); | 455 GenerateBuilder(printer); |
459 | 456 |
460 printer->Print( | |
461 "\n" | |
462 "// @@protoc_insertion_point(class_scope:$full_name$)\n", | |
463 "full_name", descriptor_->full_name()); | |
464 | |
465 | |
466 // Carefully initialize the default instance in such a way that it doesn't | 457 // Carefully initialize the default instance in such a way that it doesn't |
467 // conflict with other initialization. | 458 // conflict with other initialization. |
468 printer->Print( | 459 printer->Print( |
469 "private static final $classname$ DEFAULT_INSTANCE;\n", | 460 "\n" |
470 "classname", name_resolver_->GetImmutableClassName(descriptor_)); | |
471 printer->Print( | |
472 "static {\n" | 461 "static {\n" |
473 " DEFAULT_INSTANCE = new $classname$();\n" | 462 " defaultInstance = new $classname$(true);\n" |
| 463 " defaultInstance.initFields();\n" |
474 "}\n" | 464 "}\n" |
475 "\n", | 465 "\n" |
476 "classname", name_resolver_->GetImmutableClassName(descriptor_)); | 466 "// @@protoc_insertion_point(class_scope:$full_name$)\n", |
| 467 "classname", descriptor_->name(), |
| 468 "full_name", descriptor_->full_name()); |
477 | 469 |
478 printer->Print( | 470 // Extensions must be declared after the defaultInstance is initialized |
479 "public static $classname$ getDefaultInstance() {\n" | 471 // because the defaultInstance is used by the extension to lazily retrieve |
480 " return DEFAULT_INSTANCE;\n" | |
481 "}\n" | |
482 "\n", | |
483 "classname", name_resolver_->GetImmutableClassName(descriptor_)); | |
484 | |
485 GenerateParser(printer); | |
486 | |
487 printer->Print( | |
488 "public $classname$ getDefaultInstanceForType() {\n" | |
489 " return DEFAULT_INSTANCE;\n" | |
490 "}\n" | |
491 "\n", | |
492 "classname", name_resolver_->GetImmutableClassName(descriptor_)); | |
493 | |
494 // Extensions must be declared after the DEFAULT_INSTANCE is initialized | |
495 // because the DEFAULT_INSTANCE is used by the extension to lazily retrieve | |
496 // the outer class's FileDescriptor. | 472 // the outer class's FileDescriptor. |
497 for (int i = 0; i < descriptor_->extension_count(); i++) { | 473 for (int i = 0; i < descriptor_->extension_count(); i++) { |
498 ImmutableExtensionGenerator(descriptor_->extension(i), context_) | 474 ExtensionGenerator(descriptor_->extension(i)).Generate(printer); |
499 .Generate(printer); | |
500 } | 475 } |
501 | 476 |
502 printer->Outdent(); | 477 printer->Outdent(); |
503 printer->Print("}\n\n"); | 478 printer->Print("}\n\n"); |
504 } | 479 } |
505 | 480 |
506 | 481 |
507 // =================================================================== | 482 // =================================================================== |
508 | 483 |
509 void ImmutableMessageGenerator:: | 484 void MessageGenerator:: |
510 GenerateMessageSerializationMethods(io::Printer* printer) { | 485 GenerateMessageSerializationMethods(io::Printer* printer) { |
511 google::protobuf::scoped_array<const FieldDescriptor * > sorted_fields( | 486 scoped_array<const FieldDescriptor*> sorted_fields( |
512 SortFieldsByNumber(descriptor_)); | 487 SortFieldsByNumber(descriptor_)); |
513 | 488 |
514 vector<const Descriptor::ExtensionRange*> sorted_extensions; | 489 vector<const Descriptor::ExtensionRange*> sorted_extensions; |
515 for (int i = 0; i < descriptor_->extension_range_count(); ++i) { | 490 for (int i = 0; i < descriptor_->extension_range_count(); ++i) { |
516 sorted_extensions.push_back(descriptor_->extension_range(i)); | 491 sorted_extensions.push_back(descriptor_->extension_range(i)); |
517 } | 492 } |
518 std::sort(sorted_extensions.begin(), sorted_extensions.end(), | 493 sort(sorted_extensions.begin(), sorted_extensions.end(), |
519 ExtensionRangeOrdering()); | 494 ExtensionRangeOrdering()); |
520 | 495 |
521 printer->Print( | 496 printer->Print( |
522 "public void writeTo(com.google.protobuf.CodedOutputStream output)\n" | 497 "public void writeTo(com.google.protobuf.CodedOutputStream output)\n" |
523 " throws java.io.IOException {\n"); | 498 " throws java.io.IOException {\n"); |
524 printer->Indent(); | 499 printer->Indent(); |
525 if (HasPackedFields(descriptor_)) { | 500 // writeTo(CodedOutputStream output) might be invoked without |
526 // writeTo(CodedOutputStream output) might be invoked without | 501 // getSerializedSize() ever being called, but we need the memoized |
527 // getSerializedSize() ever being called, but we need the memoized | 502 // sizes in case this message has packed fields. Rather than emit checks for |
528 // sizes in case this message has packed fields. Rather than emit checks for | 503 // each packed field, just call getSerializedSize() up front for all messages. |
529 // each packed field, just call getSerializedSize() up front. | 504 // In most cases, getSerializedSize() will have already been called anyway by |
530 // In most cases, getSerializedSize() will have already been called anyway | 505 // one of the wrapper writeTo() methods, making this call cheap. |
531 // by one of the wrapper writeTo() methods, making this call cheap. | 506 printer->Print( |
532 printer->Print( | 507 "getSerializedSize();\n"); |
533 "getSerializedSize();\n"); | |
534 } | |
535 | 508 |
536 if (descriptor_->extension_range_count() > 0) { | 509 if (descriptor_->extension_range_count() > 0) { |
537 if (descriptor_->options().message_set_wire_format()) { | 510 if (descriptor_->options().message_set_wire_format()) { |
538 printer->Print( | 511 printer->Print( |
539 "com.google.protobuf.GeneratedMessage\n" | 512 "com.google.protobuf.GeneratedMessage$lite$\n" |
540 " .ExtendableMessage<$classname$>.ExtensionWriter\n" | 513 " .ExtendableMessage<$classname$>.ExtensionWriter extensionWriter =\n" |
541 " extensionWriter = newMessageSetExtensionWriter();\n", | 514 " newMessageSetExtensionWriter();\n", |
542 "classname", name_resolver_->GetImmutableClassName(descriptor_)); | 515 "lite", HasDescriptorMethods(descriptor_) ? "" : "Lite", |
| 516 "classname", ClassName(descriptor_)); |
543 } else { | 517 } else { |
544 printer->Print( | 518 printer->Print( |
545 "com.google.protobuf.GeneratedMessage\n" | 519 "com.google.protobuf.GeneratedMessage$lite$\n" |
546 " .ExtendableMessage<$classname$>.ExtensionWriter\n" | 520 " .ExtendableMessage<$classname$>.ExtensionWriter extensionWriter =\n" |
547 " extensionWriter = newExtensionWriter();\n", | 521 " newExtensionWriter();\n", |
548 "classname", name_resolver_->GetImmutableClassName(descriptor_)); | 522 "lite", HasDescriptorMethods(descriptor_) ? "" : "Lite", |
| 523 "classname", ClassName(descriptor_)); |
549 } | 524 } |
550 } | 525 } |
551 | 526 |
552 // Merge the fields and the extension ranges, both sorted by field number. | 527 // Merge the fields and the extension ranges, both sorted by field number. |
553 for (int i = 0, j = 0; | 528 for (int i = 0, j = 0; |
554 i < descriptor_->field_count() || j < sorted_extensions.size(); | 529 i < descriptor_->field_count() || j < sorted_extensions.size(); |
555 ) { | 530 ) { |
556 if (i == descriptor_->field_count()) { | 531 if (i == descriptor_->field_count()) { |
557 GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]); | 532 GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]); |
558 } else if (j == sorted_extensions.size()) { | 533 } else if (j == sorted_extensions.size()) { |
559 GenerateSerializeOneField(printer, sorted_fields[i++]); | 534 GenerateSerializeOneField(printer, sorted_fields[i++]); |
560 } else if (sorted_fields[i]->number() < sorted_extensions[j]->start) { | 535 } else if (sorted_fields[i]->number() < sorted_extensions[j]->start) { |
561 GenerateSerializeOneField(printer, sorted_fields[i++]); | 536 GenerateSerializeOneField(printer, sorted_fields[i++]); |
562 } else { | 537 } else { |
563 GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]); | 538 GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]); |
564 } | 539 } |
565 } | 540 } |
566 | 541 |
567 if (PreserveUnknownFields(descriptor_)) { | 542 if (HasUnknownFields(descriptor_)) { |
568 if (descriptor_->options().message_set_wire_format()) { | 543 if (descriptor_->options().message_set_wire_format()) { |
569 printer->Print( | 544 printer->Print( |
570 "unknownFields.writeAsMessageSetTo(output);\n"); | 545 "getUnknownFields().writeAsMessageSetTo(output);\n"); |
571 } else { | 546 } else { |
572 printer->Print( | 547 printer->Print( |
573 "unknownFields.writeTo(output);\n"); | 548 "getUnknownFields().writeTo(output);\n"); |
574 } | 549 } |
575 } | 550 } |
576 | 551 |
577 printer->Outdent(); | 552 printer->Outdent(); |
578 printer->Print( | 553 printer->Print( |
579 "}\n" | 554 "}\n" |
580 "\n" | 555 "\n" |
581 "private int memoizedSerializedSize = -1;\n" | 556 "private int memoizedSerializedSize = -1;\n" |
582 "public int getSerializedSize() {\n" | 557 "public int getSerializedSize() {\n" |
583 " int size = memoizedSerializedSize;\n" | 558 " int size = memoizedSerializedSize;\n" |
584 " if (size != -1) return size;\n" | 559 " if (size != -1) return size;\n" |
585 "\n" | 560 "\n" |
586 " size = 0;\n"); | 561 " size = 0;\n"); |
587 printer->Indent(); | 562 printer->Indent(); |
588 | 563 |
589 for (int i = 0; i < descriptor_->field_count(); i++) { | 564 for (int i = 0; i < descriptor_->field_count(); i++) { |
590 field_generators_.get(sorted_fields[i]).GenerateSerializedSizeCode(printer); | 565 field_generators_.get(sorted_fields[i]).GenerateSerializedSizeCode(printer); |
591 } | 566 } |
592 | 567 |
593 if (descriptor_->extension_range_count() > 0) { | 568 if (descriptor_->extension_range_count() > 0) { |
594 if (descriptor_->options().message_set_wire_format()) { | 569 if (descriptor_->options().message_set_wire_format()) { |
595 printer->Print( | 570 printer->Print( |
596 "size += extensionsSerializedSizeAsMessageSet();\n"); | 571 "size += extensionsSerializedSizeAsMessageSet();\n"); |
597 } else { | 572 } else { |
598 printer->Print( | 573 printer->Print( |
599 "size += extensionsSerializedSize();\n"); | 574 "size += extensionsSerializedSize();\n"); |
600 } | 575 } |
601 } | 576 } |
602 | 577 |
603 if (PreserveUnknownFields(descriptor_)) { | 578 if (HasUnknownFields(descriptor_)) { |
604 if (descriptor_->options().message_set_wire_format()) { | 579 if (descriptor_->options().message_set_wire_format()) { |
605 printer->Print( | 580 printer->Print( |
606 "size += unknownFields.getSerializedSizeAsMessageSet();\n"); | 581 "size += getUnknownFields().getSerializedSizeAsMessageSet();\n"); |
607 } else { | 582 } else { |
608 printer->Print( | 583 printer->Print( |
609 "size += unknownFields.getSerializedSize();\n"); | 584 "size += getUnknownFields().getSerializedSize();\n"); |
610 } | 585 } |
611 } | 586 } |
612 | 587 |
613 printer->Outdent(); | 588 printer->Outdent(); |
614 printer->Print( | 589 printer->Print( |
615 " memoizedSerializedSize = size;\n" | 590 " memoizedSerializedSize = size;\n" |
616 " return size;\n" | 591 " return size;\n" |
617 "}\n" | 592 "}\n" |
618 "\n"); | 593 "\n"); |
619 | 594 |
620 printer->Print( | 595 printer->Print( |
621 "private static final long serialVersionUID = 0L;\n"); | 596 "private static final long serialVersionUID = 0L;\n" |
| 597 "@java.lang.Override\n" |
| 598 "protected java.lang.Object writeReplace()\n" |
| 599 " throws java.io.ObjectStreamException {\n" |
| 600 " return super.writeReplace();\n" |
| 601 "}\n" |
| 602 "\n"); |
622 } | 603 } |
623 | 604 |
624 void ImmutableMessageGenerator:: | 605 void MessageGenerator:: |
625 GenerateParseFromMethods(io::Printer* printer) { | 606 GenerateParseFromMethods(io::Printer* printer) { |
626 // Note: These are separate from GenerateMessageSerializationMethods() | 607 // Note: These are separate from GenerateMessageSerializationMethods() |
627 // because they need to be generated even for messages that are optimized | 608 // because they need to be generated even for messages that are optimized |
628 // for code size. | 609 // for code size. |
629 printer->Print( | 610 printer->Print( |
630 "public static $classname$ parseFrom(\n" | 611 "public static $classname$ parseFrom(\n" |
631 " com.google.protobuf.ByteString data)\n" | 612 " com.google.protobuf.ByteString data)\n" |
632 " throws com.google.protobuf.InvalidProtocolBufferException {\n" | 613 " throws com.google.protobuf.InvalidProtocolBufferException {\n" |
633 " return PARSER.parseFrom(data);\n" | 614 " return PARSER.parseFrom(data);\n" |
634 "}\n" | 615 "}\n" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 " throws java.io.IOException {\n" | 654 " throws java.io.IOException {\n" |
674 " return PARSER.parseFrom(input);\n" | 655 " return PARSER.parseFrom(input);\n" |
675 "}\n" | 656 "}\n" |
676 "public static $classname$ parseFrom(\n" | 657 "public static $classname$ parseFrom(\n" |
677 " com.google.protobuf.CodedInputStream input,\n" | 658 " com.google.protobuf.CodedInputStream input,\n" |
678 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" | 659 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" |
679 " throws java.io.IOException {\n" | 660 " throws java.io.IOException {\n" |
680 " return PARSER.parseFrom(input, extensionRegistry);\n" | 661 " return PARSER.parseFrom(input, extensionRegistry);\n" |
681 "}\n" | 662 "}\n" |
682 "\n", | 663 "\n", |
683 "classname", name_resolver_->GetImmutableClassName(descriptor_)); | 664 "classname", ClassName(descriptor_)); |
684 } | 665 } |
685 | 666 |
686 void ImmutableMessageGenerator::GenerateSerializeOneField( | 667 void MessageGenerator::GenerateSerializeOneField( |
687 io::Printer* printer, const FieldDescriptor* field) { | 668 io::Printer* printer, const FieldDescriptor* field) { |
688 field_generators_.get(field).GenerateSerializationCode(printer); | 669 field_generators_.get(field).GenerateSerializationCode(printer); |
689 } | 670 } |
690 | 671 |
691 void ImmutableMessageGenerator::GenerateSerializeOneExtensionRange( | 672 void MessageGenerator::GenerateSerializeOneExtensionRange( |
692 io::Printer* printer, const Descriptor::ExtensionRange* range) { | 673 io::Printer* printer, const Descriptor::ExtensionRange* range) { |
693 printer->Print( | 674 printer->Print( |
694 "extensionWriter.writeUntil($end$, output);\n", | 675 "extensionWriter.writeUntil($end$, output);\n", |
695 "end", SimpleItoa(range->end)); | 676 "end", SimpleItoa(range->end)); |
696 } | 677 } |
697 | 678 |
698 // =================================================================== | 679 // =================================================================== |
699 | 680 |
700 void ImmutableMessageGenerator::GenerateBuilder(io::Printer* printer) { | 681 void MessageGenerator::GenerateBuilder(io::Printer* printer) { |
701 // LITE_RUNTIME implements this at the GeneratedMessageLite level. | 682 printer->Print( |
702 printer->Print( | 683 "public static Builder newBuilder() { return Builder.create(); }\n" |
703 "public Builder newBuilderForType() { return newBuilder(); }\n"); | 684 "public Builder newBuilderForType() { return newBuilder(); }\n" |
704 | |
705 printer->Print( | |
706 "public static Builder newBuilder() {\n" | |
707 " return DEFAULT_INSTANCE.toBuilder();\n" | |
708 "}\n" | |
709 "public static Builder newBuilder($classname$ prototype) {\n" | 685 "public static Builder newBuilder($classname$ prototype) {\n" |
710 " return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);\n" | 686 " return newBuilder().mergeFrom(prototype);\n" |
711 "}\n" | 687 "}\n" |
712 "public Builder toBuilder() {\n" | 688 "public Builder toBuilder() { return newBuilder(this); }\n" |
713 " return this == DEFAULT_INSTANCE\n" | |
714 " ? new Builder() : new Builder().mergeFrom(this);\n" | |
715 "}\n" | |
716 "\n", | 689 "\n", |
717 "classname", name_resolver_->GetImmutableClassName(descriptor_)); | 690 "classname", ClassName(descriptor_)); |
718 | 691 |
719 printer->Print( | 692 if (HasNestedBuilders(descriptor_)) { |
720 "@java.lang.Override\n" | 693 printer->Print( |
721 "protected Builder newBuilderForType(\n" | 694 "@java.lang.Override\n" |
722 " com.google.protobuf.GeneratedMessage.BuilderParent parent) {\n" | 695 "protected Builder newBuilderForType(\n" |
723 " Builder builder = new Builder(parent);\n" | 696 " com.google.protobuf.GeneratedMessage.BuilderParent parent) {\n" |
724 " return builder;\n" | 697 " Builder builder = new Builder(parent);\n" |
725 "}\n"); | 698 " return builder;\n" |
726 | 699 "}\n"); |
727 MessageBuilderGenerator builderGenerator(descriptor_, context_); | 700 } |
728 builderGenerator.Generate(printer); | 701 |
| 702 WriteMessageDocComment(printer, descriptor_); |
| 703 |
| 704 if (descriptor_->extension_range_count() > 0) { |
| 705 if (HasDescriptorMethods(descriptor_)) { |
| 706 printer->Print( |
| 707 "public static final class Builder extends\n" |
| 708 " com.google.protobuf.GeneratedMessage.ExtendableBuilder<\n" |
| 709 " $classname$, Builder> implements $classname$OrBuilder {\n", |
| 710 "classname", ClassName(descriptor_)); |
| 711 } else { |
| 712 printer->Print( |
| 713 "public static final class Builder extends\n" |
| 714 " com.google.protobuf.GeneratedMessageLite.ExtendableBuilder<\n" |
| 715 " $classname$, Builder> implements $classname$OrBuilder {\n", |
| 716 "classname", ClassName(descriptor_)); |
| 717 } |
| 718 } else { |
| 719 if (HasDescriptorMethods(descriptor_)) { |
| 720 printer->Print( |
| 721 "public static final class Builder extends\n" |
| 722 " com.google.protobuf.GeneratedMessage.Builder<Builder>\n" |
| 723 " implements $classname$OrBuilder {\n", |
| 724 "classname", ClassName(descriptor_)); |
| 725 } else { |
| 726 printer->Print( |
| 727 "public static final class Builder extends\n" |
| 728 " com.google.protobuf.GeneratedMessageLite.Builder<\n" |
| 729 " $classname$, Builder>\n" |
| 730 " implements $classname$OrBuilder {\n", |
| 731 "classname", ClassName(descriptor_)); |
| 732 } |
| 733 } |
| 734 printer->Indent(); |
| 735 |
| 736 GenerateDescriptorMethods(printer); |
| 737 GenerateCommonBuilderMethods(printer); |
| 738 |
| 739 if (HasGeneratedMethods(descriptor_)) { |
| 740 GenerateIsInitialized(printer, DONT_MEMOIZE); |
| 741 GenerateBuilderParsingMethods(printer); |
| 742 } |
| 743 |
| 744 // Integers for bit fields. |
| 745 int totalBits = 0; |
| 746 for (int i = 0; i < descriptor_->field_count(); i++) { |
| 747 totalBits += field_generators_.get(descriptor_->field(i)) |
| 748 .GetNumBitsForBuilder(); |
| 749 } |
| 750 int totalInts = (totalBits + 31) / 32; |
| 751 for (int i = 0; i < totalInts; i++) { |
| 752 printer->Print("private int $bit_field_name$;\n", |
| 753 "bit_field_name", GetBitFieldName(i)); |
| 754 } |
| 755 |
| 756 for (int i = 0; i < descriptor_->field_count(); i++) { |
| 757 printer->Print("\n"); |
| 758 PrintFieldComment(printer, descriptor_->field(i)); |
| 759 field_generators_.get(descriptor_->field(i)) |
| 760 .GenerateBuilderMembers(printer); |
| 761 } |
| 762 |
| 763 printer->Print( |
| 764 "\n" |
| 765 "// @@protoc_insertion_point(builder_scope:$full_name$)\n", |
| 766 "full_name", descriptor_->full_name()); |
| 767 |
| 768 printer->Outdent(); |
| 769 printer->Print("}\n"); |
729 } | 770 } |
730 | 771 |
731 void ImmutableMessageGenerator:: | 772 void MessageGenerator::GenerateDescriptorMethods(io::Printer* printer) { |
732 GenerateDescriptorMethods(io::Printer* printer) { | 773 if (HasDescriptorMethods(descriptor_)) { |
733 if (!descriptor_->options().no_standard_descriptor_accessor()) { | 774 if (!descriptor_->options().no_standard_descriptor_accessor()) { |
734 printer->Print( | 775 printer->Print( |
735 "public static final com.google.protobuf.Descriptors.Descriptor\n" | 776 "public static final com.google.protobuf.Descriptors.Descriptor\n" |
736 " getDescriptor() {\n" | 777 " getDescriptor() {\n" |
| 778 " return $fileclass$.internal_$identifier$_descriptor;\n" |
| 779 "}\n" |
| 780 "\n", |
| 781 "fileclass", ClassName(descriptor_->file()), |
| 782 "identifier", UniqueFileScopeIdentifier(descriptor_)); |
| 783 } |
| 784 printer->Print( |
| 785 "protected com.google.protobuf.GeneratedMessage.FieldAccessorTable\n" |
| 786 " internalGetFieldAccessorTable() {\n" |
| 787 " return $fileclass$.internal_$identifier$_fieldAccessorTable\n" |
| 788 " .ensureFieldAccessorsInitialized(\n" |
| 789 " $classname$.class, $classname$.Builder.class);\n" |
| 790 "}\n" |
| 791 "\n", |
| 792 "classname", ClassName(descriptor_), |
| 793 "fileclass", ClassName(descriptor_->file()), |
| 794 "identifier", UniqueFileScopeIdentifier(descriptor_)); |
| 795 } |
| 796 } |
| 797 |
| 798 // =================================================================== |
| 799 |
| 800 void MessageGenerator::GenerateCommonBuilderMethods(io::Printer* printer) { |
| 801 printer->Print( |
| 802 "// Construct using $classname$.newBuilder()\n" |
| 803 "private Builder() {\n" |
| 804 " maybeForceBuilderInitialization();\n" |
| 805 "}\n" |
| 806 "\n", |
| 807 "classname", ClassName(descriptor_)); |
| 808 |
| 809 if (HasDescriptorMethods(descriptor_)) { |
| 810 printer->Print( |
| 811 "private Builder(\n" |
| 812 " com.google.protobuf.GeneratedMessage.BuilderParent parent) {\n" |
| 813 " super(parent);\n" |
| 814 " maybeForceBuilderInitialization();\n" |
| 815 "}\n", |
| 816 "classname", ClassName(descriptor_)); |
| 817 } |
| 818 |
| 819 |
| 820 if (HasNestedBuilders(descriptor_)) { |
| 821 printer->Print( |
| 822 "private void maybeForceBuilderInitialization() {\n" |
| 823 " if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {\n"); |
| 824 |
| 825 printer->Indent(); |
| 826 printer->Indent(); |
| 827 for (int i = 0; i < descriptor_->field_count(); i++) { |
| 828 field_generators_.get(descriptor_->field(i)) |
| 829 .GenerateFieldBuilderInitializationCode(printer); |
| 830 } |
| 831 printer->Outdent(); |
| 832 printer->Outdent(); |
| 833 |
| 834 printer->Print( |
| 835 " }\n" |
| 836 "}\n"); |
| 837 } else { |
| 838 printer->Print( |
| 839 "private void maybeForceBuilderInitialization() {\n" |
| 840 "}\n"); |
| 841 } |
| 842 |
| 843 printer->Print( |
| 844 "private static Builder create() {\n" |
| 845 " return new Builder();\n" |
| 846 "}\n" |
| 847 "\n" |
| 848 "public Builder clear() {\n" |
| 849 " super.clear();\n", |
| 850 "classname", ClassName(descriptor_)); |
| 851 |
| 852 printer->Indent(); |
| 853 |
| 854 for (int i = 0; i < descriptor_->field_count(); i++) { |
| 855 field_generators_.get(descriptor_->field(i)) |
| 856 .GenerateBuilderClearCode(printer); |
| 857 } |
| 858 |
| 859 printer->Outdent(); |
| 860 |
| 861 printer->Print( |
| 862 " return this;\n" |
| 863 "}\n" |
| 864 "\n" |
| 865 "public Builder clone() {\n" |
| 866 " return create().mergeFrom(buildPartial());\n" |
| 867 "}\n" |
| 868 "\n", |
| 869 "classname", ClassName(descriptor_)); |
| 870 if (HasDescriptorMethods(descriptor_)) { |
| 871 printer->Print( |
| 872 "public com.google.protobuf.Descriptors.Descriptor\n" |
| 873 " getDescriptorForType() {\n" |
737 " return $fileclass$.internal_$identifier$_descriptor;\n" | 874 " return $fileclass$.internal_$identifier$_descriptor;\n" |
738 "}\n" | 875 "}\n" |
739 "\n", | 876 "\n", |
740 "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), | 877 "fileclass", ClassName(descriptor_->file()), |
741 "identifier", UniqueFileScopeIdentifier(descriptor_)); | 878 "identifier", UniqueFileScopeIdentifier(descriptor_)); |
742 } | 879 } |
743 vector<const FieldDescriptor*> map_fields; | 880 printer->Print( |
744 for (int i = 0; i < descriptor_->field_count(); i++) { | 881 "public $classname$ getDefaultInstanceForType() {\n" |
745 const FieldDescriptor* field = descriptor_->field(i); | 882 " return $classname$.getDefaultInstance();\n" |
746 if (GetJavaType(field) == JAVATYPE_MESSAGE && | 883 "}\n" |
747 IsMapEntry(field->message_type())) { | 884 "\n", |
748 map_fields.push_back(field); | 885 "classname", ClassName(descriptor_)); |
749 } | 886 |
750 } | 887 // ----------------------------------------------------------------- |
751 if (!map_fields.empty()) { | 888 |
752 printer->Print( | 889 printer->Print( |
753 "@SuppressWarnings({\"rawtypes\"})\n" | 890 "public $classname$ build() {\n" |
754 "protected com.google.protobuf.MapField internalGetMapField(\n" | 891 " $classname$ result = buildPartial();\n" |
755 " int number) {\n" | 892 " if (!result.isInitialized()) {\n" |
756 " switch (number) {\n"); | 893 " throw newUninitializedMessageException(result);\n" |
| 894 " }\n" |
| 895 " return result;\n" |
| 896 "}\n" |
| 897 "\n" |
| 898 "public $classname$ buildPartial() {\n" |
| 899 " $classname$ result = new $classname$(this);\n", |
| 900 "classname", ClassName(descriptor_)); |
| 901 |
| 902 printer->Indent(); |
| 903 |
| 904 // Local vars for from and to bit fields to avoid accessing the builder and |
| 905 // message over and over for these fields. Seems to provide a slight |
| 906 // perforamance improvement in micro benchmark and this is also what proto1 |
| 907 // code does. |
| 908 int totalBuilderBits = 0; |
| 909 int totalMessageBits = 0; |
| 910 for (int i = 0; i < descriptor_->field_count(); i++) { |
| 911 const FieldGenerator& field = field_generators_.get(descriptor_->field(i)); |
| 912 totalBuilderBits += field.GetNumBitsForBuilder(); |
| 913 totalMessageBits += field.GetNumBitsForMessage(); |
| 914 } |
| 915 int totalBuilderInts = (totalBuilderBits + 31) / 32; |
| 916 int totalMessageInts = (totalMessageBits + 31) / 32; |
| 917 for (int i = 0; i < totalBuilderInts; i++) { |
| 918 printer->Print("int from_$bit_field_name$ = $bit_field_name$;\n", |
| 919 "bit_field_name", GetBitFieldName(i)); |
| 920 } |
| 921 for (int i = 0; i < totalMessageInts; i++) { |
| 922 printer->Print("int to_$bit_field_name$ = 0;\n", |
| 923 "bit_field_name", GetBitFieldName(i)); |
| 924 } |
| 925 |
| 926 // Output generation code for each field. |
| 927 for (int i = 0; i < descriptor_->field_count(); i++) { |
| 928 field_generators_.get(descriptor_->field(i)).GenerateBuildingCode(printer); |
| 929 } |
| 930 |
| 931 // Copy the bit field results to the generated message |
| 932 for (int i = 0; i < totalMessageInts; i++) { |
| 933 printer->Print("result.$bit_field_name$ = to_$bit_field_name$;\n", |
| 934 "bit_field_name", GetBitFieldName(i)); |
| 935 } |
| 936 |
| 937 printer->Outdent(); |
| 938 |
| 939 if (HasDescriptorMethods(descriptor_)) { |
| 940 printer->Print( |
| 941 " onBuilt();\n"); |
| 942 } |
| 943 |
| 944 printer->Print( |
| 945 " return result;\n" |
| 946 "}\n" |
| 947 "\n", |
| 948 "classname", ClassName(descriptor_)); |
| 949 |
| 950 // ----------------------------------------------------------------- |
| 951 |
| 952 if (HasGeneratedMethods(descriptor_)) { |
| 953 // MergeFrom(Message other) requires the ability to distinguish the other |
| 954 // messages type by its descriptor. |
| 955 if (HasDescriptorMethods(descriptor_)) { |
| 956 printer->Print( |
| 957 "public Builder mergeFrom(com.google.protobuf.Message other) {\n" |
| 958 " if (other instanceof $classname$) {\n" |
| 959 " return mergeFrom(($classname$)other);\n" |
| 960 " } else {\n" |
| 961 " super.mergeFrom(other);\n" |
| 962 " return this;\n" |
| 963 " }\n" |
| 964 "}\n" |
| 965 "\n", |
| 966 "classname", ClassName(descriptor_)); |
| 967 } |
| 968 |
| 969 printer->Print( |
| 970 "public Builder mergeFrom($classname$ other) {\n" |
| 971 // Optimization: If other is the default instance, we know none of its |
| 972 // fields are set so we can skip the merge. |
| 973 " if (other == $classname$.getDefaultInstance()) return this;\n", |
| 974 "classname", ClassName(descriptor_)); |
757 printer->Indent(); | 975 printer->Indent(); |
758 printer->Indent(); | 976 |
759 for (int i = 0; i < map_fields.size(); ++i) { | 977 for (int i = 0; i < descriptor_->field_count(); i++) { |
760 const FieldDescriptor* field = map_fields[i]; | 978 field_generators_.get(descriptor_->field(i)).GenerateMergingCode(printer); |
761 const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); | 979 } |
762 printer->Print( | 980 |
763 "case $number$:\n" | |
764 " return internalGet$capitalized_name$();\n", | |
765 "number", SimpleItoa(field->number()), | |
766 "capitalized_name", info->capitalized_name); | |
767 } | |
768 printer->Print( | |
769 "default:\n" | |
770 " throw new RuntimeException(\n" | |
771 " \"Invalid map field number: \" + number);\n"); | |
772 printer->Outdent(); | 981 printer->Outdent(); |
773 printer->Outdent(); | 982 |
774 printer->Print( | 983 // if message type has extensions |
775 " }\n" | 984 if (descriptor_->extension_range_count() > 0) { |
776 "}\n"); | 985 printer->Print( |
777 } | 986 " this.mergeExtensionFields(other);\n"); |
778 printer->Print( | 987 } |
779 "protected com.google.protobuf.GeneratedMessage.FieldAccessorTable\n" | 988 |
780 " internalGetFieldAccessorTable() {\n" | 989 if (HasUnknownFields(descriptor_)) { |
781 " return $fileclass$.internal_$identifier$_fieldAccessorTable\n" | 990 printer->Print( |
782 " .ensureFieldAccessorsInitialized(\n" | 991 " this.mergeUnknownFields(other.getUnknownFields());\n"); |
783 " $classname$.class, $classname$.Builder.class);\n" | 992 } |
784 "}\n" | 993 |
785 "\n", | 994 printer->Print( |
786 "classname", name_resolver_->GetImmutableClassName(descriptor_), | 995 " return this;\n" |
787 "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()), | 996 "}\n" |
788 "identifier", UniqueFileScopeIdentifier(descriptor_)); | 997 "\n"); |
| 998 } |
789 } | 999 } |
790 | 1000 |
791 // =================================================================== | 1001 // =================================================================== |
792 | 1002 |
793 void ImmutableMessageGenerator::GenerateIsInitialized( | 1003 void MessageGenerator::GenerateBuilderParsingMethods(io::Printer* printer) { |
794 io::Printer* printer) { | 1004 printer->Print( |
795 // Memoizes whether the protocol buffer is fully initialized (has all | 1005 "public Builder mergeFrom(\n" |
796 // required fields). -1 means not yet computed. 0 means false and 1 means | 1006 " com.google.protobuf.CodedInputStream input,\n" |
797 // true. | 1007 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" |
798 printer->Print( | 1008 " throws java.io.IOException {\n" |
799 "private byte memoizedIsInitialized = -1;\n"); | 1009 " $classname$ parsedMessage = null;\n" |
| 1010 " try {\n" |
| 1011 " parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);\n" |
| 1012 " } catch (com.google.protobuf.InvalidProtocolBufferException e) {\n" |
| 1013 " parsedMessage = ($classname$) e.getUnfinishedMessage();\n" |
| 1014 " throw e;\n" |
| 1015 " } finally {\n" |
| 1016 " if (parsedMessage != null) {\n" |
| 1017 " mergeFrom(parsedMessage);\n" |
| 1018 " }\n" |
| 1019 " }\n" |
| 1020 " return this;\n" |
| 1021 "}\n", |
| 1022 "classname", ClassName(descriptor_)); |
| 1023 } |
| 1024 |
| 1025 // =================================================================== |
| 1026 |
| 1027 void MessageGenerator::GenerateIsInitialized( |
| 1028 io::Printer* printer, UseMemoization useMemoization) { |
| 1029 bool memoization = useMemoization == MEMOIZE; |
| 1030 if (memoization) { |
| 1031 // Memoizes whether the protocol buffer is fully initialized (has all |
| 1032 // required fields). -1 means not yet computed. 0 means false and 1 means |
| 1033 // true. |
| 1034 printer->Print( |
| 1035 "private byte memoizedIsInitialized = -1;\n"); |
| 1036 } |
800 printer->Print( | 1037 printer->Print( |
801 "public final boolean isInitialized() {\n"); | 1038 "public final boolean isInitialized() {\n"); |
802 printer->Indent(); | 1039 printer->Indent(); |
803 | 1040 |
804 // Don't directly compare to -1 to avoid an Android x86 JIT bug. | 1041 if (memoization) { |
805 printer->Print( | 1042 printer->Print( |
806 "byte isInitialized = memoizedIsInitialized;\n" | 1043 "byte isInitialized = memoizedIsInitialized;\n" |
807 "if (isInitialized == 1) return true;\n" | 1044 "if (isInitialized != -1) return isInitialized == 1;\n" |
808 "if (isInitialized == 0) return false;\n" | 1045 "\n"); |
809 "\n"); | 1046 } |
810 | 1047 |
811 // Check that all required fields in this message are set. | 1048 // Check that all required fields in this message are set. |
812 // TODO(kenton): We can optimize this when we switch to putting all the | 1049 // TODO(kenton): We can optimize this when we switch to putting all the |
813 // "has" fields into a single bitfield. | 1050 // "has" fields into a single bitfield. |
814 for (int i = 0; i < descriptor_->field_count(); i++) { | 1051 for (int i = 0; i < descriptor_->field_count(); i++) { |
815 const FieldDescriptor* field = descriptor_->field(i); | 1052 const FieldDescriptor* field = descriptor_->field(i); |
816 const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); | |
817 | 1053 |
818 if (field->is_required()) { | 1054 if (field->is_required()) { |
819 printer->Print( | 1055 printer->Print( |
820 "if (!has$name$()) {\n" | 1056 "if (!has$name$()) {\n" |
821 " memoizedIsInitialized = 0;\n" | 1057 " $memoize$\n" |
822 " return false;\n" | 1058 " return false;\n" |
823 "}\n", | 1059 "}\n", |
824 "name", info->capitalized_name); | 1060 "name", UnderscoresToCapitalizedCamelCase(field), |
| 1061 "memoize", memoization ? "memoizedIsInitialized = 0;" : ""); |
825 } | 1062 } |
826 } | 1063 } |
827 | 1064 |
828 // Now check that all embedded messages are initialized. | 1065 // Now check that all embedded messages are initialized. |
829 for (int i = 0; i < descriptor_->field_count(); i++) { | 1066 for (int i = 0; i < descriptor_->field_count(); i++) { |
830 const FieldDescriptor* field = descriptor_->field(i); | 1067 const FieldDescriptor* field = descriptor_->field(i); |
831 const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); | |
832 if (GetJavaType(field) == JAVATYPE_MESSAGE && | 1068 if (GetJavaType(field) == JAVATYPE_MESSAGE && |
833 HasRequiredFields(field->message_type())) { | 1069 HasRequiredFields(field->message_type())) { |
834 switch (field->label()) { | 1070 switch (field->label()) { |
835 case FieldDescriptor::LABEL_REQUIRED: | 1071 case FieldDescriptor::LABEL_REQUIRED: |
836 printer->Print( | 1072 printer->Print( |
837 "if (!get$name$().isInitialized()) {\n" | 1073 "if (!get$name$().isInitialized()) {\n" |
838 " memoizedIsInitialized = 0;\n" | 1074 " $memoize$\n" |
839 " return false;\n" | 1075 " return false;\n" |
840 "}\n", | 1076 "}\n", |
841 "type", name_resolver_->GetImmutableClassName( | 1077 "type", ClassName(field->message_type()), |
842 field->message_type()), | 1078 "name", UnderscoresToCapitalizedCamelCase(field), |
843 "name", info->capitalized_name); | 1079 "memoize", memoization ? "memoizedIsInitialized = 0;" : ""); |
844 break; | 1080 break; |
845 case FieldDescriptor::LABEL_OPTIONAL: | 1081 case FieldDescriptor::LABEL_OPTIONAL: |
846 if (!SupportFieldPresence(descriptor_->file()) && | |
847 field->containing_oneof() != NULL) { | |
848 const OneofDescriptor* oneof = field->containing_oneof(); | |
849 const OneofGeneratorInfo* oneof_info = | |
850 context_->GetOneofGeneratorInfo(oneof); | |
851 printer->Print( | |
852 "if ($oneof_name$Case_ == $field_number$) {\n", | |
853 "oneof_name", oneof_info->name, | |
854 "field_number", SimpleItoa(field->number())); | |
855 } else { | |
856 printer->Print( | |
857 "if (has$name$()) {\n", | |
858 "name", info->capitalized_name); | |
859 } | |
860 printer->Print( | 1082 printer->Print( |
| 1083 "if (has$name$()) {\n" |
861 " if (!get$name$().isInitialized()) {\n" | 1084 " if (!get$name$().isInitialized()) {\n" |
862 " memoizedIsInitialized = 0;\n" | 1085 " $memoize$\n" |
863 " return false;\n" | 1086 " return false;\n" |
864 " }\n" | 1087 " }\n" |
865 "}\n", | 1088 "}\n", |
866 "name", info->capitalized_name); | 1089 "type", ClassName(field->message_type()), |
| 1090 "name", UnderscoresToCapitalizedCamelCase(field), |
| 1091 "memoize", memoization ? "memoizedIsInitialized = 0;" : ""); |
867 break; | 1092 break; |
868 case FieldDescriptor::LABEL_REPEATED: | 1093 case FieldDescriptor::LABEL_REPEATED: |
869 if (IsMapEntry(field->message_type())) { | 1094 printer->Print( |
870 printer->Print( | 1095 "for (int i = 0; i < get$name$Count(); i++) {\n" |
871 "for ($type$ item : get$name$().values()) {\n" | 1096 " if (!get$name$(i).isInitialized()) {\n" |
872 " if (!item.isInitialized()) {\n" | 1097 " $memoize$\n" |
873 " memoizedIsInitialized = 0;\n" | 1098 " return false;\n" |
874 " return false;\n" | 1099 " }\n" |
875 " }\n" | 1100 "}\n", |
876 "}\n", | 1101 "type", ClassName(field->message_type()), |
877 "type", MapValueImmutableClassdName(field->message_type(), | 1102 "name", UnderscoresToCapitalizedCamelCase(field), |
878 name_resolver_), | 1103 "memoize", memoization ? "memoizedIsInitialized = 0;" : ""); |
879 "name", info->capitalized_name); | |
880 } else { | |
881 printer->Print( | |
882 "for (int i = 0; i < get$name$Count(); i++) {\n" | |
883 " if (!get$name$(i).isInitialized()) {\n" | |
884 " memoizedIsInitialized = 0;\n" | |
885 " return false;\n" | |
886 " }\n" | |
887 "}\n", | |
888 "type", name_resolver_->GetImmutableClassName( | |
889 field->message_type()), | |
890 "name", info->capitalized_name); | |
891 } | |
892 break; | 1104 break; |
893 } | 1105 } |
894 } | 1106 } |
895 } | 1107 } |
896 | 1108 |
897 if (descriptor_->extension_range_count() > 0) { | 1109 if (descriptor_->extension_range_count() > 0) { |
898 printer->Print( | 1110 printer->Print( |
899 "if (!extensionsAreInitialized()) {\n" | 1111 "if (!extensionsAreInitialized()) {\n" |
900 " memoizedIsInitialized = 0;\n" | 1112 " $memoize$\n" |
901 " return false;\n" | 1113 " return false;\n" |
902 "}\n"); | 1114 "}\n", |
| 1115 "memoize", memoization ? "memoizedIsInitialized = 0;" : ""); |
903 } | 1116 } |
904 | 1117 |
905 printer->Outdent(); | 1118 printer->Outdent(); |
906 | 1119 |
907 printer->Print( | 1120 if (memoization) { |
908 " memoizedIsInitialized = 1;\n"); | 1121 printer->Print( |
| 1122 " memoizedIsInitialized = 1;\n"); |
| 1123 } |
909 | 1124 |
910 printer->Print( | 1125 printer->Print( |
911 " return true;\n" | 1126 " return true;\n" |
912 "}\n" | 1127 "}\n" |
913 "\n"); | 1128 "\n"); |
914 } | 1129 } |
915 | 1130 |
916 // =================================================================== | 1131 // =================================================================== |
917 | 1132 |
918 namespace { | 1133 void MessageGenerator::GenerateEqualsAndHashCode(io::Printer* printer) { |
919 bool CheckHasBitsForEqualsAndHashCode(const FieldDescriptor* field) { | |
920 if (field->is_repeated()) { | |
921 return false; | |
922 } | |
923 if (SupportFieldPresence(field->file())) { | |
924 return true; | |
925 } | |
926 return GetJavaType(field) == JAVATYPE_MESSAGE && | |
927 field->containing_oneof() == NULL; | |
928 } | |
929 } // namespace | |
930 | |
931 void ImmutableMessageGenerator:: | |
932 GenerateEqualsAndHashCode(io::Printer* printer) { | |
933 printer->Print( | 1134 printer->Print( |
934 "@java.lang.Override\n" | 1135 "@java.lang.Override\n" |
935 "public boolean equals(final java.lang.Object obj) {\n"); | 1136 "public boolean equals(final java.lang.Object obj) {\n"); |
936 printer->Indent(); | 1137 printer->Indent(); |
937 printer->Print( | 1138 printer->Print( |
938 "if (obj == this) {\n" | 1139 "if (obj == this) {\n" |
939 " return true;\n" | 1140 " return true;\n" |
940 "}\n" | 1141 "}\n" |
941 "if (!(obj instanceof $classname$)) {\n" | 1142 "if (!(obj instanceof $classname$)) {\n" |
942 " return super.equals(obj);\n" | 1143 " return super.equals(obj);\n" |
943 "}\n" | 1144 "}\n" |
944 "$classname$ other = ($classname$) obj;\n" | 1145 "$classname$ other = ($classname$) obj;\n" |
945 "\n", | 1146 "\n", |
946 "classname", name_resolver_->GetImmutableClassName(descriptor_)); | 1147 "classname", ClassName(descriptor_)); |
947 | 1148 |
948 printer->Print("boolean result = true;\n"); | 1149 printer->Print("boolean result = true;\n"); |
949 for (int i = 0; i < descriptor_->field_count(); i++) { | 1150 for (int i = 0; i < descriptor_->field_count(); i++) { |
950 const FieldDescriptor* field = descriptor_->field(i); | 1151 const FieldDescriptor* field = descriptor_->field(i); |
951 const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); | 1152 if (!field->is_repeated()) { |
952 bool check_has_bits = CheckHasBitsForEqualsAndHashCode(field); | |
953 if (check_has_bits) { | |
954 printer->Print( | 1153 printer->Print( |
955 "result = result && (has$name$() == other.has$name$());\n" | 1154 "result = result && (has$name$() == other.has$name$());\n" |
956 "if (has$name$()) {\n", | 1155 "if (has$name$()) {\n", |
957 "name", info->capitalized_name); | 1156 "name", UnderscoresToCapitalizedCamelCase(field)); |
958 printer->Indent(); | 1157 printer->Indent(); |
959 } | 1158 } |
960 field_generators_.get(field).GenerateEqualsCode(printer); | 1159 field_generators_.get(field).GenerateEqualsCode(printer); |
961 if (check_has_bits) { | 1160 if (!field->is_repeated()) { |
962 printer->Outdent(); | 1161 printer->Outdent(); |
963 printer->Print( | 1162 printer->Print( |
964 "}\n"); | 1163 "}\n"); |
965 } | 1164 } |
966 } | 1165 } |
967 if (PreserveUnknownFields(descriptor_)) { | 1166 if (HasDescriptorMethods(descriptor_)) { |
968 // Always consider unknown fields for equality. This will sometimes return | |
969 // false for non-canonical ordering when running in LITE_RUNTIME but it's | |
970 // the best we can do. | |
971 printer->Print( | |
972 "result = result && unknownFields.equals(other.unknownFields);\n"); | |
973 } | |
974 if (descriptor_->extension_range_count() > 0) { | |
975 printer->Print( | 1167 printer->Print( |
976 "result = result &&\n" | 1168 "result = result &&\n" |
977 " getExtensionFields().equals(other.getExtensionFields());\n"); | 1169 " getUnknownFields().equals(other.getUnknownFields());\n"); |
| 1170 if (descriptor_->extension_range_count() > 0) { |
| 1171 printer->Print( |
| 1172 "result = result &&\n" |
| 1173 " getExtensionFields().equals(other.getExtensionFields());\n"); |
| 1174 } |
978 } | 1175 } |
979 printer->Print( | 1176 printer->Print( |
980 "return result;\n"); | 1177 "return result;\n"); |
981 printer->Outdent(); | 1178 printer->Outdent(); |
982 printer->Print( | 1179 printer->Print( |
983 "}\n" | 1180 "}\n" |
984 "\n"); | 1181 "\n"); |
985 | 1182 |
986 printer->Print( | 1183 printer->Print( |
| 1184 "private int memoizedHashCode = 0;\n"); |
| 1185 printer->Print( |
987 "@java.lang.Override\n" | 1186 "@java.lang.Override\n" |
988 "public int hashCode() {\n"); | 1187 "public int hashCode() {\n"); |
989 printer->Indent(); | 1188 printer->Indent(); |
990 printer->Print( | 1189 printer->Print( |
991 "if (memoizedHashCode != 0) {\n"); | 1190 "if (memoizedHashCode != 0) {\n"); |
992 printer->Indent(); | 1191 printer->Indent(); |
993 printer->Print( | 1192 printer->Print( |
994 "return memoizedHashCode;\n"); | 1193 "return memoizedHashCode;\n"); |
995 printer->Outdent(); | 1194 printer->Outdent(); |
996 printer->Print( | 1195 printer->Print( |
997 "}\n" | 1196 "}\n" |
998 "int hash = 41;\n"); | 1197 "int hash = 41;\n" |
999 | 1198 "hash = (19 * hash) + getDescriptorForType().hashCode();\n"); |
1000 printer->Print("hash = (19 * hash) + getDescriptorForType().hashCode();\n"); | |
1001 | |
1002 for (int i = 0; i < descriptor_->field_count(); i++) { | 1199 for (int i = 0; i < descriptor_->field_count(); i++) { |
1003 const FieldDescriptor* field = descriptor_->field(i); | 1200 const FieldDescriptor* field = descriptor_->field(i); |
1004 const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field); | 1201 if (!field->is_repeated()) { |
1005 bool check_has_bits = CheckHasBitsForEqualsAndHashCode(field); | |
1006 if (check_has_bits) { | |
1007 printer->Print( | 1202 printer->Print( |
1008 "if (has$name$()) {\n", | 1203 "if (has$name$()) {\n", |
1009 "name", info->capitalized_name); | 1204 "name", UnderscoresToCapitalizedCamelCase(field)); |
1010 printer->Indent(); | 1205 printer->Indent(); |
1011 } | 1206 } |
1012 field_generators_.get(field).GenerateHashCode(printer); | 1207 field_generators_.get(field).GenerateHashCode(printer); |
1013 if (check_has_bits) { | 1208 if (!field->is_repeated()) { |
1014 printer->Outdent(); | 1209 printer->Outdent(); |
1015 printer->Print("}\n"); | 1210 printer->Print("}\n"); |
1016 } | 1211 } |
1017 } | 1212 } |
1018 if (descriptor_->extension_range_count() > 0) { | 1213 if (HasDescriptorMethods(descriptor_)) { |
1019 printer->Print( | 1214 if (descriptor_->extension_range_count() > 0) { |
1020 "hash = hashFields(hash, getExtensionFields());\n"); | 1215 printer->Print( |
| 1216 "hash = hashFields(hash, getExtensionFields());\n"); |
| 1217 } |
1021 } | 1218 } |
1022 | |
1023 printer->Print( | 1219 printer->Print( |
1024 "hash = (29 * hash) + unknownFields.hashCode();\n"); | 1220 "hash = (29 * hash) + getUnknownFields().hashCode();\n" |
1025 printer->Print( | |
1026 "memoizedHashCode = hash;\n" | 1221 "memoizedHashCode = hash;\n" |
1027 "return hash;\n"); | 1222 "return hash;\n"); |
1028 printer->Outdent(); | 1223 printer->Outdent(); |
1029 printer->Print( | 1224 printer->Print( |
1030 "}\n" | 1225 "}\n" |
1031 "\n"); | 1226 "\n"); |
1032 } | 1227 } |
1033 | 1228 |
1034 // =================================================================== | 1229 // =================================================================== |
1035 | 1230 |
1036 void ImmutableMessageGenerator:: | 1231 void MessageGenerator::GenerateExtensionRegistrationCode(io::Printer* printer) { |
1037 GenerateExtensionRegistrationCode(io::Printer* printer) { | |
1038 for (int i = 0; i < descriptor_->extension_count(); i++) { | 1232 for (int i = 0; i < descriptor_->extension_count(); i++) { |
1039 ImmutableExtensionGenerator(descriptor_->extension(i), context_) | 1233 ExtensionGenerator(descriptor_->extension(i)) |
1040 .GenerateRegistrationCode(printer); | 1234 .GenerateRegistrationCode(printer); |
1041 } | 1235 } |
1042 | 1236 |
1043 for (int i = 0; i < descriptor_->nested_type_count(); i++) { | 1237 for (int i = 0; i < descriptor_->nested_type_count(); i++) { |
1044 ImmutableMessageGenerator(descriptor_->nested_type(i), context_) | 1238 MessageGenerator(descriptor_->nested_type(i)) |
1045 .GenerateExtensionRegistrationCode(printer); | 1239 .GenerateExtensionRegistrationCode(printer); |
1046 } | 1240 } |
1047 } | 1241 } |
1048 | 1242 |
1049 // =================================================================== | 1243 // =================================================================== |
1050 void ImmutableMessageGenerator:: | 1244 void MessageGenerator::GenerateParsingConstructor(io::Printer* printer) { |
1051 GenerateParsingConstructor(io::Printer* printer) { | 1245 scoped_array<const FieldDescriptor*> sorted_fields( |
1052 google::protobuf::scoped_array<const FieldDescriptor * > sorted_fields( | |
1053 SortFieldsByNumber(descriptor_)); | 1246 SortFieldsByNumber(descriptor_)); |
1054 | 1247 |
1055 printer->Print( | 1248 printer->Print( |
1056 "private $classname$(\n" | 1249 "private $classname$(\n" |
1057 " com.google.protobuf.CodedInputStream input,\n" | 1250 " com.google.protobuf.CodedInputStream input,\n" |
1058 " com.google.protobuf.ExtensionRegistryLite extensionRegistry) {\n", | 1251 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" |
| 1252 " throws com.google.protobuf.InvalidProtocolBufferException {\n", |
1059 "classname", descriptor_->name()); | 1253 "classname", descriptor_->name()); |
1060 printer->Indent(); | 1254 printer->Indent(); |
1061 | 1255 |
1062 // Initialize all fields to default. | 1256 // Initialize all fields to default. |
1063 printer->Print( | 1257 printer->Print( |
1064 "this();\n"); | 1258 "initFields();\n"); |
1065 | 1259 |
1066 // Use builder bits to track mutable repeated fields. | 1260 // Use builder bits to track mutable repeated fields. |
1067 int totalBuilderBits = 0; | 1261 int totalBuilderBits = 0; |
1068 for (int i = 0; i < descriptor_->field_count(); i++) { | 1262 for (int i = 0; i < descriptor_->field_count(); i++) { |
1069 const ImmutableFieldGenerator& field = | 1263 const FieldGenerator& field = field_generators_.get(descriptor_->field(i)); |
1070 field_generators_.get(descriptor_->field(i)); | |
1071 totalBuilderBits += field.GetNumBitsForBuilder(); | 1264 totalBuilderBits += field.GetNumBitsForBuilder(); |
1072 } | 1265 } |
1073 int totalBuilderInts = (totalBuilderBits + 31) / 32; | 1266 int totalBuilderInts = (totalBuilderBits + 31) / 32; |
1074 for (int i = 0; i < totalBuilderInts; i++) { | 1267 for (int i = 0; i < totalBuilderInts; i++) { |
1075 printer->Print("int mutable_$bit_field_name$ = 0;\n", | 1268 printer->Print("int mutable_$bit_field_name$ = 0;\n", |
1076 "bit_field_name", GetBitFieldName(i)); | 1269 "bit_field_name", GetBitFieldName(i)); |
1077 } | 1270 } |
1078 | 1271 |
1079 if (PreserveUnknownFields(descriptor_)) { | 1272 if (HasUnknownFields(descriptor_)) { |
1080 printer->Print( | 1273 printer->Print( |
1081 "com.google.protobuf.UnknownFieldSet.Builder unknownFields =\n" | 1274 "com.google.protobuf.UnknownFieldSet.Builder unknownFields =\n" |
1082 " com.google.protobuf.UnknownFieldSet.newBuilder();\n"); | 1275 " com.google.protobuf.UnknownFieldSet.newBuilder();\n"); |
1083 } | 1276 } |
1084 | 1277 |
1085 printer->Print( | 1278 printer->Print( |
1086 "try {\n"); | 1279 "try {\n"); |
1087 printer->Indent(); | 1280 printer->Indent(); |
1088 | 1281 |
1089 printer->Print( | 1282 printer->Print( |
1090 "boolean done = false;\n" | 1283 "boolean done = false;\n" |
1091 "while (!done) {\n"); | 1284 "while (!done) {\n"); |
1092 printer->Indent(); | 1285 printer->Indent(); |
1093 | 1286 |
1094 printer->Print( | 1287 printer->Print( |
1095 "int tag = input.readTag();\n" | 1288 "int tag = input.readTag();\n" |
1096 "switch (tag) {\n"); | 1289 "switch (tag) {\n"); |
1097 printer->Indent(); | 1290 printer->Indent(); |
1098 | 1291 |
1099 printer->Print( | 1292 printer->Print( |
1100 "case 0:\n" // zero signals EOF / limit reached | 1293 "case 0:\n" // zero signals EOF / limit reached |
1101 " done = true;\n" | 1294 " done = true;\n" |
1102 " break;\n"); | 1295 " break;\n" |
1103 | 1296 "default: {\n" |
1104 if (PreserveUnknownFields(descriptor_)) { | 1297 " if (!parseUnknownField(input,$unknown_fields$\n" |
1105 printer->Print( | 1298 " extensionRegistry, tag)) {\n" |
1106 "default: {\n" | 1299 " done = true;\n" // it's an endgroup tag |
1107 " if (!parseUnknownField(input, unknownFields,\n" | 1300 " }\n" |
1108 " extensionRegistry, tag)) {\n" | 1301 " break;\n" |
1109 " done = true;\n" // it's an endgroup tag | 1302 "}\n", |
1110 " }\n" | 1303 "unknown_fields", HasUnknownFields(descriptor_) |
1111 " break;\n" | 1304 ? " unknownFields," : ""); |
1112 "}\n"); | |
1113 } else { | |
1114 printer->Print( | |
1115 "default: {\n" | |
1116 " if (!input.skipField(tag)) {\n" | |
1117 " done = true;\n" // it's an endgroup tag | |
1118 " }\n" | |
1119 " break;\n" | |
1120 "}\n"); | |
1121 } | |
1122 | 1305 |
1123 for (int i = 0; i < descriptor_->field_count(); i++) { | 1306 for (int i = 0; i < descriptor_->field_count(); i++) { |
1124 const FieldDescriptor* field = sorted_fields[i]; | 1307 const FieldDescriptor* field = sorted_fields[i]; |
1125 uint32 tag = WireFormatLite::MakeTag(field->number(), | 1308 uint32 tag = WireFormatLite::MakeTag(field->number(), |
1126 WireFormat::WireTypeForFieldType(field->type())); | 1309 WireFormat::WireTypeForFieldType(field->type())); |
1127 | 1310 |
1128 printer->Print( | 1311 printer->Print( |
1129 "case $tag$: {\n", | 1312 "case $tag$: {\n", |
1130 "tag", SimpleItoa(tag)); | 1313 "tag", SimpleItoa(tag)); |
1131 printer->Indent(); | 1314 printer->Indent(); |
(...skipping 26 matching lines...) Expand all Loading... |
1158 | 1341 |
1159 printer->Outdent(); | 1342 printer->Outdent(); |
1160 printer->Outdent(); | 1343 printer->Outdent(); |
1161 printer->Print( | 1344 printer->Print( |
1162 " }\n" // switch (tag) | 1345 " }\n" // switch (tag) |
1163 "}\n"); // while (!done) | 1346 "}\n"); // while (!done) |
1164 | 1347 |
1165 printer->Outdent(); | 1348 printer->Outdent(); |
1166 printer->Print( | 1349 printer->Print( |
1167 "} catch (com.google.protobuf.InvalidProtocolBufferException e) {\n" | 1350 "} catch (com.google.protobuf.InvalidProtocolBufferException e) {\n" |
1168 " throw new RuntimeException(e.setUnfinishedMessage(this));\n" | 1351 " throw e.setUnfinishedMessage(this);\n" |
1169 "} catch (java.io.IOException e) {\n" | 1352 "} catch (java.io.IOException e) {\n" |
1170 " throw new RuntimeException(\n" | 1353 " throw new com.google.protobuf.InvalidProtocolBufferException(\n" |
1171 " new com.google.protobuf.InvalidProtocolBufferException(\n" | 1354 " e.getMessage()).setUnfinishedMessage(this);\n" |
1172 " e.getMessage()).setUnfinishedMessage(this));\n" | |
1173 "} finally {\n"); | 1355 "} finally {\n"); |
1174 printer->Indent(); | 1356 printer->Indent(); |
1175 | 1357 |
1176 // Make repeated field list immutable. | 1358 // Make repeated field list immutable. |
1177 for (int i = 0; i < descriptor_->field_count(); i++) { | 1359 for (int i = 0; i < descriptor_->field_count(); i++) { |
1178 const FieldDescriptor* field = sorted_fields[i]; | 1360 const FieldDescriptor* field = sorted_fields[i]; |
1179 field_generators_.get(field).GenerateParsingDoneCode(printer); | 1361 field_generators_.get(field).GenerateParsingDoneCode(printer); |
1180 } | 1362 } |
1181 | 1363 |
1182 if (PreserveUnknownFields(descriptor_)) { | 1364 // Make unknown fields immutable. |
1183 // Make unknown fields immutable. | 1365 if (HasUnknownFields(descriptor_)) { |
1184 printer->Print("this.unknownFields = unknownFields.build();\n"); | 1366 printer->Print( |
| 1367 "this.unknownFields = unknownFields.build();\n"); |
1185 } | 1368 } |
1186 | 1369 |
1187 // Make extensions immutable. | 1370 // Make extensions immutable. |
1188 printer->Print( | 1371 printer->Print( |
1189 "makeExtensionsImmutable();\n"); | 1372 "makeExtensionsImmutable();\n"); |
1190 | 1373 |
1191 printer->Outdent(); | 1374 printer->Outdent(); |
1192 printer->Outdent(); | 1375 printer->Outdent(); |
1193 printer->Print( | 1376 printer->Print( |
1194 " }\n" // finally | 1377 " }\n" // finally |
1195 "}\n"); | 1378 "}\n"); |
1196 } | 1379 } |
1197 | 1380 |
1198 // =================================================================== | 1381 // =================================================================== |
1199 void ImmutableMessageGenerator::GenerateParser(io::Printer* printer) { | 1382 void MessageGenerator::GenerateParser(io::Printer* printer) { |
1200 printer->Print( | 1383 printer->Print( |
1201 "public static final com.google.protobuf.Parser<$classname$> PARSER =\n" | 1384 "public static com.google.protobuf.Parser<$classname$> PARSER =\n" |
1202 " new com.google.protobuf.AbstractParser<$classname$>() {\n", | 1385 " new com.google.protobuf.AbstractParser<$classname$>() {\n", |
1203 "classname", descriptor_->name()); | 1386 "classname", descriptor_->name()); |
1204 printer->Indent(); | 1387 printer->Indent(); |
1205 printer->Print( | 1388 printer->Print( |
1206 "public $classname$ parsePartialFrom(\n" | 1389 "public $classname$ parsePartialFrom(\n" |
1207 " com.google.protobuf.CodedInputStream input,\n" | 1390 " com.google.protobuf.CodedInputStream input,\n" |
1208 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" | 1391 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n" |
1209 " throws com.google.protobuf.InvalidProtocolBufferException {\n", | 1392 " throws com.google.protobuf.InvalidProtocolBufferException {\n", |
1210 "classname", descriptor_->name()); | 1393 "classname", descriptor_->name()); |
1211 if (HasGeneratedMethods(descriptor_)) { | 1394 if (HasGeneratedMethods(descriptor_)) { |
1212 // The parsing constructor throws an InvalidProtocolBufferException via a | |
1213 // RuntimeException to aid in method pruning. We unwrap it here. | |
1214 printer->Print( | 1395 printer->Print( |
1215 " try {\n" | 1396 " return new $classname$(input, extensionRegistry);\n", |
1216 " return new $classname$(input, extensionRegistry);\n" | |
1217 " } catch (RuntimeException e) {\n" | |
1218 " if (e.getCause() instanceof\n" | |
1219 " com.google.protobuf.InvalidProtocolBufferException) {\n" | |
1220 " throw (com.google.protobuf.InvalidProtocolBufferException)\n" | |
1221 " e.getCause();\n" | |
1222 " }\n" | |
1223 " throw e;\n" | |
1224 " }\n", | |
1225 "classname", descriptor_->name()); | 1397 "classname", descriptor_->name()); |
1226 } else { | 1398 } else { |
1227 // When parsing constructor isn't generated, use builder to parse | 1399 // When parsing constructor isn't generated, use builder to parse messages. |
1228 // messages. Note, will fallback to use reflection based mergeFieldFrom() | 1400 // Note, will fallback to use reflection based mergeFieldFrom() in |
1229 // in AbstractMessage.Builder. | 1401 // AbstractMessage.Builder. |
1230 printer->Indent(); | 1402 printer->Indent(); |
1231 printer->Print( | 1403 printer->Print( |
1232 "Builder builder = newBuilder();\n" | 1404 "Builder builder = newBuilder();\n" |
1233 "try {\n" | 1405 "try {\n" |
1234 " builder.mergeFrom(input, extensionRegistry);\n" | 1406 " builder.mergeFrom(input, extensionRegistry);\n" |
1235 "} catch (com.google.protobuf.InvalidProtocolBufferException e) {\n" | 1407 "} catch (com.google.protobuf.InvalidProtocolBufferException e) {\n" |
1236 " throw e.setUnfinishedMessage(builder.buildPartial());\n" | 1408 " throw e.setUnfinishedMessage(builder.buildPartial());\n" |
1237 "} catch (java.io.IOException e) {\n" | 1409 "} catch (java.io.IOException e) {\n" |
1238 " throw new com.google.protobuf.InvalidProtocolBufferException(\n" | 1410 " throw new com.google.protobuf.InvalidProtocolBufferException(\n" |
1239 " e.getMessage()).setUnfinishedMessage(\n" | 1411 " e.getMessage()).setUnfinishedMessage(builder.buildPartial());\n" |
1240 " builder.buildPartial());\n" | |
1241 "}\n" | 1412 "}\n" |
1242 "return builder.buildPartial();\n"); | 1413 "return builder.buildPartial();\n"); |
1243 printer->Outdent(); | 1414 printer->Outdent(); |
1244 } | 1415 } |
1245 printer->Print( | 1416 printer->Print( |
1246 "}\n"); | 1417 "}\n"); |
1247 printer->Outdent(); | 1418 printer->Outdent(); |
1248 printer->Print( | 1419 printer->Print( |
1249 "};\n" | 1420 "};\n" |
1250 "\n"); | 1421 "\n"); |
1251 | 1422 |
1252 printer->Print( | 1423 printer->Print( |
1253 "@java.lang.Override\n" | 1424 "@java.lang.Override\n" |
1254 "public com.google.protobuf.Parser<$classname$> getParserForType() {\n" | 1425 "public com.google.protobuf.Parser<$classname$> getParserForType() {\n" |
1255 " return PARSER;\n" | 1426 " return PARSER;\n" |
1256 "}\n" | 1427 "}\n" |
1257 "\n", | 1428 "\n", |
1258 "classname", descriptor_->name()); | 1429 "classname", descriptor_->name()); |
1259 } | 1430 } |
1260 | 1431 |
1261 // =================================================================== | |
1262 void ImmutableMessageGenerator::GenerateInitializers(io::Printer* printer) { | |
1263 for (int i = 0; i < descriptor_->field_count(); i++) { | |
1264 if (!descriptor_->field(i)->containing_oneof()) { | |
1265 field_generators_.get(descriptor_->field(i)) | |
1266 .GenerateInitializationCode(printer); | |
1267 } | |
1268 } | |
1269 } | |
1270 | |
1271 | |
1272 } // namespace java | 1432 } // namespace java |
1273 } // namespace compiler | 1433 } // namespace compiler |
1274 } // namespace protobuf | 1434 } // namespace protobuf |
1275 } // namespace google | 1435 } // namespace google |
OLD | NEW |