/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/compiler/java/java_message.cc

http://github.com/tomahawk-player/tomahawk · C++ · 1287 lines · 1022 code · 139 blank · 126 comment · 126 complexity · 66c1ad7ab600afc5545fff4e67637ce1 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <algorithm>
  34. #include <google/protobuf/stubs/hash.h>
  35. #include <google/protobuf/compiler/java/java_message.h>
  36. #include <google/protobuf/compiler/java/java_enum.h>
  37. #include <google/protobuf/compiler/java/java_extension.h>
  38. #include <google/protobuf/compiler/java/java_helpers.h>
  39. #include <google/protobuf/stubs/strutil.h>
  40. #include <google/protobuf/io/printer.h>
  41. #include <google/protobuf/io/coded_stream.h>
  42. #include <google/protobuf/wire_format.h>
  43. #include <google/protobuf/descriptor.pb.h>
  44. namespace google {
  45. namespace protobuf {
  46. namespace compiler {
  47. namespace java {
  48. using internal::WireFormat;
  49. using internal::WireFormatLite;
  50. namespace {
  51. void PrintFieldComment(io::Printer* printer, const FieldDescriptor* field) {
  52. // Print the field's proto-syntax definition as a comment. We don't want to
  53. // print group bodies so we cut off after the first line.
  54. string def = field->DebugString();
  55. printer->Print("// $def$\n",
  56. "def", def.substr(0, def.find_first_of('\n')));
  57. }
  58. struct FieldOrderingByNumber {
  59. inline bool operator()(const FieldDescriptor* a,
  60. const FieldDescriptor* b) const {
  61. return a->number() < b->number();
  62. }
  63. };
  64. struct ExtensionRangeOrdering {
  65. bool operator()(const Descriptor::ExtensionRange* a,
  66. const Descriptor::ExtensionRange* b) const {
  67. return a->start < b->start;
  68. }
  69. };
  70. // Sort the fields of the given Descriptor by number into a new[]'d array
  71. // and return it.
  72. const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor) {
  73. const FieldDescriptor** fields =
  74. new const FieldDescriptor*[descriptor->field_count()];
  75. for (int i = 0; i < descriptor->field_count(); i++) {
  76. fields[i] = descriptor->field(i);
  77. }
  78. sort(fields, fields + descriptor->field_count(),
  79. FieldOrderingByNumber());
  80. return fields;
  81. }
  82. // Get an identifier that uniquely identifies this type within the file.
  83. // This is used to declare static variables related to this type at the
  84. // outermost file scope.
  85. string UniqueFileScopeIdentifier(const Descriptor* descriptor) {
  86. return "static_" + StringReplace(descriptor->full_name(), ".", "_", true);
  87. }
  88. // Returns true if the message type has any required fields. If it doesn't,
  89. // we can optimize out calls to its isInitialized() method.
  90. //
  91. // already_seen is used to avoid checking the same type multiple times
  92. // (and also to protect against recursion).
  93. static bool HasRequiredFields(
  94. const Descriptor* type,
  95. hash_set<const Descriptor*>* already_seen) {
  96. if (already_seen->count(type) > 0) {
  97. // The type is already in cache. This means that either:
  98. // a. The type has no required fields.
  99. // b. We are in the midst of checking if the type has required fields,
  100. // somewhere up the stack. In this case, we know that if the type
  101. // has any required fields, they'll be found when we return to it,
  102. // and the whole call to HasRequiredFields() will return true.
  103. // Therefore, we don't have to check if this type has required fields
  104. // here.
  105. return false;
  106. }
  107. already_seen->insert(type);
  108. // If the type has extensions, an extension with message type could contain
  109. // required fields, so we have to be conservative and assume such an
  110. // extension exists.
  111. if (type->extension_range_count() > 0) return true;
  112. for (int i = 0; i < type->field_count(); i++) {
  113. const FieldDescriptor* field = type->field(i);
  114. if (field->is_required()) {
  115. return true;
  116. }
  117. if (GetJavaType(field) == JAVATYPE_MESSAGE) {
  118. if (HasRequiredFields(field->message_type(), already_seen)) {
  119. return true;
  120. }
  121. }
  122. }
  123. return false;
  124. }
  125. static bool HasRequiredFields(const Descriptor* type) {
  126. hash_set<const Descriptor*> already_seen;
  127. return HasRequiredFields(type, &already_seen);
  128. }
  129. } // namespace
  130. // ===================================================================
  131. MessageGenerator::MessageGenerator(const Descriptor* descriptor)
  132. : descriptor_(descriptor),
  133. field_generators_(descriptor) {
  134. }
  135. MessageGenerator::~MessageGenerator() {}
  136. void MessageGenerator::GenerateStaticVariables(io::Printer* printer) {
  137. if (HasDescriptorMethods(descriptor_)) {
  138. // Because descriptor.proto (com.google.protobuf.DescriptorProtos) is
  139. // used in the construction of descriptors, we have a tricky bootstrapping
  140. // problem. To help control static initialization order, we make sure all
  141. // descriptors and other static data that depends on them are members of
  142. // the outermost class in the file. This way, they will be initialized in
  143. // a deterministic order.
  144. map<string, string> vars;
  145. vars["identifier"] = UniqueFileScopeIdentifier(descriptor_);
  146. vars["index"] = SimpleItoa(descriptor_->index());
  147. vars["classname"] = ClassName(descriptor_);
  148. if (descriptor_->containing_type() != NULL) {
  149. vars["parent"] = UniqueFileScopeIdentifier(
  150. descriptor_->containing_type());
  151. }
  152. if (descriptor_->file()->options().java_multiple_files()) {
  153. // We can only make these package-private since the classes that use them
  154. // are in separate files.
  155. vars["private"] = "";
  156. } else {
  157. vars["private"] = "private ";
  158. }
  159. // The descriptor for this type.
  160. printer->Print(vars,
  161. "$private$static com.google.protobuf.Descriptors.Descriptor\n"
  162. " internal_$identifier$_descriptor;\n");
  163. // And the FieldAccessorTable.
  164. printer->Print(vars,
  165. "$private$static\n"
  166. " com.google.protobuf.GeneratedMessage.FieldAccessorTable\n"
  167. " internal_$identifier$_fieldAccessorTable;\n");
  168. }
  169. // Generate static members for all nested types.
  170. for (int i = 0; i < descriptor_->nested_type_count(); i++) {
  171. // TODO(kenton): Reuse MessageGenerator objects?
  172. MessageGenerator(descriptor_->nested_type(i))
  173. .GenerateStaticVariables(printer);
  174. }
  175. }
  176. void MessageGenerator::GenerateStaticVariableInitializers(
  177. io::Printer* printer) {
  178. if (HasDescriptorMethods(descriptor_)) {
  179. map<string, string> vars;
  180. vars["identifier"] = UniqueFileScopeIdentifier(descriptor_);
  181. vars["index"] = SimpleItoa(descriptor_->index());
  182. vars["classname"] = ClassName(descriptor_);
  183. if (descriptor_->containing_type() != NULL) {
  184. vars["parent"] = UniqueFileScopeIdentifier(
  185. descriptor_->containing_type());
  186. }
  187. // The descriptor for this type.
  188. if (descriptor_->containing_type() == NULL) {
  189. printer->Print(vars,
  190. "internal_$identifier$_descriptor =\n"
  191. " getDescriptor().getMessageTypes().get($index$);\n");
  192. } else {
  193. printer->Print(vars,
  194. "internal_$identifier$_descriptor =\n"
  195. " internal_$parent$_descriptor.getNestedTypes().get($index$);\n");
  196. }
  197. // And the FieldAccessorTable.
  198. printer->Print(vars,
  199. "internal_$identifier$_fieldAccessorTable = new\n"
  200. " com.google.protobuf.GeneratedMessage.FieldAccessorTable(\n"
  201. " internal_$identifier$_descriptor,\n"
  202. " new java.lang.String[] { ");
  203. for (int i = 0; i < descriptor_->field_count(); i++) {
  204. printer->Print(
  205. "\"$field_name$\", ",
  206. "field_name",
  207. UnderscoresToCapitalizedCamelCase(descriptor_->field(i)));
  208. }
  209. printer->Print("},\n"
  210. " $classname$.class,\n"
  211. " $classname$.Builder.class);\n",
  212. "classname", ClassName(descriptor_));
  213. }
  214. // Generate static member initializers for all nested types.
  215. for (int i = 0; i < descriptor_->nested_type_count(); i++) {
  216. // TODO(kenton): Reuse MessageGenerator objects?
  217. MessageGenerator(descriptor_->nested_type(i))
  218. .GenerateStaticVariableInitializers(printer);
  219. }
  220. }
  221. // ===================================================================
  222. void MessageGenerator::GenerateInterface(io::Printer* printer) {
  223. if (descriptor_->extension_range_count() > 0) {
  224. if (HasDescriptorMethods(descriptor_)) {
  225. printer->Print(
  226. "public interface $classname$OrBuilder extends\n"
  227. " com.google.protobuf.GeneratedMessage.\n"
  228. " ExtendableMessageOrBuilder<$classname$> {\n",
  229. "classname", descriptor_->name());
  230. } else {
  231. printer->Print(
  232. "public interface $classname$OrBuilder extends \n"
  233. " com.google.protobuf.GeneratedMessageLite.\n"
  234. " ExtendableMessageOrBuilder<$classname$> {\n",
  235. "classname", descriptor_->name());
  236. }
  237. } else {
  238. if (HasDescriptorMethods(descriptor_)) {
  239. printer->Print(
  240. "public interface $classname$OrBuilder\n"
  241. " extends com.google.protobuf.MessageOrBuilder {\n",
  242. "classname", descriptor_->name());
  243. } else {
  244. printer->Print(
  245. "public interface $classname$OrBuilder\n"
  246. " extends com.google.protobuf.MessageLiteOrBuilder {\n",
  247. "classname", descriptor_->name());
  248. }
  249. }
  250. printer->Indent();
  251. for (int i = 0; i < descriptor_->field_count(); i++) {
  252. printer->Print("\n");
  253. PrintFieldComment(printer, descriptor_->field(i));
  254. field_generators_.get(descriptor_->field(i))
  255. .GenerateInterfaceMembers(printer);
  256. }
  257. printer->Outdent();
  258. printer->Print("}\n");
  259. }
  260. // ===================================================================
  261. void MessageGenerator::Generate(io::Printer* printer) {
  262. bool is_own_file =
  263. descriptor_->containing_type() == NULL &&
  264. descriptor_->file()->options().java_multiple_files();
  265. if (descriptor_->extension_range_count() > 0) {
  266. if (HasDescriptorMethods(descriptor_)) {
  267. printer->Print(
  268. "public $static$ final class $classname$ extends\n"
  269. " com.google.protobuf.GeneratedMessage.ExtendableMessage<\n"
  270. " $classname$> implements $classname$OrBuilder {\n",
  271. "static", is_own_file ? "" : "static",
  272. "classname", descriptor_->name());
  273. } else {
  274. printer->Print(
  275. "public $static$ final class $classname$ extends\n"
  276. " com.google.protobuf.GeneratedMessageLite.ExtendableMessage<\n"
  277. " $classname$> implements $classname$OrBuilder {\n",
  278. "static", is_own_file ? "" : "static",
  279. "classname", descriptor_->name());
  280. }
  281. } else {
  282. if (HasDescriptorMethods(descriptor_)) {
  283. printer->Print(
  284. "public $static$ final class $classname$ extends\n"
  285. " com.google.protobuf.GeneratedMessage\n"
  286. " implements $classname$OrBuilder {\n",
  287. "static", is_own_file ? "" : "static",
  288. "classname", descriptor_->name());
  289. } else {
  290. printer->Print(
  291. "public $static$ final class $classname$ extends\n"
  292. " com.google.protobuf.GeneratedMessageLite\n"
  293. " implements $classname$OrBuilder {\n",
  294. "static", is_own_file ? "" : "static",
  295. "classname", descriptor_->name());
  296. }
  297. }
  298. printer->Indent();
  299. printer->Print(
  300. "// Use $classname$.newBuilder() to construct.\n"
  301. "private $classname$(Builder builder) {\n"
  302. " super(builder);\n"
  303. "}\n"
  304. // Used when constructing the default instance, which cannot be initialized
  305. // immediately because it may cyclically refer to other default instances.
  306. "private $classname$(boolean noInit) {}\n"
  307. "\n"
  308. "private static final $classname$ defaultInstance;\n"
  309. "public static $classname$ getDefaultInstance() {\n"
  310. " return defaultInstance;\n"
  311. "}\n"
  312. "\n"
  313. "public $classname$ getDefaultInstanceForType() {\n"
  314. " return defaultInstance;\n"
  315. "}\n"
  316. "\n",
  317. "classname", descriptor_->name());
  318. GenerateDescriptorMethods(printer);
  319. // Nested types
  320. for (int i = 0; i < descriptor_->enum_type_count(); i++) {
  321. EnumGenerator(descriptor_->enum_type(i)).Generate(printer);
  322. }
  323. for (int i = 0; i < descriptor_->nested_type_count(); i++) {
  324. MessageGenerator messageGenerator(descriptor_->nested_type(i));
  325. messageGenerator.GenerateInterface(printer);
  326. messageGenerator.Generate(printer);
  327. }
  328. // Integers for bit fields.
  329. int totalBits = 0;
  330. for (int i = 0; i < descriptor_->field_count(); i++) {
  331. totalBits += field_generators_.get(descriptor_->field(i))
  332. .GetNumBitsForMessage();
  333. }
  334. int totalInts = (totalBits + 31) / 32;
  335. for (int i = 0; i < totalInts; i++) {
  336. printer->Print("private int $bit_field_name$;\n",
  337. "bit_field_name", GetBitFieldName(i));
  338. }
  339. // Fields
  340. for (int i = 0; i < descriptor_->field_count(); i++) {
  341. PrintFieldComment(printer, descriptor_->field(i));
  342. printer->Print("public static final int $constant_name$ = $number$;\n",
  343. "constant_name", FieldConstantName(descriptor_->field(i)),
  344. "number", SimpleItoa(descriptor_->field(i)->number()));
  345. field_generators_.get(descriptor_->field(i)).GenerateMembers(printer);
  346. printer->Print("\n");
  347. }
  348. // Called by the constructor, except in the case of the default instance,
  349. // in which case this is called by static init code later on.
  350. printer->Print("private void initFields() {\n");
  351. printer->Indent();
  352. for (int i = 0; i < descriptor_->field_count(); i++) {
  353. field_generators_.get(descriptor_->field(i))
  354. .GenerateInitializationCode(printer);
  355. }
  356. printer->Outdent();
  357. printer->Print("}\n");
  358. if (HasGeneratedMethods(descriptor_)) {
  359. GenerateIsInitialized(printer, MEMOIZE);
  360. GenerateMessageSerializationMethods(printer);
  361. }
  362. if (HasEqualsAndHashCode(descriptor_)) {
  363. GenerateEqualsAndHashCode(printer);
  364. }
  365. GenerateParseFromMethods(printer);
  366. GenerateBuilder(printer);
  367. // Carefully initialize the default instance in such a way that it doesn't
  368. // conflict with other initialization.
  369. printer->Print(
  370. "\n"
  371. "static {\n"
  372. " defaultInstance = new $classname$(true);\n"
  373. " defaultInstance.initFields();\n"
  374. "}\n"
  375. "\n"
  376. "// @@protoc_insertion_point(class_scope:$full_name$)\n",
  377. "classname", descriptor_->name(),
  378. "full_name", descriptor_->full_name());
  379. // Extensions must be declared after the defaultInstance is initialized
  380. // because the defaultInstance is used by the extension to lazily retrieve
  381. // the outer class's FileDescriptor.
  382. for (int i = 0; i < descriptor_->extension_count(); i++) {
  383. ExtensionGenerator(descriptor_->extension(i)).Generate(printer);
  384. }
  385. printer->Outdent();
  386. printer->Print("}\n\n");
  387. }
  388. // ===================================================================
  389. void MessageGenerator::
  390. GenerateMessageSerializationMethods(io::Printer* printer) {
  391. scoped_array<const FieldDescriptor*> sorted_fields(
  392. SortFieldsByNumber(descriptor_));
  393. vector<const Descriptor::ExtensionRange*> sorted_extensions;
  394. for (int i = 0; i < descriptor_->extension_range_count(); ++i) {
  395. sorted_extensions.push_back(descriptor_->extension_range(i));
  396. }
  397. sort(sorted_extensions.begin(), sorted_extensions.end(),
  398. ExtensionRangeOrdering());
  399. printer->Print(
  400. "public void writeTo(com.google.protobuf.CodedOutputStream output)\n"
  401. " throws java.io.IOException {\n");
  402. printer->Indent();
  403. // writeTo(CodedOutputStream output) might be invoked without
  404. // getSerializedSize() ever being called, but we need the memoized
  405. // sizes in case this message has packed fields. Rather than emit checks for
  406. // each packed field, just call getSerializedSize() up front for all messages.
  407. // In most cases, getSerializedSize() will have already been called anyway by
  408. // one of the wrapper writeTo() methods, making this call cheap.
  409. printer->Print(
  410. "getSerializedSize();\n");
  411. if (descriptor_->extension_range_count() > 0) {
  412. if (descriptor_->options().message_set_wire_format()) {
  413. printer->Print(
  414. "com.google.protobuf.GeneratedMessage$lite$\n"
  415. " .ExtendableMessage<$classname$>.ExtensionWriter extensionWriter =\n"
  416. " newMessageSetExtensionWriter();\n",
  417. "lite", HasDescriptorMethods(descriptor_) ? "" : "Lite",
  418. "classname", ClassName(descriptor_));
  419. } else {
  420. printer->Print(
  421. "com.google.protobuf.GeneratedMessage$lite$\n"
  422. " .ExtendableMessage<$classname$>.ExtensionWriter extensionWriter =\n"
  423. " newExtensionWriter();\n",
  424. "lite", HasDescriptorMethods(descriptor_) ? "" : "Lite",
  425. "classname", ClassName(descriptor_));
  426. }
  427. }
  428. // Merge the fields and the extension ranges, both sorted by field number.
  429. for (int i = 0, j = 0;
  430. i < descriptor_->field_count() || j < sorted_extensions.size();
  431. ) {
  432. if (i == descriptor_->field_count()) {
  433. GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]);
  434. } else if (j == sorted_extensions.size()) {
  435. GenerateSerializeOneField(printer, sorted_fields[i++]);
  436. } else if (sorted_fields[i]->number() < sorted_extensions[j]->start) {
  437. GenerateSerializeOneField(printer, sorted_fields[i++]);
  438. } else {
  439. GenerateSerializeOneExtensionRange(printer, sorted_extensions[j++]);
  440. }
  441. }
  442. if (HasUnknownFields(descriptor_)) {
  443. if (descriptor_->options().message_set_wire_format()) {
  444. printer->Print(
  445. "getUnknownFields().writeAsMessageSetTo(output);\n");
  446. } else {
  447. printer->Print(
  448. "getUnknownFields().writeTo(output);\n");
  449. }
  450. }
  451. printer->Outdent();
  452. printer->Print(
  453. "}\n"
  454. "\n"
  455. "private int memoizedSerializedSize = -1;\n"
  456. "public int getSerializedSize() {\n"
  457. " int size = memoizedSerializedSize;\n"
  458. " if (size != -1) return size;\n"
  459. "\n"
  460. " size = 0;\n");
  461. printer->Indent();
  462. for (int i = 0; i < descriptor_->field_count(); i++) {
  463. field_generators_.get(sorted_fields[i]).GenerateSerializedSizeCode(printer);
  464. }
  465. if (descriptor_->extension_range_count() > 0) {
  466. if (descriptor_->options().message_set_wire_format()) {
  467. printer->Print(
  468. "size += extensionsSerializedSizeAsMessageSet();\n");
  469. } else {
  470. printer->Print(
  471. "size += extensionsSerializedSize();\n");
  472. }
  473. }
  474. if (HasUnknownFields(descriptor_)) {
  475. if (descriptor_->options().message_set_wire_format()) {
  476. printer->Print(
  477. "size += getUnknownFields().getSerializedSizeAsMessageSet();\n");
  478. } else {
  479. printer->Print(
  480. "size += getUnknownFields().getSerializedSize();\n");
  481. }
  482. }
  483. printer->Outdent();
  484. printer->Print(
  485. " memoizedSerializedSize = size;\n"
  486. " return size;\n"
  487. "}\n"
  488. "\n");
  489. printer->Print(
  490. "private static final long serialVersionUID = 0L;\n"
  491. "@java.lang.Override\n"
  492. "protected java.lang.Object writeReplace()\n"
  493. " throws java.io.ObjectStreamException {\n"
  494. " return super.writeReplace();\n"
  495. "}\n"
  496. "\n");
  497. }
  498. void MessageGenerator::
  499. GenerateParseFromMethods(io::Printer* printer) {
  500. // Note: These are separate from GenerateMessageSerializationMethods()
  501. // because they need to be generated even for messages that are optimized
  502. // for code size.
  503. printer->Print(
  504. "public static $classname$ parseFrom(\n"
  505. " com.google.protobuf.ByteString data)\n"
  506. " throws com.google.protobuf.InvalidProtocolBufferException {\n"
  507. " return newBuilder().mergeFrom(data).buildParsed();\n"
  508. "}\n"
  509. "public static $classname$ parseFrom(\n"
  510. " com.google.protobuf.ByteString data,\n"
  511. " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
  512. " throws com.google.protobuf.InvalidProtocolBufferException {\n"
  513. " return newBuilder().mergeFrom(data, extensionRegistry)\n"
  514. " .buildParsed();\n"
  515. "}\n"
  516. "public static $classname$ parseFrom(byte[] data)\n"
  517. " throws com.google.protobuf.InvalidProtocolBufferException {\n"
  518. " return newBuilder().mergeFrom(data).buildParsed();\n"
  519. "}\n"
  520. "public static $classname$ parseFrom(\n"
  521. " byte[] data,\n"
  522. " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
  523. " throws com.google.protobuf.InvalidProtocolBufferException {\n"
  524. " return newBuilder().mergeFrom(data, extensionRegistry)\n"
  525. " .buildParsed();\n"
  526. "}\n"
  527. "public static $classname$ parseFrom(java.io.InputStream input)\n"
  528. " throws java.io.IOException {\n"
  529. " return newBuilder().mergeFrom(input).buildParsed();\n"
  530. "}\n"
  531. "public static $classname$ parseFrom(\n"
  532. " java.io.InputStream input,\n"
  533. " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
  534. " throws java.io.IOException {\n"
  535. " return newBuilder().mergeFrom(input, extensionRegistry)\n"
  536. " .buildParsed();\n"
  537. "}\n"
  538. "public static $classname$ parseDelimitedFrom(java.io.InputStream input)\n"
  539. " throws java.io.IOException {\n"
  540. " Builder builder = newBuilder();\n"
  541. " if (builder.mergeDelimitedFrom(input)) {\n"
  542. " return builder.buildParsed();\n"
  543. " } else {\n"
  544. " return null;\n"
  545. " }\n"
  546. "}\n"
  547. "public static $classname$ parseDelimitedFrom(\n"
  548. " java.io.InputStream input,\n"
  549. " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
  550. " throws java.io.IOException {\n"
  551. " Builder builder = newBuilder();\n"
  552. " if (builder.mergeDelimitedFrom(input, extensionRegistry)) {\n"
  553. " return builder.buildParsed();\n"
  554. " } else {\n"
  555. " return null;\n"
  556. " }\n"
  557. "}\n"
  558. "public static $classname$ parseFrom(\n"
  559. " com.google.protobuf.CodedInputStream input)\n"
  560. " throws java.io.IOException {\n"
  561. " return newBuilder().mergeFrom(input).buildParsed();\n"
  562. "}\n"
  563. "public static $classname$ parseFrom(\n"
  564. " com.google.protobuf.CodedInputStream input,\n"
  565. " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
  566. " throws java.io.IOException {\n"
  567. " return newBuilder().mergeFrom(input, extensionRegistry)\n"
  568. " .buildParsed();\n"
  569. "}\n"
  570. "\n",
  571. "classname", ClassName(descriptor_));
  572. }
  573. void MessageGenerator::GenerateSerializeOneField(
  574. io::Printer* printer, const FieldDescriptor* field) {
  575. field_generators_.get(field).GenerateSerializationCode(printer);
  576. }
  577. void MessageGenerator::GenerateSerializeOneExtensionRange(
  578. io::Printer* printer, const Descriptor::ExtensionRange* range) {
  579. printer->Print(
  580. "extensionWriter.writeUntil($end$, output);\n",
  581. "end", SimpleItoa(range->end));
  582. }
  583. // ===================================================================
  584. void MessageGenerator::GenerateBuilder(io::Printer* printer) {
  585. printer->Print(
  586. "public static Builder newBuilder() { return Builder.create(); }\n"
  587. "public Builder newBuilderForType() { return newBuilder(); }\n"
  588. "public static Builder newBuilder($classname$ prototype) {\n"
  589. " return newBuilder().mergeFrom(prototype);\n"
  590. "}\n"
  591. "public Builder toBuilder() { return newBuilder(this); }\n"
  592. "\n",
  593. "classname", ClassName(descriptor_));
  594. if (HasNestedBuilders(descriptor_)) {
  595. printer->Print(
  596. "@java.lang.Override\n"
  597. "protected Builder newBuilderForType(\n"
  598. " com.google.protobuf.GeneratedMessage.BuilderParent parent) {\n"
  599. " Builder builder = new Builder(parent);\n"
  600. " return builder;\n"
  601. "}\n");
  602. }
  603. if (descriptor_->extension_range_count() > 0) {
  604. if (HasDescriptorMethods(descriptor_)) {
  605. printer->Print(
  606. "public static final class Builder extends\n"
  607. " com.google.protobuf.GeneratedMessage.ExtendableBuilder<\n"
  608. " $classname$, Builder> implements $classname$OrBuilder {\n",
  609. "classname", ClassName(descriptor_));
  610. } else {
  611. printer->Print(
  612. "public static final class Builder extends\n"
  613. " com.google.protobuf.GeneratedMessageLite.ExtendableBuilder<\n"
  614. " $classname$, Builder> implements $classname$OrBuilder {\n",
  615. "classname", ClassName(descriptor_));
  616. }
  617. } else {
  618. if (HasDescriptorMethods(descriptor_)) {
  619. printer->Print(
  620. "public static final class Builder extends\n"
  621. " com.google.protobuf.GeneratedMessage.Builder<Builder>\n"
  622. " implements $classname$OrBuilder {\n",
  623. "classname", ClassName(descriptor_));
  624. } else {
  625. printer->Print(
  626. "public static final class Builder extends\n"
  627. " com.google.protobuf.GeneratedMessageLite.Builder<\n"
  628. " $classname$, Builder>\n"
  629. " implements $classname$OrBuilder {\n",
  630. "classname", ClassName(descriptor_));
  631. }
  632. }
  633. printer->Indent();
  634. GenerateDescriptorMethods(printer);
  635. GenerateCommonBuilderMethods(printer);
  636. if (HasGeneratedMethods(descriptor_)) {
  637. GenerateIsInitialized(printer, DONT_MEMOIZE);
  638. GenerateBuilderParsingMethods(printer);
  639. }
  640. // Integers for bit fields.
  641. int totalBits = 0;
  642. for (int i = 0; i < descriptor_->field_count(); i++) {
  643. totalBits += field_generators_.get(descriptor_->field(i))
  644. .GetNumBitsForBuilder();
  645. }
  646. int totalInts = (totalBits + 31) / 32;
  647. for (int i = 0; i < totalInts; i++) {
  648. printer->Print("private int $bit_field_name$;\n",
  649. "bit_field_name", GetBitFieldName(i));
  650. }
  651. for (int i = 0; i < descriptor_->field_count(); i++) {
  652. printer->Print("\n");
  653. PrintFieldComment(printer, descriptor_->field(i));
  654. field_generators_.get(descriptor_->field(i))
  655. .GenerateBuilderMembers(printer);
  656. }
  657. printer->Print(
  658. "\n"
  659. "// @@protoc_insertion_point(builder_scope:$full_name$)\n",
  660. "full_name", descriptor_->full_name());
  661. printer->Outdent();
  662. printer->Print("}\n");
  663. }
  664. void MessageGenerator::GenerateDescriptorMethods(io::Printer* printer) {
  665. if (HasDescriptorMethods(descriptor_)) {
  666. printer->Print(
  667. "public static final com.google.protobuf.Descriptors.Descriptor\n"
  668. " getDescriptor() {\n"
  669. " return $fileclass$.internal_$identifier$_descriptor;\n"
  670. "}\n"
  671. "\n"
  672. "protected com.google.protobuf.GeneratedMessage.FieldAccessorTable\n"
  673. " internalGetFieldAccessorTable() {\n"
  674. " return $fileclass$.internal_$identifier$_fieldAccessorTable;\n"
  675. "}\n"
  676. "\n",
  677. "fileclass", ClassName(descriptor_->file()),
  678. "identifier", UniqueFileScopeIdentifier(descriptor_));
  679. }
  680. }
  681. // ===================================================================
  682. void MessageGenerator::GenerateCommonBuilderMethods(io::Printer* printer) {
  683. printer->Print(
  684. "// Construct using $classname$.newBuilder()\n"
  685. "private Builder() {\n"
  686. " maybeForceBuilderInitialization();\n"
  687. "}\n"
  688. "\n",
  689. "classname", ClassName(descriptor_));
  690. if (HasDescriptorMethods(descriptor_)) {
  691. printer->Print(
  692. "private Builder(BuilderParent parent) {\n"
  693. " super(parent);\n"
  694. " maybeForceBuilderInitialization();\n"
  695. "}\n",
  696. "classname", ClassName(descriptor_));
  697. }
  698. if (HasNestedBuilders(descriptor_)) {
  699. printer->Print(
  700. "private void maybeForceBuilderInitialization() {\n"
  701. " if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {\n");
  702. printer->Indent();
  703. printer->Indent();
  704. for (int i = 0; i < descriptor_->field_count(); i++) {
  705. field_generators_.get(descriptor_->field(i))
  706. .GenerateFieldBuilderInitializationCode(printer);
  707. }
  708. printer->Outdent();
  709. printer->Outdent();
  710. printer->Print(
  711. " }\n"
  712. "}\n");
  713. } else {
  714. printer->Print(
  715. "private void maybeForceBuilderInitialization() {\n"
  716. "}\n");
  717. }
  718. printer->Print(
  719. "private static Builder create() {\n"
  720. " return new Builder();\n"
  721. "}\n"
  722. "\n"
  723. "public Builder clear() {\n"
  724. " super.clear();\n",
  725. "classname", ClassName(descriptor_));
  726. printer->Indent();
  727. for (int i = 0; i < descriptor_->field_count(); i++) {
  728. field_generators_.get(descriptor_->field(i))
  729. .GenerateBuilderClearCode(printer);
  730. }
  731. printer->Outdent();
  732. printer->Print(
  733. " return this;\n"
  734. "}\n"
  735. "\n"
  736. "public Builder clone() {\n"
  737. " return create().mergeFrom(buildPartial());\n"
  738. "}\n"
  739. "\n",
  740. "classname", ClassName(descriptor_));
  741. if (HasDescriptorMethods(descriptor_)) {
  742. printer->Print(
  743. "public com.google.protobuf.Descriptors.Descriptor\n"
  744. " getDescriptorForType() {\n"
  745. " return $classname$.getDescriptor();\n"
  746. "}\n"
  747. "\n",
  748. "classname", ClassName(descriptor_));
  749. }
  750. printer->Print(
  751. "public $classname$ getDefaultInstanceForType() {\n"
  752. " return $classname$.getDefaultInstance();\n"
  753. "}\n"
  754. "\n",
  755. "classname", ClassName(descriptor_));
  756. // -----------------------------------------------------------------
  757. printer->Print(
  758. "public $classname$ build() {\n"
  759. " $classname$ result = buildPartial();\n"
  760. " if (!result.isInitialized()) {\n"
  761. " throw newUninitializedMessageException(result);\n"
  762. " }\n"
  763. " return result;\n"
  764. "}\n"
  765. "\n"
  766. "private $classname$ buildParsed()\n"
  767. " throws com.google.protobuf.InvalidProtocolBufferException {\n"
  768. " $classname$ result = buildPartial();\n"
  769. " if (!result.isInitialized()) {\n"
  770. " throw newUninitializedMessageException(\n"
  771. " result).asInvalidProtocolBufferException();\n"
  772. " }\n"
  773. " return result;\n"
  774. "}\n"
  775. "\n"
  776. "public $classname$ buildPartial() {\n"
  777. " $classname$ result = new $classname$(this);\n",
  778. "classname", ClassName(descriptor_));
  779. printer->Indent();
  780. // Local vars for from and to bit fields to avoid accessing the builder and
  781. // message over and over for these fields. Seems to provide a slight
  782. // perforamance improvement in micro benchmark and this is also what proto1
  783. // code does.
  784. int totalBuilderBits = 0;
  785. int totalMessageBits = 0;
  786. for (int i = 0; i < descriptor_->field_count(); i++) {
  787. const FieldGenerator& field = field_generators_.get(descriptor_->field(i));
  788. totalBuilderBits += field.GetNumBitsForBuilder();
  789. totalMessageBits += field.GetNumBitsForMessage();
  790. }
  791. int totalBuilderInts = (totalBuilderBits + 31) / 32;
  792. int totalMessageInts = (totalMessageBits + 31) / 32;
  793. for (int i = 0; i < totalBuilderInts; i++) {
  794. printer->Print("int from_$bit_field_name$ = $bit_field_name$;\n",
  795. "bit_field_name", GetBitFieldName(i));
  796. }
  797. for (int i = 0; i < totalMessageInts; i++) {
  798. printer->Print("int to_$bit_field_name$ = 0;\n",
  799. "bit_field_name", GetBitFieldName(i));
  800. }
  801. // Output generation code for each field.
  802. for (int i = 0; i < descriptor_->field_count(); i++) {
  803. field_generators_.get(descriptor_->field(i)).GenerateBuildingCode(printer);
  804. }
  805. // Copy the bit field results to the generated message
  806. for (int i = 0; i < totalMessageInts; i++) {
  807. printer->Print("result.$bit_field_name$ = to_$bit_field_name$;\n",
  808. "bit_field_name", GetBitFieldName(i));
  809. }
  810. printer->Outdent();
  811. if (HasDescriptorMethods(descriptor_)) {
  812. printer->Print(
  813. " onBuilt();\n");
  814. }
  815. printer->Print(
  816. " return result;\n"
  817. "}\n"
  818. "\n",
  819. "classname", ClassName(descriptor_));
  820. // -----------------------------------------------------------------
  821. if (HasGeneratedMethods(descriptor_)) {
  822. // MergeFrom(Message other) requires the ability to distinguish the other
  823. // messages type by its descriptor.
  824. if (HasDescriptorMethods(descriptor_)) {
  825. printer->Print(
  826. "public Builder mergeFrom(com.google.protobuf.Message other) {\n"
  827. " if (other instanceof $classname$) {\n"
  828. " return mergeFrom(($classname$)other);\n"
  829. " } else {\n"
  830. " super.mergeFrom(other);\n"
  831. " return this;\n"
  832. " }\n"
  833. "}\n"
  834. "\n",
  835. "classname", ClassName(descriptor_));
  836. }
  837. printer->Print(
  838. "public Builder mergeFrom($classname$ other) {\n"
  839. // Optimization: If other is the default instance, we know none of its
  840. // fields are set so we can skip the merge.
  841. " if (other == $classname$.getDefaultInstance()) return this;\n",
  842. "classname", ClassName(descriptor_));
  843. printer->Indent();
  844. for (int i = 0; i < descriptor_->field_count(); i++) {
  845. field_generators_.get(descriptor_->field(i)).GenerateMergingCode(printer);
  846. }
  847. printer->Outdent();
  848. // if message type has extensions
  849. if (descriptor_->extension_range_count() > 0) {
  850. printer->Print(
  851. " this.mergeExtensionFields(other);\n");
  852. }
  853. if (HasUnknownFields(descriptor_)) {
  854. printer->Print(
  855. " this.mergeUnknownFields(other.getUnknownFields());\n");
  856. }
  857. printer->Print(
  858. " return this;\n"
  859. "}\n"
  860. "\n");
  861. }
  862. }
  863. // ===================================================================
  864. void MessageGenerator::GenerateBuilderParsingMethods(io::Printer* printer) {
  865. scoped_array<const FieldDescriptor*> sorted_fields(
  866. SortFieldsByNumber(descriptor_));
  867. printer->Print(
  868. "public Builder mergeFrom(\n"
  869. " com.google.protobuf.CodedInputStream input,\n"
  870. " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
  871. " throws java.io.IOException {\n");
  872. printer->Indent();
  873. if (HasUnknownFields(descriptor_)) {
  874. printer->Print(
  875. "com.google.protobuf.UnknownFieldSet.Builder unknownFields =\n"
  876. " com.google.protobuf.UnknownFieldSet.newBuilder(\n"
  877. " this.getUnknownFields());\n");
  878. }
  879. printer->Print(
  880. "while (true) {\n");
  881. printer->Indent();
  882. printer->Print(
  883. "int tag = input.readTag();\n"
  884. "switch (tag) {\n");
  885. printer->Indent();
  886. if (HasUnknownFields(descriptor_)) {
  887. printer->Print(
  888. "case 0:\n" // zero signals EOF / limit reached
  889. " this.setUnknownFields(unknownFields.build());\n"
  890. " $on_changed$\n"
  891. " return this;\n"
  892. "default: {\n"
  893. " if (!parseUnknownField(input, unknownFields,\n"
  894. " extensionRegistry, tag)) {\n"
  895. " this.setUnknownFields(unknownFields.build());\n"
  896. " $on_changed$\n"
  897. " return this;\n" // it's an endgroup tag
  898. " }\n"
  899. " break;\n"
  900. "}\n",
  901. "on_changed", HasDescriptorMethods(descriptor_) ? "onChanged();" : "");
  902. } else {
  903. printer->Print(
  904. "case 0:\n" // zero signals EOF / limit reached
  905. " $on_changed$\n"
  906. " return this;\n"
  907. "default: {\n"
  908. " if (!parseUnknownField(input, extensionRegistry, tag)) {\n"
  909. " $on_changed$\n"
  910. " return this;\n" // it's an endgroup tag
  911. " }\n"
  912. " break;\n"
  913. "}\n",
  914. "on_changed", HasDescriptorMethods(descriptor_) ? "onChanged();" : "");
  915. }
  916. for (int i = 0; i < descriptor_->field_count(); i++) {
  917. const FieldDescriptor* field = sorted_fields[i];
  918. uint32 tag = WireFormatLite::MakeTag(field->number(),
  919. WireFormat::WireTypeForFieldType(field->type()));
  920. printer->Print(
  921. "case $tag$: {\n",
  922. "tag", SimpleItoa(tag));
  923. printer->Indent();
  924. field_generators_.get(field).GenerateParsingCode(printer);
  925. printer->Outdent();
  926. printer->Print(
  927. " break;\n"
  928. "}\n");
  929. if (field->is_packable()) {
  930. // To make packed = true wire compatible, we generate parsing code from a
  931. // packed version of this field regardless of field->options().packed().
  932. uint32 packed_tag = WireFormatLite::MakeTag(field->number(),
  933. WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
  934. printer->Print(
  935. "case $tag$: {\n",
  936. "tag", SimpleItoa(packed_tag));
  937. printer->Indent();
  938. field_generators_.get(field).GenerateParsingCodeFromPacked(printer);
  939. printer->Outdent();
  940. printer->Print(
  941. " break;\n"
  942. "}\n");
  943. }
  944. }
  945. printer->Outdent();
  946. printer->Outdent();
  947. printer->Outdent();
  948. printer->Print(
  949. " }\n" // switch (tag)
  950. " }\n" // while (true)
  951. "}\n"
  952. "\n");
  953. }
  954. // ===================================================================
  955. void MessageGenerator::GenerateIsInitialized(
  956. io::Printer* printer, UseMemoization useMemoization) {
  957. bool memoization = useMemoization == MEMOIZE;
  958. if (memoization) {
  959. // Memoizes whether the protocol buffer is fully initialized (has all
  960. // required fields). -1 means not yet computed. 0 means false and 1 means
  961. // true.
  962. printer->Print(
  963. "private byte memoizedIsInitialized = -1;\n");
  964. }
  965. printer->Print(
  966. "public final boolean isInitialized() {\n");
  967. printer->Indent();
  968. if (memoization) {
  969. printer->Print(
  970. "byte isInitialized = memoizedIsInitialized;\n"
  971. "if (isInitialized != -1) return isInitialized == 1;\n"
  972. "\n");
  973. }
  974. // Check that all required fields in this message are set.
  975. // TODO(kenton): We can optimize this when we switch to putting all the
  976. // "has" fields into a single bitfield.
  977. for (int i = 0; i < descriptor_->field_count(); i++) {
  978. const FieldDescriptor* field = descriptor_->field(i);
  979. if (field->is_required()) {
  980. printer->Print(
  981. "if (!has$name$()) {\n"
  982. " $memoize$\n"
  983. " return false;\n"
  984. "}\n",
  985. "name", UnderscoresToCapitalizedCamelCase(field),
  986. "memoize", memoization ? "memoizedIsInitialized = 0;" : "");
  987. }
  988. }
  989. // Now check that all embedded messages are initialized.
  990. for (int i = 0; i < descriptor_->field_count(); i++) {
  991. const FieldDescriptor* field = descriptor_->field(i);
  992. if (GetJavaType(field) == JAVATYPE_MESSAGE &&
  993. HasRequiredFields(field->message_type())) {
  994. switch (field->label()) {
  995. case FieldDescriptor::LABEL_REQUIRED:
  996. printer->Print(
  997. "if (!get$name$().isInitialized()) {\n"
  998. " $memoize$\n"
  999. " return false;\n"
  1000. "}\n",
  1001. "type", ClassName(field->message_type()),
  1002. "name", UnderscoresToCapitalizedCamelCase(field),
  1003. "memoize", memoization ? "memoizedIsInitialized = 0;" : "");
  1004. break;
  1005. case FieldDescriptor::LABEL_OPTIONAL:
  1006. printer->Print(
  1007. "if (has$name$()) {\n"
  1008. " if (!get$name$().isInitialized()) {\n"
  1009. " $memoize$\n"
  1010. " return false;\n"
  1011. " }\n"
  1012. "}\n",
  1013. "type", ClassName(field->message_type()),
  1014. "name", UnderscoresToCapitalizedCamelCase(field),
  1015. "memoize", memoization ? "memoizedIsInitialized = 0;" : "");
  1016. break;
  1017. case FieldDescriptor::LABEL_REPEATED:
  1018. printer->Print(
  1019. "for (int i = 0; i < get$name$Count(); i++) {\n"
  1020. " if (!get$name$(i).isInitialized()) {\n"
  1021. " $memoize$\n"
  1022. " return false;\n"
  1023. " }\n"
  1024. "}\n",
  1025. "type", ClassName(field->message_type()),
  1026. "name", UnderscoresToCapitalizedCamelCase(field),
  1027. "memoize", memoization ? "memoizedIsInitialized = 0;" : "");
  1028. break;
  1029. }
  1030. }
  1031. }
  1032. if (descriptor_->extension_range_count() > 0) {
  1033. printer->Print(
  1034. "if (!extensionsAreInitialized()) {\n"
  1035. " $memoize$\n"
  1036. " return false;\n"
  1037. "}\n",
  1038. "memoize", memoization ? "memoizedIsInitialized = 0;" : "");
  1039. }
  1040. printer->Outdent();
  1041. if (memoization) {
  1042. printer->Print(
  1043. " memoizedIsInitialized = 1;\n");
  1044. }
  1045. printer->Print(
  1046. " return true;\n"
  1047. "}\n"
  1048. "\n");
  1049. }
  1050. // ===================================================================
  1051. void MessageGenerator::GenerateEqualsAndHashCode(io::Printer* printer) {
  1052. printer->Print(
  1053. "@java.lang.Override\n"
  1054. "public boolean equals(final java.lang.Object obj) {\n");
  1055. printer->Indent();
  1056. printer->Print(
  1057. "if (obj == this) {\n"
  1058. " return true;\n"
  1059. "}\n"
  1060. "if (!(obj instanceof $classname$)) {\n"
  1061. " return super.equals(obj);\n"
  1062. "}\n"
  1063. "$classname$ other = ($classname$) obj;\n"
  1064. "\n",
  1065. "classname", ClassName(descriptor_));
  1066. printer->Print("boolean result = true;\n");
  1067. for (int i = 0; i < descriptor_->field_count(); i++) {
  1068. const FieldDescriptor* field = descriptor_->field(i);
  1069. if (!field->is_repeated()) {
  1070. printer->Print(
  1071. "result = result && (has$name$() == other.has$name$());\n"
  1072. "if (has$name$()) {\n",
  1073. "name", UnderscoresToCapitalizedCamelCase(field));
  1074. printer->Indent();
  1075. }
  1076. field_generators_.get(field).GenerateEqualsCode(printer);
  1077. if (!field->is_repeated()) {
  1078. printer->Outdent();
  1079. printer->Print(
  1080. "}\n");
  1081. }
  1082. }
  1083. if (HasDescriptorMethods(descriptor_)) {
  1084. printer->Print(
  1085. "result = result &&\n"
  1086. " getUnknownFields().equals(other.getUnknownFields());\n");
  1087. if (descriptor_->extension_range_count() > 0) {
  1088. printer->Print(
  1089. "result = result &&\n"
  1090. " getExtensionFields().equals(other.getExtensionFields());\n");
  1091. }
  1092. }
  1093. printer->Print(
  1094. "return result;\n");
  1095. printer->Outdent();
  1096. printer->Print(
  1097. "}\n"
  1098. "\n");
  1099. printer->Print(
  1100. "@java.lang.Override\n"
  1101. "public int hashCode() {\n");
  1102. printer->Indent();
  1103. printer->Print(
  1104. "int hash = 41;\n"
  1105. "hash = (19 * hash) + getDescriptorForType().hashCode();\n");
  1106. for (int i = 0; i < descriptor_->field_count(); i++) {
  1107. const FieldDescriptor* field = descriptor_->field(i);
  1108. if (!field->is_repeated()) {
  1109. printer->Print(
  1110. "if (has$name$()) {\n",
  1111. "name", UnderscoresToCapitalizedCamelCase(field));
  1112. printer->Indent();
  1113. }
  1114. field_generators_.get(field).GenerateHashCode(printer);
  1115. if (!field->is_repeated()) {
  1116. printer->Outdent();
  1117. printer->Print("}\n");
  1118. }
  1119. }
  1120. if (HasDescriptorMethods(descriptor_)) {
  1121. if (descriptor_->extension_range_count() > 0) {
  1122. printer->Print(
  1123. "hash = hashFields(hash, getExtensionFields());\n");
  1124. }
  1125. }
  1126. printer->Print(
  1127. "hash = (29 * hash) + getUnknownFields().hashCode();\n"
  1128. "return hash;\n");
  1129. printer->Outdent();
  1130. printer->Print(
  1131. "}\n"
  1132. "\n");
  1133. }
  1134. // ===================================================================
  1135. void MessageGenerator::GenerateExtensionRegistrationCode(io::Printer* printer) {
  1136. for (int i = 0; i < descriptor_->extension_count(); i++) {
  1137. ExtensionGenerator(descriptor_->extension(i))
  1138. .GenerateRegistrationCode(printer);
  1139. }
  1140. for (int i = 0; i < descriptor_->nested_type_count(); i++) {
  1141. MessageGenerator(descriptor_->nested_type(i))
  1142. .GenerateExtensionRegistrationCode(printer);
  1143. }
  1144. }
  1145. } // namespace java
  1146. } // namespace compiler
  1147. } // namespace protobuf
  1148. } // namespace google