LCOV - code coverage report
Current view: top level - third_party/protobuf/src/google/protobuf/compiler/java - java_message_builder.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 0 201 0.0 %
Date: 2015-10-10 Functions: 0 10 0.0 %

          Line data    Source code
       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: dweis@google.com (Daniel Weis)
      32             : //  Based on original Protocol Buffers design by
      33             : //  Sanjay Ghemawat, Jeff Dean, and others.
      34             : 
      35             : #include <google/protobuf/compiler/java/java_message_builder.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_name_resolver.h>
      53             : #include <google/protobuf/io/coded_stream.h>
      54             : #include <google/protobuf/io/printer.h>
      55             : #include <google/protobuf/descriptor.pb.h>
      56             : #include <google/protobuf/wire_format.h>
      57             : #include <google/protobuf/stubs/strutil.h>
      58             : #include <google/protobuf/stubs/substitute.h>
      59             : 
      60             : namespace google {
      61             : namespace protobuf {
      62             : namespace compiler {
      63             : namespace java {
      64             : 
      65             : namespace {
      66           0 : bool GenerateHasBits(const Descriptor* descriptor) {
      67           0 :   return SupportFieldPresence(descriptor->file()) ||
      68           0 :       HasRepeatedFields(descriptor);
      69             : }
      70             : 
      71           0 : string MapValueImmutableClassdName(const Descriptor* descriptor,
      72             :                                    ClassNameResolver* name_resolver) {
      73           0 :   const FieldDescriptor* value_field = descriptor->FindFieldByName("value");
      74           0 :   GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, value_field->type());
      75           0 :   return name_resolver->GetImmutableClassName(value_field->message_type());
      76             : }
      77             : }  // namespace
      78             : 
      79           0 : MessageBuilderGenerator::MessageBuilderGenerator(
      80           0 :     const Descriptor* descriptor, Context* context)
      81             :   : descriptor_(descriptor), context_(context),
      82           0 :     name_resolver_(context->GetNameResolver()),
      83           0 :     field_generators_(descriptor, context_) {
      84           0 :   GOOGLE_CHECK_NE(
      85           0 :       FileOptions::LITE_RUNTIME, descriptor->file()->options().optimize_for());
      86           0 : }
      87             : 
      88           0 : MessageBuilderGenerator::~MessageBuilderGenerator() {}
      89             : 
      90           0 : void MessageBuilderGenerator::
      91             : Generate(io::Printer* printer) {
      92           0 :   WriteMessageDocComment(printer, descriptor_);
      93           0 :   if (descriptor_->extension_range_count() > 0) {
      94             :     printer->Print(
      95             :       "public static final class Builder extends\n"
      96             :       "    com.google.protobuf.GeneratedMessage.ExtendableBuilder<\n"
      97             :       "      $classname$, Builder> implements\n"
      98             :       "    $extra_interfaces$\n"
      99             :       "    $classname$OrBuilder {\n",
     100             :       "classname", name_resolver_->GetImmutableClassName(descriptor_),
     101           0 :       "extra_interfaces", ExtraBuilderInterfaces(descriptor_));
     102             :   } else {
     103             :     printer->Print(
     104             :       "public static final class Builder extends\n"
     105             :       "    com.google.protobuf.GeneratedMessage.Builder<Builder> implements\n"
     106             :       "    $extra_interfaces$\n"
     107             :       "    $classname$OrBuilder {\n",
     108             :       "classname", name_resolver_->GetImmutableClassName(descriptor_),
     109           0 :       "extra_interfaces", ExtraBuilderInterfaces(descriptor_));
     110             :   }
     111           0 :   printer->Indent();
     112             : 
     113           0 :   GenerateDescriptorMethods(printer);
     114           0 :   GenerateCommonBuilderMethods(printer);
     115             : 
     116           0 :   if (HasGeneratedMethods(descriptor_)) {
     117           0 :     GenerateIsInitialized(printer);
     118           0 :     GenerateBuilderParsingMethods(printer);
     119             :   }
     120             : 
     121             :   // oneof
     122             :   map<string, string> vars;
     123           0 :   for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
     124           0 :     vars["oneof_name"] = context_->GetOneofGeneratorInfo(
     125           0 :         descriptor_->oneof_decl(i))->name;
     126           0 :     vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo(
     127           0 :         descriptor_->oneof_decl(i))->capitalized_name;
     128           0 :     vars["oneof_index"] = SimpleItoa(descriptor_->oneof_decl(i)->index());
     129             :     // oneofCase_ and oneof_
     130             :     printer->Print(vars,
     131             :       "private int $oneof_name$Case_ = 0;\n"
     132           0 :       "private java.lang.Object $oneof_name$_;\n");
     133             :     // oneofCase() and clearOneof()
     134             :     printer->Print(vars,
     135             :       "public $oneof_capitalized_name$Case\n"
     136             :       "    get$oneof_capitalized_name$Case() {\n"
     137             :       "  return $oneof_capitalized_name$Case.valueOf(\n"
     138             :       "      $oneof_name$Case_);\n"
     139             :       "}\n"
     140             :       "\n"
     141             :       "public Builder clear$oneof_capitalized_name$() {\n"
     142             :       "  $oneof_name$Case_ = 0;\n"
     143           0 :       "  $oneof_name$_ = null;\n");
     144           0 :     printer->Print("  onChanged();\n");
     145             :     printer->Print(
     146             :       "  return this;\n"
     147             :       "}\n"
     148           0 :       "\n");
     149             :   }
     150             : 
     151           0 :   if (GenerateHasBits(descriptor_)) {
     152             :     // Integers for bit fields.
     153             :     int totalBits = 0;
     154           0 :     for (int i = 0; i < descriptor_->field_count(); i++) {
     155           0 :       totalBits += field_generators_.get(descriptor_->field(i))
     156           0 :           .GetNumBitsForBuilder();
     157             :     }
     158           0 :     int totalInts = (totalBits + 31) / 32;
     159           0 :     for (int i = 0; i < totalInts; i++) {
     160             :       printer->Print("private int $bit_field_name$;\n",
     161           0 :         "bit_field_name", GetBitFieldName(i));
     162             :     }
     163             :   }
     164             : 
     165           0 :   for (int i = 0; i < descriptor_->field_count(); i++) {
     166           0 :     printer->Print("\n");
     167           0 :     field_generators_.get(descriptor_->field(i))
     168           0 :                      .GenerateBuilderMembers(printer);
     169             :   }
     170             : 
     171           0 :   if (!PreserveUnknownFields(descriptor_)) {
     172             :     printer->Print(
     173             :       "public final Builder setUnknownFields(\n"
     174             :       "    final com.google.protobuf.UnknownFieldSet unknownFields) {\n"
     175             :       "  return this;\n"
     176             :       "}\n"
     177             :       "\n"
     178             :       "public final Builder mergeUnknownFields(\n"
     179             :       "    final com.google.protobuf.UnknownFieldSet unknownFields) {\n"
     180             :       "  return this;\n"
     181             :       "}\n"
     182           0 :       "\n");
     183             :   }
     184             : 
     185             :   printer->Print(
     186             :     "\n"
     187             :     "// @@protoc_insertion_point(builder_scope:$full_name$)\n",
     188           0 :     "full_name", descriptor_->full_name());
     189             : 
     190           0 :   printer->Outdent();
     191           0 :   printer->Print("}\n");
     192           0 : }
     193             : 
     194             : // ===================================================================
     195             : 
     196           0 : void MessageBuilderGenerator::
     197             : GenerateDescriptorMethods(io::Printer* printer) {
     198           0 :   if (!descriptor_->options().no_standard_descriptor_accessor()) {
     199             :     printer->Print(
     200             :       "public static final com.google.protobuf.Descriptors.Descriptor\n"
     201             :       "    getDescriptor() {\n"
     202             :       "  return $fileclass$.internal_$identifier$_descriptor;\n"
     203             :       "}\n"
     204             :       "\n",
     205             :       "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()),
     206           0 :       "identifier", UniqueFileScopeIdentifier(descriptor_));
     207             :   }
     208             :   vector<const FieldDescriptor*> map_fields;
     209           0 :   for (int i = 0; i < descriptor_->field_count(); i++) {
     210           0 :     const FieldDescriptor* field = descriptor_->field(i);
     211           0 :     if (GetJavaType(field) == JAVATYPE_MESSAGE &&
     212           0 :         IsMapEntry(field->message_type())) {
     213           0 :       map_fields.push_back(field);
     214             :     }
     215             :   }
     216           0 :   if (!map_fields.empty()) {
     217             :     printer->Print(
     218             :       "@SuppressWarnings({\"rawtypes\"})\n"
     219             :       "protected com.google.protobuf.MapField internalGetMapField(\n"
     220             :       "    int number) {\n"
     221           0 :       "  switch (number) {\n");
     222           0 :     printer->Indent();
     223           0 :     printer->Indent();
     224           0 :     for (int i = 0; i < map_fields.size(); ++i) {
     225           0 :       const FieldDescriptor* field = map_fields[i];
     226           0 :       const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field);
     227             :       printer->Print(
     228             :         "case $number$:\n"
     229             :         "  return internalGet$capitalized_name$();\n",
     230             :         "number", SimpleItoa(field->number()),
     231           0 :         "capitalized_name", info->capitalized_name);
     232             :     }
     233             :     printer->Print(
     234             :         "default:\n"
     235             :         "  throw new RuntimeException(\n"
     236           0 :         "      \"Invalid map field number: \" + number);\n");
     237           0 :     printer->Outdent();
     238           0 :     printer->Outdent();
     239             :     printer->Print(
     240             :         "  }\n"
     241           0 :         "}\n");
     242             :     printer->Print(
     243             :       "@SuppressWarnings({\"rawtypes\"})\n"
     244             :       "protected com.google.protobuf.MapField internalGetMutableMapField(\n"
     245             :       "    int number) {\n"
     246           0 :       "  switch (number) {\n");
     247           0 :     printer->Indent();
     248           0 :     printer->Indent();
     249           0 :     for (int i = 0; i < map_fields.size(); ++i) {
     250           0 :       const FieldDescriptor* field = map_fields[i];
     251             :       const FieldGeneratorInfo* info =
     252           0 :           context_->GetFieldGeneratorInfo(field);
     253             :       printer->Print(
     254             :         "case $number$:\n"
     255             :         "  return internalGetMutable$capitalized_name$();\n",
     256             :         "number", SimpleItoa(field->number()),
     257           0 :         "capitalized_name", info->capitalized_name);
     258             :     }
     259             :     printer->Print(
     260             :         "default:\n"
     261             :         "  throw new RuntimeException(\n"
     262           0 :         "      \"Invalid map field number: \" + number);\n");
     263           0 :     printer->Outdent();
     264           0 :     printer->Outdent();
     265             :     printer->Print(
     266             :         "  }\n"
     267           0 :         "}\n");
     268             :   }
     269             :   printer->Print(
     270             :     "protected com.google.protobuf.GeneratedMessage.FieldAccessorTable\n"
     271             :     "    internalGetFieldAccessorTable() {\n"
     272             :     "  return $fileclass$.internal_$identifier$_fieldAccessorTable\n"
     273             :     "      .ensureFieldAccessorsInitialized(\n"
     274             :     "          $classname$.class, $classname$.Builder.class);\n"
     275             :     "}\n"
     276             :     "\n",
     277             :     "classname", name_resolver_->GetImmutableClassName(descriptor_),
     278             :     "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()),
     279           0 :     "identifier", UniqueFileScopeIdentifier(descriptor_));
     280           0 : }
     281             : 
     282             : // ===================================================================
     283             : 
     284           0 : void MessageBuilderGenerator::
     285             : GenerateCommonBuilderMethods(io::Printer* printer) {
     286             :   printer->Print(
     287             :       "// Construct using $classname$.newBuilder()\n"
     288             :       "private Builder() {\n"
     289             :       "  maybeForceBuilderInitialization();\n"
     290             :       "}\n"
     291             :       "\n",
     292           0 :       "classname", name_resolver_->GetImmutableClassName(descriptor_));
     293             : 
     294             :   printer->Print(
     295             :     "private Builder(\n"
     296             :     "    com.google.protobuf.GeneratedMessage.BuilderParent parent) {\n"
     297             :     "  super(parent);\n"
     298             :     "  maybeForceBuilderInitialization();\n"
     299             :     "}\n",
     300           0 :     "classname", name_resolver_->GetImmutableClassName(descriptor_));
     301             : 
     302             :   printer->Print(
     303             :     "private void maybeForceBuilderInitialization() {\n"
     304           0 :     "  if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {\n");
     305             : 
     306           0 :   printer->Indent();
     307           0 :   printer->Indent();
     308           0 :   for (int i = 0; i < descriptor_->field_count(); i++) {
     309           0 :     if (!descriptor_->field(i)->containing_oneof()) {
     310           0 :       field_generators_.get(descriptor_->field(i))
     311           0 :           .GenerateFieldBuilderInitializationCode(printer);
     312             :     }
     313             :   }
     314           0 :   printer->Outdent();
     315           0 :   printer->Outdent();
     316             : 
     317             :   printer->Print(
     318             :     "  }\n"
     319           0 :     "}\n");
     320             : 
     321             :   printer->Print(
     322             :     "public Builder clear() {\n"
     323           0 :     "  super.clear();\n");
     324             : 
     325           0 :   printer->Indent();
     326             : 
     327           0 :   for (int i = 0; i < descriptor_->field_count(); i++) {
     328           0 :     if (!descriptor_->field(i)->containing_oneof()) {
     329           0 :       field_generators_.get(descriptor_->field(i))
     330           0 :           .GenerateBuilderClearCode(printer);
     331             :     }
     332             :   }
     333             : 
     334           0 :   for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
     335             :     printer->Print(
     336             :       "$oneof_name$Case_ = 0;\n"
     337             :       "$oneof_name$_ = null;\n",
     338             :       "oneof_name", context_->GetOneofGeneratorInfo(
     339           0 :           descriptor_->oneof_decl(i))->name);
     340             :   }
     341             : 
     342           0 :   printer->Outdent();
     343             : 
     344             :   printer->Print(
     345             :     "  return this;\n"
     346             :     "}\n"
     347           0 :     "\n");
     348             : 
     349             :   printer->Print(
     350             :     "public com.google.protobuf.Descriptors.Descriptor\n"
     351             :     "    getDescriptorForType() {\n"
     352             :     "  return $fileclass$.internal_$identifier$_descriptor;\n"
     353             :     "}\n"
     354             :     "\n",
     355             :     "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()),
     356           0 :     "identifier", UniqueFileScopeIdentifier(descriptor_));
     357             : 
     358             :   // LITE runtime implements this in GeneratedMessageLite.
     359             :   printer->Print(
     360             :     "public $classname$ getDefaultInstanceForType() {\n"
     361             :     "  return $classname$.getDefaultInstance();\n"
     362             :     "}\n"
     363             :     "\n",
     364           0 :     "classname", name_resolver_->GetImmutableClassName(descriptor_));
     365             : 
     366             :   printer->Print(
     367             :     "public $classname$ build() {\n"
     368             :     "  $classname$ result = buildPartial();\n"
     369             :     "  if (!result.isInitialized()) {\n"
     370             :     "    throw newUninitializedMessageException(result);\n"
     371             :     "  }\n"
     372             :     "  return result;\n"
     373             :     "}\n"
     374             :     "\n",
     375           0 :     "classname", name_resolver_->GetImmutableClassName(descriptor_));
     376             : 
     377             :   printer->Print(
     378             :     "public $classname$ buildPartial() {\n"
     379             :     "  $classname$ result = new $classname$(this);\n",
     380           0 :     "classname", name_resolver_->GetImmutableClassName(descriptor_));
     381             : 
     382           0 :   printer->Indent();
     383             : 
     384           0 :   int totalBuilderBits = 0;
     385           0 :   int totalMessageBits = 0;
     386           0 :   for (int i = 0; i < descriptor_->field_count(); i++) {
     387             :     const ImmutableFieldGenerator& field =
     388           0 :         field_generators_.get(descriptor_->field(i));
     389           0 :     totalBuilderBits += field.GetNumBitsForBuilder();
     390           0 :     totalMessageBits += field.GetNumBitsForMessage();
     391             :   }
     392           0 :   int totalBuilderInts = (totalBuilderBits + 31) / 32;
     393           0 :   int totalMessageInts = (totalMessageBits + 31) / 32;
     394             : 
     395           0 :   if (GenerateHasBits(descriptor_)) {
     396             :     // Local vars for from and to bit fields to avoid accessing the builder and
     397             :     // message over and over for these fields. Seems to provide a slight
     398             :     // perforamance improvement in micro benchmark and this is also what proto1
     399             :     // code does.
     400           0 :     for (int i = 0; i < totalBuilderInts; i++) {
     401             :       printer->Print("int from_$bit_field_name$ = $bit_field_name$;\n",
     402           0 :         "bit_field_name", GetBitFieldName(i));
     403             :     }
     404           0 :     for (int i = 0; i < totalMessageInts; i++) {
     405             :       printer->Print("int to_$bit_field_name$ = 0;\n",
     406           0 :         "bit_field_name", GetBitFieldName(i));
     407             :     }
     408             :   }
     409             : 
     410             :   // Output generation code for each field.
     411           0 :   for (int i = 0; i < descriptor_->field_count(); i++) {
     412           0 :     field_generators_.get(descriptor_->field(i)).GenerateBuildingCode(printer);
     413             :   }
     414             : 
     415           0 :   if (GenerateHasBits(descriptor_)) {
     416             :     // Copy the bit field results to the generated message
     417           0 :     for (int i = 0; i < totalMessageInts; i++) {
     418             :       printer->Print("result.$bit_field_name$ = to_$bit_field_name$;\n",
     419           0 :         "bit_field_name", GetBitFieldName(i));
     420             :     }
     421             :   }
     422             : 
     423           0 :   for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
     424             :     printer->Print("result.$oneof_name$Case_ = $oneof_name$Case_;\n",
     425             :                    "oneof_name", context_->GetOneofGeneratorInfo(
     426           0 :                        descriptor_->oneof_decl(i))->name);
     427             :   }
     428             : 
     429           0 :   printer->Outdent();
     430             : 
     431             :   printer->Print(
     432           0 :     "  onBuilt();\n");
     433             : 
     434             :   printer->Print(
     435             :     "  return result;\n"
     436             :     "}\n"
     437             :     "\n",
     438           0 :     "classname", name_resolver_->GetImmutableClassName(descriptor_));
     439             : 
     440             :   // -----------------------------------------------------------------
     441             : 
     442           0 :   if (HasGeneratedMethods(descriptor_)) {
     443             :     printer->Print(
     444             :       "public Builder mergeFrom(com.google.protobuf.Message other) {\n"
     445             :       "  if (other instanceof $classname$) {\n"
     446             :       "    return mergeFrom(($classname$)other);\n"
     447             :       "  } else {\n"
     448             :       "    super.mergeFrom(other);\n"
     449             :       "    return this;\n"
     450             :       "  }\n"
     451             :       "}\n"
     452             :       "\n",
     453           0 :       "classname", name_resolver_->GetImmutableClassName(descriptor_));
     454             : 
     455             :     printer->Print(
     456             :       "public Builder mergeFrom($classname$ other) {\n"
     457             :       // Optimization:  If other is the default instance, we know none of its
     458             :       //   fields are set so we can skip the merge.
     459             :       "  if (other == $classname$.getDefaultInstance()) return this;\n",
     460           0 :       "classname", name_resolver_->GetImmutableClassName(descriptor_));
     461           0 :     printer->Indent();
     462             : 
     463           0 :     for (int i = 0; i < descriptor_->field_count(); i++) {
     464           0 :       if (!descriptor_->field(i)->containing_oneof()) {
     465             :         field_generators_.get(
     466           0 :             descriptor_->field(i)).GenerateMergingCode(printer);
     467             :       }
     468             :     }
     469             : 
     470             :     // Merge oneof fields.
     471           0 :     for (int i = 0; i < descriptor_->oneof_decl_count(); ++i) {
     472             :       printer->Print(
     473             :         "switch (other.get$oneof_capitalized_name$Case()) {\n",
     474             :         "oneof_capitalized_name",
     475             :         context_->GetOneofGeneratorInfo(
     476           0 :             descriptor_->oneof_decl(i))->capitalized_name);
     477           0 :       printer->Indent();
     478           0 :       for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) {
     479           0 :         const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j);
     480             :         printer->Print(
     481             :           "case $field_name$: {\n",
     482             :           "field_name",
     483           0 :           ToUpper(field->name()));
     484           0 :         printer->Indent();
     485           0 :         field_generators_.get(field).GenerateMergingCode(printer);
     486             :         printer->Print(
     487           0 :             "break;\n");
     488           0 :         printer->Outdent();
     489             :         printer->Print(
     490           0 :             "}\n");
     491             :       }
     492             :       printer->Print(
     493             :         "case $cap_oneof_name$_NOT_SET: {\n"
     494             :         "  break;\n"
     495             :         "}\n",
     496             :         "cap_oneof_name",
     497             :         ToUpper(context_->GetOneofGeneratorInfo(
     498           0 :             descriptor_->oneof_decl(i))->name));
     499           0 :       printer->Outdent();
     500             :       printer->Print(
     501           0 :           "}\n");
     502             :     }
     503             : 
     504           0 :     printer->Outdent();
     505             : 
     506             :     // if message type has extensions
     507           0 :     if (descriptor_->extension_range_count() > 0) {
     508             :       printer->Print(
     509           0 :         "  this.mergeExtensionFields(other);\n");
     510             :     }
     511             : 
     512           0 :     if (PreserveUnknownFields(descriptor_)) {
     513             :       printer->Print(
     514           0 :         "  this.mergeUnknownFields(other.unknownFields);\n");
     515             :     }
     516             : 
     517             :     printer->Print(
     518           0 :       "  onChanged();\n");
     519             : 
     520             :     printer->Print(
     521             :       "  return this;\n"
     522             :       "}\n"
     523           0 :       "\n");
     524             :   }
     525           0 : }
     526             : 
     527             : // ===================================================================
     528             : 
     529           0 : void MessageBuilderGenerator::
     530             : GenerateBuilderParsingMethods(io::Printer* printer) {
     531             :   printer->Print(
     532             :     "public Builder mergeFrom(\n"
     533             :     "    com.google.protobuf.CodedInputStream input,\n"
     534             :     "    com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
     535             :     "    throws java.io.IOException {\n"
     536             :     "  $classname$ parsedMessage = null;\n"
     537             :     "  try {\n"
     538             :     "    parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);\n"
     539             :     "  } catch (com.google.protobuf.InvalidProtocolBufferException e) {\n"
     540             :     "    parsedMessage = ($classname$) e.getUnfinishedMessage();\n"
     541             :     "    throw e;\n"
     542             :     "  } finally {\n"
     543             :     "    if (parsedMessage != null) {\n"
     544             :     "      mergeFrom(parsedMessage);\n"
     545             :     "    }\n"
     546             :     "  }\n"
     547             :     "  return this;\n"
     548             :     "}\n",
     549           0 :     "classname", name_resolver_->GetImmutableClassName(descriptor_));
     550           0 : }
     551             : 
     552             : // ===================================================================
     553             : 
     554           0 : void MessageBuilderGenerator::GenerateIsInitialized(
     555             :     io::Printer* printer) {
     556             :   printer->Print(
     557           0 :     "public final boolean isInitialized() {\n");
     558           0 :   printer->Indent();
     559             : 
     560             :   // Check that all required fields in this message are set.
     561             :   // TODO(kenton):  We can optimize this when we switch to putting all the
     562             :   //   "has" fields into a single bitfield.
     563           0 :   for (int i = 0; i < descriptor_->field_count(); i++) {
     564           0 :     const FieldDescriptor* field = descriptor_->field(i);
     565           0 :     const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field);
     566             : 
     567           0 :     if (field->is_required()) {
     568             :       printer->Print(
     569             :         "if (!has$name$()) {\n"
     570             :         "  return false;\n"
     571             :         "}\n",
     572           0 :         "name", info->capitalized_name);
     573             :     }
     574             :   }
     575             : 
     576             :   // Now check that all embedded messages are initialized.
     577           0 :   for (int i = 0; i < descriptor_->field_count(); i++) {
     578           0 :     const FieldDescriptor* field = descriptor_->field(i);
     579           0 :     const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field);
     580           0 :     if (GetJavaType(field) == JAVATYPE_MESSAGE &&
     581           0 :         HasRequiredFields(field->message_type())) {
     582           0 :       switch (field->label()) {
     583             :         case FieldDescriptor::LABEL_REQUIRED:
     584             :           printer->Print(
     585             :             "if (!get$name$().isInitialized()) {\n"
     586             :              "  return false;\n"
     587             :              "}\n",
     588             :             "type", name_resolver_->GetImmutableClassName(
     589             :                 field->message_type()),
     590           0 :             "name", info->capitalized_name);
     591           0 :           break;
     592             :         case FieldDescriptor::LABEL_OPTIONAL:
     593           0 :           if (!SupportFieldPresence(descriptor_->file()) &&
     594           0 :               field->containing_oneof() != NULL) {
     595           0 :             const OneofDescriptor* oneof = field->containing_oneof();
     596             :             const OneofGeneratorInfo* oneof_info =
     597           0 :                 context_->GetOneofGeneratorInfo(oneof);
     598             :             printer->Print(
     599             :               "if ($oneof_name$Case_ == $field_number$) {\n",
     600             :               "oneof_name", oneof_info->name,
     601           0 :               "field_number", SimpleItoa(field->number()));
     602             :           } else {
     603             :             printer->Print(
     604             :               "if (has$name$()) {\n",
     605           0 :               "name", info->capitalized_name);
     606             :           }
     607             :           printer->Print(
     608             :             "  if (!get$name$().isInitialized()) {\n"
     609             :             "    return false;\n"
     610             :             "  }\n"
     611             :             "}\n",
     612           0 :             "name", info->capitalized_name);
     613           0 :           break;
     614             :         case FieldDescriptor::LABEL_REPEATED:
     615           0 :           if (IsMapEntry(field->message_type())) {
     616             :             printer->Print(
     617             :               "for ($type$ item : get$name$().values()) {\n"
     618             :               "  if (!item.isInitialized()) {\n"
     619             :               "    return false;\n"
     620             :               "  }\n"
     621             :               "}\n",
     622             :               "type", MapValueImmutableClassdName(field->message_type(),
     623             :                                                   name_resolver_),
     624           0 :               "name", info->capitalized_name);
     625             :           } else {
     626             :             printer->Print(
     627             :               "for (int i = 0; i < get$name$Count(); i++) {\n"
     628             :               "  if (!get$name$(i).isInitialized()) {\n"
     629             :               "    return false;\n"
     630             :               "  }\n"
     631             :               "}\n",
     632             :               "type", name_resolver_->GetImmutableClassName(
     633             :                   field->message_type()),
     634           0 :               "name", info->capitalized_name);
     635             :           }
     636             :           break;
     637             :       }
     638             :     }
     639             :   }
     640             : 
     641           0 :   if (descriptor_->extension_range_count() > 0) {
     642             :     printer->Print(
     643             :       "if (!extensionsAreInitialized()) {\n"
     644             :       "  return false;\n"
     645           0 :       "}\n");
     646             :   }
     647             : 
     648           0 :   printer->Outdent();
     649             : 
     650             :   printer->Print(
     651             :     "  return true;\n"
     652             :     "}\n"
     653           0 :     "\n");
     654           0 : }
     655             : 
     656             : // ===================================================================
     657             : 
     658             : }  // namespace java
     659             : }  // namespace compiler
     660             : }  // namespace protobuf
     661             : }  // namespace google

Generated by: LCOV version 1.10