PageRenderTime 135ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/tomahawk-player/tomahawk
C++ | 884 lines | 639 code | 146 blank | 99 comment | 22 complexity | fa531c6e33b932793b966df713b4e223 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  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 <map>
  34. #include <string>
  35. #include <google/protobuf/compiler/java/java_message_field.h>
  36. #include <google/protobuf/compiler/java/java_helpers.h>
  37. #include <google/protobuf/io/printer.h>
  38. #include <google/protobuf/wire_format.h>
  39. #include <google/protobuf/stubs/strutil.h>
  40. namespace google {
  41. namespace protobuf {
  42. namespace compiler {
  43. namespace java {
  44. namespace {
  45. // TODO(kenton): Factor out a "SetCommonFieldVariables()" to get rid of
  46. // repeat code between this and the other field types.
  47. void SetMessageVariables(const FieldDescriptor* descriptor,
  48. int messageBitIndex,
  49. int builderBitIndex,
  50. map<string, string>* variables) {
  51. (*variables)["name"] =
  52. UnderscoresToCamelCase(descriptor);
  53. (*variables)["capitalized_name"] =
  54. UnderscoresToCapitalizedCamelCase(descriptor);
  55. (*variables)["constant_name"] = FieldConstantName(descriptor);
  56. (*variables)["number"] = SimpleItoa(descriptor->number());
  57. (*variables)["type"] = ClassName(descriptor->message_type());
  58. (*variables)["group_or_message"] =
  59. (GetType(descriptor) == FieldDescriptor::TYPE_GROUP) ?
  60. "Group" : "Message";
  61. // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
  62. // by the proto compiler
  63. (*variables)["deprecation"] = descriptor->options().deprecated()
  64. ? "@java.lang.Deprecated " : "";
  65. (*variables)["on_changed"] =
  66. HasDescriptorMethods(descriptor->containing_type()) ? "onChanged();" : "";
  67. // For singular messages and builders, one bit is used for the hasField bit.
  68. (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
  69. (*variables)["get_has_field_bit_builder"] = GenerateGetBit(builderBitIndex);
  70. (*variables)["set_has_field_bit_builder"] = GenerateSetBit(builderBitIndex);
  71. (*variables)["clear_has_field_bit_builder"] =
  72. GenerateClearBit(builderBitIndex);
  73. // For repated builders, one bit is used for whether the array is immutable.
  74. (*variables)["get_mutable_bit_builder"] = GenerateGetBit(builderBitIndex);
  75. (*variables)["set_mutable_bit_builder"] = GenerateSetBit(builderBitIndex);
  76. (*variables)["clear_mutable_bit_builder"] = GenerateClearBit(builderBitIndex);
  77. (*variables)["get_has_field_bit_from_local"] =
  78. GenerateGetBitFromLocal(builderBitIndex);
  79. (*variables)["set_has_field_bit_to_local"] =
  80. GenerateSetBitToLocal(messageBitIndex);
  81. }
  82. } // namespace
  83. // ===================================================================
  84. MessageFieldGenerator::
  85. MessageFieldGenerator(const FieldDescriptor* descriptor,
  86. int messageBitIndex,
  87. int builderBitIndex)
  88. : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
  89. builderBitIndex_(builderBitIndex) {
  90. SetMessageVariables(descriptor, messageBitIndex, builderBitIndex,
  91. &variables_);
  92. }
  93. MessageFieldGenerator::~MessageFieldGenerator() {}
  94. int MessageFieldGenerator::GetNumBitsForMessage() const {
  95. return 1;
  96. }
  97. int MessageFieldGenerator::GetNumBitsForBuilder() const {
  98. return 1;
  99. }
  100. void MessageFieldGenerator::
  101. GenerateInterfaceMembers(io::Printer* printer) const {
  102. // TODO(jonp): In the future, consider having a method specific to the
  103. // interface so that builders can choose dynamically to either return a
  104. // message or a nested builder, so that asking for the interface doesn't
  105. // cause a message to ever be built.
  106. printer->Print(variables_,
  107. "$deprecation$boolean has$capitalized_name$();\n"
  108. "$deprecation$$type$ get$capitalized_name$();\n");
  109. if (HasNestedBuilders(descriptor_->containing_type())) {
  110. printer->Print(variables_,
  111. "$deprecation$$type$OrBuilder get$capitalized_name$OrBuilder();\n");
  112. }
  113. }
  114. void MessageFieldGenerator::
  115. GenerateMembers(io::Printer* printer) const {
  116. printer->Print(variables_,
  117. "private $type$ $name$_;\n"
  118. "$deprecation$public boolean has$capitalized_name$() {\n"
  119. " return $get_has_field_bit_message$;\n"
  120. "}\n"
  121. "$deprecation$public $type$ get$capitalized_name$() {\n"
  122. " return $name$_;\n"
  123. "}\n");
  124. if (HasNestedBuilders(descriptor_->containing_type())) {
  125. printer->Print(variables_,
  126. "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder() {\n"
  127. " return $name$_;\n"
  128. "}\n");
  129. }
  130. }
  131. void MessageFieldGenerator::PrintNestedBuilderCondition(
  132. io::Printer* printer,
  133. const char* regular_case,
  134. const char* nested_builder_case) const {
  135. if (HasNestedBuilders(descriptor_->containing_type())) {
  136. printer->Print(variables_, "if ($name$Builder_ == null) {\n");
  137. printer->Indent();
  138. printer->Print(variables_, regular_case);
  139. printer->Outdent();
  140. printer->Print("} else {\n");
  141. printer->Indent();
  142. printer->Print(variables_, nested_builder_case);
  143. printer->Outdent();
  144. printer->Print("}\n");
  145. } else {
  146. printer->Print(variables_, regular_case);
  147. }
  148. }
  149. void MessageFieldGenerator::PrintNestedBuilderFunction(
  150. io::Printer* printer,
  151. const char* method_prototype,
  152. const char* regular_case,
  153. const char* nested_builder_case,
  154. const char* trailing_code) const {
  155. printer->Print(variables_, method_prototype);
  156. printer->Print(" {\n");
  157. printer->Indent();
  158. PrintNestedBuilderCondition(printer, regular_case, nested_builder_case);
  159. if (trailing_code != NULL) {
  160. printer->Print(variables_, trailing_code);
  161. }
  162. printer->Outdent();
  163. printer->Print("}\n");
  164. }
  165. void MessageFieldGenerator::
  166. GenerateBuilderMembers(io::Printer* printer) const {
  167. // When using nested-builders, the code initially works just like the
  168. // non-nested builder case. It only creates a nested builder lazily on
  169. // demand and then forever delegates to it after creation.
  170. printer->Print(variables_,
  171. // Used when the builder is null.
  172. "private $type$ $name$_ = $type$.getDefaultInstance();\n");
  173. if (HasNestedBuilders(descriptor_->containing_type())) {
  174. printer->Print(variables_,
  175. // If this builder is non-null, it is used and the other fields are
  176. // ignored.
  177. "private com.google.protobuf.SingleFieldBuilder<\n"
  178. " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;"
  179. "\n");
  180. }
  181. // The comments above the methods below are based on a hypothetical
  182. // field of type "Field" called "Field".
  183. // boolean hasField()
  184. printer->Print(variables_,
  185. "$deprecation$public boolean has$capitalized_name$() {\n"
  186. " return $get_has_field_bit_builder$;\n"
  187. "}\n");
  188. // Field getField()
  189. PrintNestedBuilderFunction(printer,
  190. "$deprecation$public $type$ get$capitalized_name$()",
  191. "return $name$_;\n",
  192. "return $name$Builder_.getMessage();\n",
  193. NULL);
  194. // Field.Builder setField(Field value)
  195. PrintNestedBuilderFunction(printer,
  196. "$deprecation$public Builder set$capitalized_name$($type$ value)",
  197. "if (value == null) {\n"
  198. " throw new NullPointerException();\n"
  199. "}\n"
  200. "$name$_ = value;\n"
  201. "$on_changed$\n",
  202. "$name$Builder_.setMessage(value);\n",
  203. "$set_has_field_bit_builder$;\n"
  204. "return this;\n");
  205. // Field.Builder setField(Field.Builder builderForValue)
  206. PrintNestedBuilderFunction(printer,
  207. "$deprecation$public Builder set$capitalized_name$(\n"
  208. " $type$.Builder builderForValue)",
  209. "$name$_ = builderForValue.build();\n"
  210. "$on_changed$\n",
  211. "$name$Builder_.setMessage(builderForValue.build());\n",
  212. "$set_has_field_bit_builder$;\n"
  213. "return this;\n");
  214. // Field.Builder mergeField(Field value)
  215. PrintNestedBuilderFunction(printer,
  216. "$deprecation$public Builder merge$capitalized_name$($type$ value)",
  217. "if ($get_has_field_bit_builder$ &&\n"
  218. " $name$_ != $type$.getDefaultInstance()) {\n"
  219. " $name$_ =\n"
  220. " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n"
  221. "} else {\n"
  222. " $name$_ = value;\n"
  223. "}\n"
  224. "$on_changed$\n",
  225. "$name$Builder_.mergeFrom(value);\n",
  226. "$set_has_field_bit_builder$;\n"
  227. "return this;\n");
  228. // Field.Builder clearField()
  229. PrintNestedBuilderFunction(printer,
  230. "$deprecation$public Builder clear$capitalized_name$()",
  231. "$name$_ = $type$.getDefaultInstance();\n"
  232. "$on_changed$\n",
  233. "$name$Builder_.clear();\n",
  234. "$clear_has_field_bit_builder$;\n"
  235. "return this;\n");
  236. if (HasNestedBuilders(descriptor_->containing_type())) {
  237. printer->Print(variables_,
  238. "$deprecation$public $type$.Builder get$capitalized_name$Builder() {\n"
  239. " $set_has_field_bit_builder$;\n"
  240. " $on_changed$\n"
  241. " return get$capitalized_name$FieldBuilder().getBuilder();\n"
  242. "}\n"
  243. "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder() {\n"
  244. " if ($name$Builder_ != null) {\n"
  245. " return $name$Builder_.getMessageOrBuilder();\n"
  246. " } else {\n"
  247. " return $name$_;\n"
  248. " }\n"
  249. "}\n"
  250. "private com.google.protobuf.SingleFieldBuilder<\n"
  251. " $type$, $type$.Builder, $type$OrBuilder> \n"
  252. " get$capitalized_name$FieldBuilder() {\n"
  253. " if ($name$Builder_ == null) {\n"
  254. " $name$Builder_ = new com.google.protobuf.SingleFieldBuilder<\n"
  255. " $type$, $type$.Builder, $type$OrBuilder>(\n"
  256. " $name$_,\n"
  257. " getParentForChildren(),\n"
  258. " isClean());\n"
  259. " $name$_ = null;\n"
  260. " }\n"
  261. " return $name$Builder_;\n"
  262. "}\n");
  263. }
  264. }
  265. void MessageFieldGenerator::
  266. GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
  267. printer->Print(variables_,
  268. "get$capitalized_name$FieldBuilder();\n");
  269. }
  270. void MessageFieldGenerator::
  271. GenerateInitializationCode(io::Printer* printer) const {
  272. printer->Print(variables_, "$name$_ = $type$.getDefaultInstance();\n");
  273. }
  274. void MessageFieldGenerator::
  275. GenerateBuilderClearCode(io::Printer* printer) const {
  276. PrintNestedBuilderCondition(printer,
  277. "$name$_ = $type$.getDefaultInstance();\n",
  278. "$name$Builder_.clear();\n");
  279. printer->Print(variables_, "$clear_has_field_bit_builder$;\n");
  280. }
  281. void MessageFieldGenerator::
  282. GenerateMergingCode(io::Printer* printer) const {
  283. printer->Print(variables_,
  284. "if (other.has$capitalized_name$()) {\n"
  285. " merge$capitalized_name$(other.get$capitalized_name$());\n"
  286. "}\n");
  287. }
  288. void MessageFieldGenerator::
  289. GenerateBuildingCode(io::Printer* printer) const {
  290. printer->Print(variables_,
  291. "if ($get_has_field_bit_from_local$) {\n"
  292. " $set_has_field_bit_to_local$;\n"
  293. "}\n");
  294. PrintNestedBuilderCondition(printer,
  295. "result.$name$_ = $name$_;\n",
  296. "result.$name$_ = $name$Builder_.build();\n");
  297. }
  298. void MessageFieldGenerator::
  299. GenerateParsingCode(io::Printer* printer) const {
  300. printer->Print(variables_,
  301. "$type$.Builder subBuilder = $type$.newBuilder();\n"
  302. "if (has$capitalized_name$()) {\n"
  303. " subBuilder.mergeFrom(get$capitalized_name$());\n"
  304. "}\n");
  305. if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) {
  306. printer->Print(variables_,
  307. "input.readGroup($number$, subBuilder, extensionRegistry);\n");
  308. } else {
  309. printer->Print(variables_,
  310. "input.readMessage(subBuilder, extensionRegistry);\n");
  311. }
  312. printer->Print(variables_,
  313. "set$capitalized_name$(subBuilder.buildPartial());\n");
  314. }
  315. void MessageFieldGenerator::
  316. GenerateSerializationCode(io::Printer* printer) const {
  317. printer->Print(variables_,
  318. "if ($get_has_field_bit_message$) {\n"
  319. " output.write$group_or_message$($number$, $name$_);\n"
  320. "}\n");
  321. }
  322. void MessageFieldGenerator::
  323. GenerateSerializedSizeCode(io::Printer* printer) const {
  324. printer->Print(variables_,
  325. "if ($get_has_field_bit_message$) {\n"
  326. " size += com.google.protobuf.CodedOutputStream\n"
  327. " .compute$group_or_message$Size($number$, $name$_);\n"
  328. "}\n");
  329. }
  330. void MessageFieldGenerator::
  331. GenerateEqualsCode(io::Printer* printer) const {
  332. printer->Print(variables_,
  333. "result = result && get$capitalized_name$()\n"
  334. " .equals(other.get$capitalized_name$());\n");
  335. }
  336. void MessageFieldGenerator::
  337. GenerateHashCode(io::Printer* printer) const {
  338. printer->Print(variables_,
  339. "hash = (37 * hash) + $constant_name$;\n"
  340. "hash = (53 * hash) + get$capitalized_name$().hashCode();\n");
  341. }
  342. string MessageFieldGenerator::GetBoxedType() const {
  343. return ClassName(descriptor_->message_type());
  344. }
  345. // ===================================================================
  346. RepeatedMessageFieldGenerator::
  347. RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor,
  348. int messageBitIndex,
  349. int builderBitIndex)
  350. : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
  351. builderBitIndex_(builderBitIndex) {
  352. SetMessageVariables(descriptor, messageBitIndex, builderBitIndex,
  353. &variables_);
  354. }
  355. RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {}
  356. int RepeatedMessageFieldGenerator::GetNumBitsForMessage() const {
  357. return 0;
  358. }
  359. int RepeatedMessageFieldGenerator::GetNumBitsForBuilder() const {
  360. return 1;
  361. }
  362. void RepeatedMessageFieldGenerator::
  363. GenerateInterfaceMembers(io::Printer* printer) const {
  364. // TODO(jonp): In the future, consider having methods specific to the
  365. // interface so that builders can choose dynamically to either return a
  366. // message or a nested builder, so that asking for the interface doesn't
  367. // cause a message to ever be built.
  368. printer->Print(variables_,
  369. "$deprecation$java.util.List<$type$> \n"
  370. " get$capitalized_name$List();\n"
  371. "$deprecation$$type$ get$capitalized_name$(int index);\n"
  372. "$deprecation$int get$capitalized_name$Count();\n");
  373. if (HasNestedBuilders(descriptor_->containing_type())) {
  374. printer->Print(variables_,
  375. "$deprecation$java.util.List<? extends $type$OrBuilder> \n"
  376. " get$capitalized_name$OrBuilderList();\n"
  377. "$deprecation$$type$OrBuilder get$capitalized_name$OrBuilder(\n"
  378. " int index);\n");
  379. }
  380. }
  381. void RepeatedMessageFieldGenerator::
  382. GenerateMembers(io::Printer* printer) const {
  383. printer->Print(variables_,
  384. "private java.util.List<$type$> $name$_;\n"
  385. "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n"
  386. " return $name$_;\n" // note: unmodifiable list
  387. "}\n"
  388. "$deprecation$public java.util.List<? extends $type$OrBuilder> \n"
  389. " get$capitalized_name$OrBuilderList() {\n"
  390. " return $name$_;\n"
  391. "}\n"
  392. "$deprecation$public int get$capitalized_name$Count() {\n"
  393. " return $name$_.size();\n"
  394. "}\n"
  395. "$deprecation$public $type$ get$capitalized_name$(int index) {\n"
  396. " return $name$_.get(index);\n"
  397. "}\n"
  398. "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder(\n"
  399. " int index) {\n"
  400. " return $name$_.get(index);\n"
  401. "}\n");
  402. }
  403. void RepeatedMessageFieldGenerator::PrintNestedBuilderCondition(
  404. io::Printer* printer,
  405. const char* regular_case,
  406. const char* nested_builder_case) const {
  407. if (HasNestedBuilders(descriptor_->containing_type())) {
  408. printer->Print(variables_, "if ($name$Builder_ == null) {\n");
  409. printer->Indent();
  410. printer->Print(variables_, regular_case);
  411. printer->Outdent();
  412. printer->Print("} else {\n");
  413. printer->Indent();
  414. printer->Print(variables_, nested_builder_case);
  415. printer->Outdent();
  416. printer->Print("}\n");
  417. } else {
  418. printer->Print(variables_, regular_case);
  419. }
  420. }
  421. void RepeatedMessageFieldGenerator::PrintNestedBuilderFunction(
  422. io::Printer* printer,
  423. const char* method_prototype,
  424. const char* regular_case,
  425. const char* nested_builder_case,
  426. const char* trailing_code) const {
  427. printer->Print(variables_, method_prototype);
  428. printer->Print(" {\n");
  429. printer->Indent();
  430. PrintNestedBuilderCondition(printer, regular_case, nested_builder_case);
  431. if (trailing_code != NULL) {
  432. printer->Print(variables_, trailing_code);
  433. }
  434. printer->Outdent();
  435. printer->Print("}\n");
  436. }
  437. void RepeatedMessageFieldGenerator::
  438. GenerateBuilderMembers(io::Printer* printer) const {
  439. // When using nested-builders, the code initially works just like the
  440. // non-nested builder case. It only creates a nested builder lazily on
  441. // demand and then forever delegates to it after creation.
  442. printer->Print(variables_,
  443. // Used when the builder is null.
  444. // One field is the list and the other field keeps track of whether the
  445. // list is immutable. If it's immutable, the invariant is that it must
  446. // either an instance of Collections.emptyList() or it's an ArrayList
  447. // wrapped in a Collections.unmodifiableList() wrapper and nobody else has
  448. // a refererence to the underlying ArrayList. This invariant allows us to
  449. // share instances of lists between protocol buffers avoiding expensive
  450. // memory allocations. Note, immutable is a strong guarantee here -- not
  451. // just that the list cannot be modified via the reference but that the
  452. // list can never be modified.
  453. "private java.util.List<$type$> $name$_ =\n"
  454. " java.util.Collections.emptyList();\n"
  455. "private void ensure$capitalized_name$IsMutable() {\n"
  456. " if (!$get_mutable_bit_builder$) {\n"
  457. " $name$_ = new java.util.ArrayList<$type$>($name$_);\n"
  458. " $set_mutable_bit_builder$;\n"
  459. " }\n"
  460. "}\n"
  461. "\n");
  462. if (HasNestedBuilders(descriptor_->containing_type())) {
  463. printer->Print(variables_,
  464. // If this builder is non-null, it is used and the other fields are
  465. // ignored.
  466. "private com.google.protobuf.RepeatedFieldBuilder<\n"
  467. " $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;\n"
  468. "\n");
  469. }
  470. // The comments above the methods below are based on a hypothetical
  471. // repeated field of type "Field" called "RepeatedField".
  472. // List<Field> getRepeatedFieldList()
  473. PrintNestedBuilderFunction(printer,
  474. "$deprecation$public java.util.List<$type$> get$capitalized_name$List()",
  475. "return java.util.Collections.unmodifiableList($name$_);\n",
  476. "return $name$Builder_.getMessageList();\n",
  477. NULL);
  478. // int getRepeatedFieldCount()
  479. PrintNestedBuilderFunction(printer,
  480. "$deprecation$public int get$capitalized_name$Count()",
  481. "return $name$_.size();\n",
  482. "return $name$Builder_.getCount();\n",
  483. NULL);
  484. // Field getRepeatedField(int index)
  485. PrintNestedBuilderFunction(printer,
  486. "$deprecation$public $type$ get$capitalized_name$(int index)",
  487. "return $name$_.get(index);\n",
  488. "return $name$Builder_.getMessage(index);\n",
  489. NULL);
  490. // Builder setRepeatedField(int index, Field value)
  491. PrintNestedBuilderFunction(printer,
  492. "$deprecation$public Builder set$capitalized_name$(\n"
  493. " int index, $type$ value)",
  494. "if (value == null) {\n"
  495. " throw new NullPointerException();\n"
  496. "}\n"
  497. "ensure$capitalized_name$IsMutable();\n"
  498. "$name$_.set(index, value);\n"
  499. "$on_changed$\n",
  500. "$name$Builder_.setMessage(index, value);\n",
  501. "return this;\n");
  502. // Builder setRepeatedField(int index, Field.Builder builderForValue)
  503. PrintNestedBuilderFunction(printer,
  504. "$deprecation$public Builder set$capitalized_name$(\n"
  505. " int index, $type$.Builder builderForValue)",
  506. "ensure$capitalized_name$IsMutable();\n"
  507. "$name$_.set(index, builderForValue.build());\n"
  508. "$on_changed$\n",
  509. "$name$Builder_.setMessage(index, builderForValue.build());\n",
  510. "return this;\n");
  511. // Builder addRepeatedField(Field value)
  512. PrintNestedBuilderFunction(printer,
  513. "$deprecation$public Builder add$capitalized_name$($type$ value)",
  514. "if (value == null) {\n"
  515. " throw new NullPointerException();\n"
  516. "}\n"
  517. "ensure$capitalized_name$IsMutable();\n"
  518. "$name$_.add(value);\n"
  519. "$on_changed$\n",
  520. "$name$Builder_.addMessage(value);\n",
  521. "return this;\n");
  522. // Builder addRepeatedField(int index, Field value)
  523. PrintNestedBuilderFunction(printer,
  524. "$deprecation$public Builder add$capitalized_name$(\n"
  525. " int index, $type$ value)",
  526. "if (value == null) {\n"
  527. " throw new NullPointerException();\n"
  528. "}\n"
  529. "ensure$capitalized_name$IsMutable();\n"
  530. "$name$_.add(index, value);\n"
  531. "$on_changed$\n",
  532. "$name$Builder_.addMessage(index, value);\n",
  533. "return this;\n");
  534. // Builder addRepeatedField(Field.Builder builderForValue)
  535. PrintNestedBuilderFunction(printer,
  536. "$deprecation$public Builder add$capitalized_name$(\n"
  537. " $type$.Builder builderForValue)",
  538. "ensure$capitalized_name$IsMutable();\n"
  539. "$name$_.add(builderForValue.build());\n"
  540. "$on_changed$\n",
  541. "$name$Builder_.addMessage(builderForValue.build());\n",
  542. "return this;\n");
  543. // Builder addRepeatedField(int index, Field.Builder builderForValue)
  544. PrintNestedBuilderFunction(printer,
  545. "$deprecation$public Builder add$capitalized_name$(\n"
  546. " int index, $type$.Builder builderForValue)",
  547. "ensure$capitalized_name$IsMutable();\n"
  548. "$name$_.add(index, builderForValue.build());\n"
  549. "$on_changed$\n",
  550. "$name$Builder_.addMessage(index, builderForValue.build());\n",
  551. "return this;\n");
  552. // Builder addAllRepeatedField(Iterable<Field> values)
  553. PrintNestedBuilderFunction(printer,
  554. "$deprecation$public Builder addAll$capitalized_name$(\n"
  555. " java.lang.Iterable<? extends $type$> values)",
  556. "ensure$capitalized_name$IsMutable();\n"
  557. "super.addAll(values, $name$_);\n"
  558. "$on_changed$\n",
  559. "$name$Builder_.addAllMessages(values);\n",
  560. "return this;\n");
  561. // Builder clearAllRepeatedField()
  562. PrintNestedBuilderFunction(printer,
  563. "$deprecation$public Builder clear$capitalized_name$()",
  564. "$name$_ = java.util.Collections.emptyList();\n"
  565. "$clear_mutable_bit_builder$;\n"
  566. "$on_changed$\n",
  567. "$name$Builder_.clear();\n",
  568. "return this;\n");
  569. // Builder removeRepeatedField(int index)
  570. PrintNestedBuilderFunction(printer,
  571. "$deprecation$public Builder remove$capitalized_name$(int index)",
  572. "ensure$capitalized_name$IsMutable();\n"
  573. "$name$_.remove(index);\n"
  574. "$on_changed$\n",
  575. "$name$Builder_.remove(index);\n",
  576. "return this;\n");
  577. if (HasNestedBuilders(descriptor_->containing_type())) {
  578. printer->Print(variables_,
  579. "$deprecation$public $type$.Builder get$capitalized_name$Builder(\n"
  580. " int index) {\n"
  581. " return get$capitalized_name$FieldBuilder().getBuilder(index);\n"
  582. "}\n"
  583. "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder(\n"
  584. " int index) {\n"
  585. " if ($name$Builder_ == null) {\n"
  586. " return $name$_.get(index);"
  587. " } else {\n"
  588. " return $name$Builder_.getMessageOrBuilder(index);\n"
  589. " }\n"
  590. "}\n"
  591. "$deprecation$public java.util.List<? extends $type$OrBuilder> \n"
  592. " get$capitalized_name$OrBuilderList() {\n"
  593. " if ($name$Builder_ != null) {\n"
  594. " return $name$Builder_.getMessageOrBuilderList();\n"
  595. " } else {\n"
  596. " return java.util.Collections.unmodifiableList($name$_);\n"
  597. " }\n"
  598. "}\n"
  599. "$deprecation$public $type$.Builder add$capitalized_name$Builder() {\n"
  600. " return get$capitalized_name$FieldBuilder().addBuilder(\n"
  601. " $type$.getDefaultInstance());\n"
  602. "}\n"
  603. "$deprecation$public $type$.Builder add$capitalized_name$Builder(\n"
  604. " int index) {\n"
  605. " return get$capitalized_name$FieldBuilder().addBuilder(\n"
  606. " index, $type$.getDefaultInstance());\n"
  607. "}\n"
  608. "$deprecation$public java.util.List<$type$.Builder> \n"
  609. " get$capitalized_name$BuilderList() {\n"
  610. " return get$capitalized_name$FieldBuilder().getBuilderList();\n"
  611. "}\n"
  612. "private com.google.protobuf.RepeatedFieldBuilder<\n"
  613. " $type$, $type$.Builder, $type$OrBuilder> \n"
  614. " get$capitalized_name$FieldBuilder() {\n"
  615. " if ($name$Builder_ == null) {\n"
  616. " $name$Builder_ = new com.google.protobuf.RepeatedFieldBuilder<\n"
  617. " $type$, $type$.Builder, $type$OrBuilder>(\n"
  618. " $name$_,\n"
  619. " $get_mutable_bit_builder$,\n"
  620. " getParentForChildren(),\n"
  621. " isClean());\n"
  622. " $name$_ = null;\n"
  623. " }\n"
  624. " return $name$Builder_;\n"
  625. "}\n");
  626. }
  627. }
  628. void RepeatedMessageFieldGenerator::
  629. GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
  630. printer->Print(variables_,
  631. "get$capitalized_name$FieldBuilder();\n");
  632. }
  633. void RepeatedMessageFieldGenerator::
  634. GenerateInitializationCode(io::Printer* printer) const {
  635. printer->Print(variables_, "$name$_ = java.util.Collections.emptyList();\n");
  636. }
  637. void RepeatedMessageFieldGenerator::
  638. GenerateBuilderClearCode(io::Printer* printer) const {
  639. PrintNestedBuilderCondition(printer,
  640. "$name$_ = java.util.Collections.emptyList();\n"
  641. "$clear_mutable_bit_builder$;\n",
  642. "$name$Builder_.clear();\n");
  643. }
  644. void RepeatedMessageFieldGenerator::
  645. GenerateMergingCode(io::Printer* printer) const {
  646. // The code below does two optimizations (non-nested builder case):
  647. // 1. If the other list is empty, there's nothing to do. This ensures we
  648. // don't allocate a new array if we already have an immutable one.
  649. // 2. If the other list is non-empty and our current list is empty, we can
  650. // reuse the other list which is guaranteed to be immutable.
  651. PrintNestedBuilderCondition(printer,
  652. "if (!other.$name$_.isEmpty()) {\n"
  653. " if ($name$_.isEmpty()) {\n"
  654. " $name$_ = other.$name$_;\n"
  655. " $clear_mutable_bit_builder$;\n"
  656. " } else {\n"
  657. " ensure$capitalized_name$IsMutable();\n"
  658. " $name$_.addAll(other.$name$_);\n"
  659. " }\n"
  660. " $on_changed$\n"
  661. "}\n",
  662. "if (!other.$name$_.isEmpty()) {\n"
  663. " if ($name$Builder_.isEmpty()) {\n"
  664. " $name$Builder_.dispose();\n"
  665. " $name$Builder_ = null;\n"
  666. " $name$_ = other.$name$_;\n"
  667. " $clear_mutable_bit_builder$;\n"
  668. " $name$Builder_ = \n"
  669. " com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders ?\n"
  670. " get$capitalized_name$FieldBuilder() : null;\n"
  671. " } else {\n"
  672. " $name$Builder_.addAllMessages(other.$name$_);\n"
  673. " }\n"
  674. "}\n");
  675. }
  676. void RepeatedMessageFieldGenerator::
  677. GenerateBuildingCode(io::Printer* printer) const {
  678. // The code below (non-nested builder case) ensures that the result has an
  679. // immutable list. If our list is immutable, we can just reuse it. If not,
  680. // we make it immutable.
  681. PrintNestedBuilderCondition(printer,
  682. "if ($get_mutable_bit_builder$) {\n"
  683. " $name$_ = java.util.Collections.unmodifiableList($name$_);\n"
  684. " $clear_mutable_bit_builder$;\n"
  685. "}\n"
  686. "result.$name$_ = $name$_;\n",
  687. "result.$name$_ = $name$Builder_.build();\n");
  688. }
  689. void RepeatedMessageFieldGenerator::
  690. GenerateParsingCode(io::Printer* printer) const {
  691. printer->Print(variables_,
  692. "$type$.Builder subBuilder = $type$.newBuilder();\n");
  693. if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) {
  694. printer->Print(variables_,
  695. "input.readGroup($number$, subBuilder, extensionRegistry);\n");
  696. } else {
  697. printer->Print(variables_,
  698. "input.readMessage(subBuilder, extensionRegistry);\n");
  699. }
  700. printer->Print(variables_,
  701. "add$capitalized_name$(subBuilder.buildPartial());\n");
  702. }
  703. void RepeatedMessageFieldGenerator::
  704. GenerateSerializationCode(io::Printer* printer) const {
  705. printer->Print(variables_,
  706. "for (int i = 0; i < $name$_.size(); i++) {\n"
  707. " output.write$group_or_message$($number$, $name$_.get(i));\n"
  708. "}\n");
  709. }
  710. void RepeatedMessageFieldGenerator::
  711. GenerateSerializedSizeCode(io::Printer* printer) const {
  712. printer->Print(variables_,
  713. "for (int i = 0; i < $name$_.size(); i++) {\n"
  714. " size += com.google.protobuf.CodedOutputStream\n"
  715. " .compute$group_or_message$Size($number$, $name$_.get(i));\n"
  716. "}\n");
  717. }
  718. void RepeatedMessageFieldGenerator::
  719. GenerateEqualsCode(io::Printer* printer) const {
  720. printer->Print(variables_,
  721. "result = result && get$capitalized_name$List()\n"
  722. " .equals(other.get$capitalized_name$List());\n");
  723. }
  724. void RepeatedMessageFieldGenerator::
  725. GenerateHashCode(io::Printer* printer) const {
  726. printer->Print(variables_,
  727. "if (get$capitalized_name$Count() > 0) {\n"
  728. " hash = (37 * hash) + $constant_name$;\n"
  729. " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n"
  730. "}\n");
  731. }
  732. string RepeatedMessageFieldGenerator::GetBoxedType() const {
  733. return ClassName(descriptor_->message_type());
  734. }
  735. } // namespace java
  736. } // namespace compiler
  737. } // namespace protobuf
  738. } // namespace google