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