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

http://github.com/tomahawk-player/tomahawk · C++ · 719 lines · 559 code · 87 blank · 73 comment · 24 complexity · a350429e497e38194ab8b70ff3f7d67c 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 <map>
  34. #include <string>
  35. #include <google/protobuf/compiler/java/java_primitive_field.h>
  36. #include <google/protobuf/stubs/common.h>
  37. #include <google/protobuf/compiler/java/java_helpers.h>
  38. #include <google/protobuf/io/printer.h>
  39. #include <google/protobuf/wire_format.h>
  40. #include <google/protobuf/stubs/strutil.h>
  41. namespace google {
  42. namespace protobuf {
  43. namespace compiler {
  44. namespace java {
  45. using internal::WireFormat;
  46. using internal::WireFormatLite;
  47. namespace {
  48. const char* PrimitiveTypeName(JavaType type) {
  49. switch (type) {
  50. case JAVATYPE_INT : return "int";
  51. case JAVATYPE_LONG : return "long";
  52. case JAVATYPE_FLOAT : return "float";
  53. case JAVATYPE_DOUBLE : return "double";
  54. case JAVATYPE_BOOLEAN: return "boolean";
  55. case JAVATYPE_STRING : return "java.lang.String";
  56. case JAVATYPE_BYTES : return "com.google.protobuf.ByteString";
  57. case JAVATYPE_ENUM : return NULL;
  58. case JAVATYPE_MESSAGE: return NULL;
  59. // No default because we want the compiler to complain if any new
  60. // JavaTypes are added.
  61. }
  62. GOOGLE_LOG(FATAL) << "Can't get here.";
  63. return NULL;
  64. }
  65. bool IsReferenceType(JavaType type) {
  66. switch (type) {
  67. case JAVATYPE_INT : return false;
  68. case JAVATYPE_LONG : return false;
  69. case JAVATYPE_FLOAT : return false;
  70. case JAVATYPE_DOUBLE : return false;
  71. case JAVATYPE_BOOLEAN: return false;
  72. case JAVATYPE_STRING : return true;
  73. case JAVATYPE_BYTES : return true;
  74. case JAVATYPE_ENUM : return true;
  75. case JAVATYPE_MESSAGE: return true;
  76. // No default because we want the compiler to complain if any new
  77. // JavaTypes are added.
  78. }
  79. GOOGLE_LOG(FATAL) << "Can't get here.";
  80. return false;
  81. }
  82. const char* GetCapitalizedType(const FieldDescriptor* field) {
  83. switch (GetType(field)) {
  84. case FieldDescriptor::TYPE_INT32 : return "Int32" ;
  85. case FieldDescriptor::TYPE_UINT32 : return "UInt32" ;
  86. case FieldDescriptor::TYPE_SINT32 : return "SInt32" ;
  87. case FieldDescriptor::TYPE_FIXED32 : return "Fixed32" ;
  88. case FieldDescriptor::TYPE_SFIXED32: return "SFixed32";
  89. case FieldDescriptor::TYPE_INT64 : return "Int64" ;
  90. case FieldDescriptor::TYPE_UINT64 : return "UInt64" ;
  91. case FieldDescriptor::TYPE_SINT64 : return "SInt64" ;
  92. case FieldDescriptor::TYPE_FIXED64 : return "Fixed64" ;
  93. case FieldDescriptor::TYPE_SFIXED64: return "SFixed64";
  94. case FieldDescriptor::TYPE_FLOAT : return "Float" ;
  95. case FieldDescriptor::TYPE_DOUBLE : return "Double" ;
  96. case FieldDescriptor::TYPE_BOOL : return "Bool" ;
  97. case FieldDescriptor::TYPE_STRING : return "String" ;
  98. case FieldDescriptor::TYPE_BYTES : return "Bytes" ;
  99. case FieldDescriptor::TYPE_ENUM : return "Enum" ;
  100. case FieldDescriptor::TYPE_GROUP : return "Group" ;
  101. case FieldDescriptor::TYPE_MESSAGE : return "Message" ;
  102. // No default because we want the compiler to complain if any new
  103. // types are added.
  104. }
  105. GOOGLE_LOG(FATAL) << "Can't get here.";
  106. return NULL;
  107. }
  108. // For encodings with fixed sizes, returns that size in bytes. Otherwise
  109. // returns -1.
  110. int FixedSize(FieldDescriptor::Type type) {
  111. switch (type) {
  112. case FieldDescriptor::TYPE_INT32 : return -1;
  113. case FieldDescriptor::TYPE_INT64 : return -1;
  114. case FieldDescriptor::TYPE_UINT32 : return -1;
  115. case FieldDescriptor::TYPE_UINT64 : return -1;
  116. case FieldDescriptor::TYPE_SINT32 : return -1;
  117. case FieldDescriptor::TYPE_SINT64 : return -1;
  118. case FieldDescriptor::TYPE_FIXED32 : return WireFormatLite::kFixed32Size;
  119. case FieldDescriptor::TYPE_FIXED64 : return WireFormatLite::kFixed64Size;
  120. case FieldDescriptor::TYPE_SFIXED32: return WireFormatLite::kSFixed32Size;
  121. case FieldDescriptor::TYPE_SFIXED64: return WireFormatLite::kSFixed64Size;
  122. case FieldDescriptor::TYPE_FLOAT : return WireFormatLite::kFloatSize;
  123. case FieldDescriptor::TYPE_DOUBLE : return WireFormatLite::kDoubleSize;
  124. case FieldDescriptor::TYPE_BOOL : return WireFormatLite::kBoolSize;
  125. case FieldDescriptor::TYPE_ENUM : return -1;
  126. case FieldDescriptor::TYPE_STRING : return -1;
  127. case FieldDescriptor::TYPE_BYTES : return -1;
  128. case FieldDescriptor::TYPE_GROUP : return -1;
  129. case FieldDescriptor::TYPE_MESSAGE : return -1;
  130. // No default because we want the compiler to complain if any new
  131. // types are added.
  132. }
  133. GOOGLE_LOG(FATAL) << "Can't get here.";
  134. return -1;
  135. }
  136. void SetPrimitiveVariables(const FieldDescriptor* descriptor,
  137. int messageBitIndex,
  138. int builderBitIndex,
  139. map<string, string>* variables) {
  140. (*variables)["name"] =
  141. UnderscoresToCamelCase(descriptor);
  142. (*variables)["capitalized_name"] =
  143. UnderscoresToCapitalizedCamelCase(descriptor);
  144. (*variables)["constant_name"] = FieldConstantName(descriptor);
  145. (*variables)["number"] = SimpleItoa(descriptor->number());
  146. (*variables)["type"] = PrimitiveTypeName(GetJavaType(descriptor));
  147. (*variables)["boxed_type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor));
  148. (*variables)["field_type"] = (*variables)["type"];
  149. (*variables)["field_list_type"] = "java.util.List<" +
  150. (*variables)["boxed_type"] + ">";
  151. (*variables)["empty_list"] = "java.util.Collections.emptyList()";
  152. (*variables)["default"] = DefaultValue(descriptor);
  153. (*variables)["default_init"] = IsDefaultValueJavaDefault(descriptor) ?
  154. "" : ("= " + DefaultValue(descriptor));
  155. (*variables)["capitalized_type"] = GetCapitalizedType(descriptor);
  156. (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor));
  157. (*variables)["tag_size"] = SimpleItoa(
  158. WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
  159. if (IsReferenceType(GetJavaType(descriptor))) {
  160. (*variables)["null_check"] =
  161. " if (value == null) {\n"
  162. " throw new NullPointerException();\n"
  163. " }\n";
  164. } else {
  165. (*variables)["null_check"] = "";
  166. }
  167. // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
  168. // by the proto compiler
  169. (*variables)["deprecation"] = descriptor->options().deprecated()
  170. ? "@java.lang.Deprecated " : "";
  171. int fixed_size = FixedSize(GetType(descriptor));
  172. if (fixed_size != -1) {
  173. (*variables)["fixed_size"] = SimpleItoa(fixed_size);
  174. }
  175. (*variables)["on_changed"] =
  176. HasDescriptorMethods(descriptor->containing_type()) ? "onChanged();" : "";
  177. // For singular messages and builders, one bit is used for the hasField bit.
  178. (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
  179. (*variables)["get_has_field_bit_builder"] = GenerateGetBit(builderBitIndex);
  180. (*variables)["set_has_field_bit_builder"] = GenerateSetBit(builderBitIndex);
  181. (*variables)["clear_has_field_bit_builder"] =
  182. GenerateClearBit(builderBitIndex);
  183. // For repated builders, one bit is used for whether the array is immutable.
  184. (*variables)["get_mutable_bit_builder"] = GenerateGetBit(builderBitIndex);
  185. (*variables)["set_mutable_bit_builder"] = GenerateSetBit(builderBitIndex);
  186. (*variables)["clear_mutable_bit_builder"] = GenerateClearBit(builderBitIndex);
  187. (*variables)["get_has_field_bit_from_local"] =
  188. GenerateGetBitFromLocal(builderBitIndex);
  189. (*variables)["set_has_field_bit_to_local"] =
  190. GenerateSetBitToLocal(messageBitIndex);
  191. }
  192. } // namespace
  193. // ===================================================================
  194. PrimitiveFieldGenerator::
  195. PrimitiveFieldGenerator(const FieldDescriptor* descriptor,
  196. int messageBitIndex,
  197. int builderBitIndex)
  198. : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
  199. builderBitIndex_(builderBitIndex) {
  200. SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
  201. &variables_);
  202. }
  203. PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
  204. int PrimitiveFieldGenerator::GetNumBitsForMessage() const {
  205. return 1;
  206. }
  207. int PrimitiveFieldGenerator::GetNumBitsForBuilder() const {
  208. return 1;
  209. }
  210. void PrimitiveFieldGenerator::
  211. GenerateInterfaceMembers(io::Printer* printer) const {
  212. printer->Print(variables_,
  213. "$deprecation$boolean has$capitalized_name$();\n"
  214. "$deprecation$$type$ get$capitalized_name$();\n");
  215. }
  216. void PrimitiveFieldGenerator::
  217. GenerateMembers(io::Printer* printer) const {
  218. printer->Print(variables_,
  219. "private $field_type$ $name$_;\n"
  220. "$deprecation$public boolean has$capitalized_name$() {\n"
  221. " return $get_has_field_bit_message$;\n"
  222. "}\n");
  223. printer->Print(variables_,
  224. "$deprecation$public $type$ get$capitalized_name$() {\n"
  225. " return $name$_;\n"
  226. "}\n");
  227. }
  228. void PrimitiveFieldGenerator::
  229. GenerateBuilderMembers(io::Printer* printer) const {
  230. printer->Print(variables_,
  231. "private $field_type$ $name$_ $default_init$;\n"
  232. "$deprecation$public boolean has$capitalized_name$() {\n"
  233. " return $get_has_field_bit_builder$;\n"
  234. "}\n");
  235. printer->Print(variables_,
  236. "$deprecation$public $type$ get$capitalized_name$() {\n"
  237. " return $name$_;\n"
  238. "}\n");
  239. printer->Print(variables_,
  240. "$deprecation$public Builder set$capitalized_name$($type$ value) {\n"
  241. "$null_check$"
  242. " $set_has_field_bit_builder$;\n"
  243. " $name$_ = value;\n"
  244. " $on_changed$\n"
  245. " return this;\n"
  246. "}\n"
  247. "$deprecation$public Builder clear$capitalized_name$() {\n"
  248. " $clear_has_field_bit_builder$;\n");
  249. JavaType type = GetJavaType(descriptor_);
  250. if (type == JAVATYPE_STRING || type == JAVATYPE_BYTES) {
  251. // The default value is not a simple literal so we want to avoid executing
  252. // it multiple times. Instead, get the default out of the default instance.
  253. printer->Print(variables_,
  254. " $name$_ = getDefaultInstance().get$capitalized_name$();\n");
  255. } else {
  256. printer->Print(variables_,
  257. " $name$_ = $default$;\n");
  258. }
  259. printer->Print(variables_,
  260. " $on_changed$\n"
  261. " return this;\n"
  262. "}\n");
  263. }
  264. void PrimitiveFieldGenerator::
  265. GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
  266. // noop for primitives
  267. }
  268. void PrimitiveFieldGenerator::
  269. GenerateInitializationCode(io::Printer* printer) const {
  270. printer->Print(variables_, "$name$_ = $default$;\n");
  271. }
  272. void PrimitiveFieldGenerator::
  273. GenerateBuilderClearCode(io::Printer* printer) const {
  274. printer->Print(variables_,
  275. "$name$_ = $default$;\n"
  276. "$clear_has_field_bit_builder$;\n");
  277. }
  278. void PrimitiveFieldGenerator::
  279. GenerateMergingCode(io::Printer* printer) const {
  280. printer->Print(variables_,
  281. "if (other.has$capitalized_name$()) {\n"
  282. " set$capitalized_name$(other.get$capitalized_name$());\n"
  283. "}\n");
  284. }
  285. void PrimitiveFieldGenerator::
  286. GenerateBuildingCode(io::Printer* printer) const {
  287. printer->Print(variables_,
  288. "if ($get_has_field_bit_from_local$) {\n"
  289. " $set_has_field_bit_to_local$;\n"
  290. "}\n"
  291. "result.$name$_ = $name$_;\n");
  292. }
  293. void PrimitiveFieldGenerator::
  294. GenerateParsingCode(io::Printer* printer) const {
  295. printer->Print(variables_,
  296. "$set_has_field_bit_builder$;\n"
  297. "$name$_ = input.read$capitalized_type$();\n");
  298. }
  299. void PrimitiveFieldGenerator::
  300. GenerateSerializationCode(io::Printer* printer) const {
  301. printer->Print(variables_,
  302. "if ($get_has_field_bit_message$) {\n"
  303. " output.write$capitalized_type$($number$, $name$_);\n"
  304. "}\n");
  305. }
  306. void PrimitiveFieldGenerator::
  307. GenerateSerializedSizeCode(io::Printer* printer) const {
  308. printer->Print(variables_,
  309. "if ($get_has_field_bit_message$) {\n"
  310. " size += com.google.protobuf.CodedOutputStream\n"
  311. " .compute$capitalized_type$Size($number$, $name$_);\n"
  312. "}\n");
  313. }
  314. void PrimitiveFieldGenerator::
  315. GenerateEqualsCode(io::Printer* printer) const {
  316. switch (GetJavaType(descriptor_)) {
  317. case JAVATYPE_INT:
  318. case JAVATYPE_LONG:
  319. case JAVATYPE_BOOLEAN:
  320. printer->Print(variables_,
  321. "result = result && (get$capitalized_name$()\n"
  322. " == other.get$capitalized_name$());\n");
  323. break;
  324. case JAVATYPE_FLOAT:
  325. printer->Print(variables_,
  326. "result = result && (Float.floatToIntBits(get$capitalized_name$())"
  327. " == Float.floatToIntBits(other.get$capitalized_name$()));\n");
  328. break;
  329. case JAVATYPE_DOUBLE:
  330. printer->Print(variables_,
  331. "result = result && (Double.doubleToLongBits(get$capitalized_name$())"
  332. " == Double.doubleToLongBits(other.get$capitalized_name$()));\n");
  333. break;
  334. case JAVATYPE_STRING:
  335. case JAVATYPE_BYTES:
  336. printer->Print(variables_,
  337. "result = result && get$capitalized_name$()\n"
  338. " .equals(other.get$capitalized_name$());\n");
  339. break;
  340. case JAVATYPE_ENUM:
  341. case JAVATYPE_MESSAGE:
  342. default:
  343. GOOGLE_LOG(FATAL) << "Can't get here.";
  344. break;
  345. }
  346. }
  347. void PrimitiveFieldGenerator::
  348. GenerateHashCode(io::Printer* printer) const {
  349. printer->Print(variables_,
  350. "hash = (37 * hash) + $constant_name$;\n");
  351. switch (GetJavaType(descriptor_)) {
  352. case JAVATYPE_INT:
  353. printer->Print(variables_,
  354. "hash = (53 * hash) + get$capitalized_name$();\n");
  355. break;
  356. case JAVATYPE_LONG:
  357. printer->Print(variables_,
  358. "hash = (53 * hash) + hashLong(get$capitalized_name$());\n");
  359. break;
  360. case JAVATYPE_BOOLEAN:
  361. printer->Print(variables_,
  362. "hash = (53 * hash) + hashBoolean(get$capitalized_name$());\n");
  363. break;
  364. case JAVATYPE_FLOAT:
  365. printer->Print(variables_,
  366. "hash = (53 * hash) + Float.floatToIntBits(\n"
  367. " get$capitalized_name$());\n");
  368. break;
  369. case JAVATYPE_DOUBLE:
  370. printer->Print(variables_,
  371. "hash = (53 * hash) + hashLong(\n"
  372. " Double.doubleToLongBits(get$capitalized_name$()));\n");
  373. break;
  374. case JAVATYPE_STRING:
  375. case JAVATYPE_BYTES:
  376. printer->Print(variables_,
  377. "hash = (53 * hash) + get$capitalized_name$().hashCode();\n");
  378. break;
  379. case JAVATYPE_ENUM:
  380. case JAVATYPE_MESSAGE:
  381. default:
  382. GOOGLE_LOG(FATAL) << "Can't get here.";
  383. break;
  384. }
  385. }
  386. string PrimitiveFieldGenerator::GetBoxedType() const {
  387. return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
  388. }
  389. // ===================================================================
  390. RepeatedPrimitiveFieldGenerator::
  391. RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor,
  392. int messageBitIndex,
  393. int builderBitIndex)
  394. : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
  395. builderBitIndex_(builderBitIndex) {
  396. SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
  397. &variables_);
  398. }
  399. RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {}
  400. int RepeatedPrimitiveFieldGenerator::GetNumBitsForMessage() const {
  401. return 0;
  402. }
  403. int RepeatedPrimitiveFieldGenerator::GetNumBitsForBuilder() const {
  404. return 1;
  405. }
  406. void RepeatedPrimitiveFieldGenerator::
  407. GenerateInterfaceMembers(io::Printer* printer) const {
  408. printer->Print(variables_,
  409. "$deprecation$java.util.List<$boxed_type$> get$capitalized_name$List();\n"
  410. "$deprecation$int get$capitalized_name$Count();\n"
  411. "$deprecation$$type$ get$capitalized_name$(int index);\n");
  412. }
  413. void RepeatedPrimitiveFieldGenerator::
  414. GenerateMembers(io::Printer* printer) const {
  415. printer->Print(variables_,
  416. "private $field_list_type$ $name$_;\n"
  417. "$deprecation$public java.util.List<$boxed_type$>\n"
  418. " get$capitalized_name$List() {\n"
  419. " return $name$_;\n" // note: unmodifiable list
  420. "}\n"
  421. "$deprecation$public int get$capitalized_name$Count() {\n"
  422. " return $name$_.size();\n"
  423. "}\n"
  424. "$deprecation$public $type$ get$capitalized_name$(int index) {\n"
  425. " return $name$_.get(index);\n"
  426. "}\n");
  427. if (descriptor_->options().packed() &&
  428. HasGeneratedMethods(descriptor_->containing_type())) {
  429. printer->Print(variables_,
  430. "private int $name$MemoizedSerializedSize = -1;\n");
  431. }
  432. }
  433. void RepeatedPrimitiveFieldGenerator::
  434. GenerateBuilderMembers(io::Printer* printer) const {
  435. // One field is the list and the bit field keeps track of whether the
  436. // list is immutable. If it's immutable, the invariant is that it must
  437. // either an instance of Collections.emptyList() or it's an ArrayList
  438. // wrapped in a Collections.unmodifiableList() wrapper and nobody else has
  439. // a refererence to the underlying ArrayList. This invariant allows us to
  440. // share instances of lists between protocol buffers avoiding expensive
  441. // memory allocations. Note, immutable is a strong guarantee here -- not
  442. // just that the list cannot be modified via the reference but that the
  443. // list can never be modified.
  444. printer->Print(variables_,
  445. "private $field_list_type$ $name$_ = $empty_list$;\n");
  446. printer->Print(variables_,
  447. "private void ensure$capitalized_name$IsMutable() {\n"
  448. " if (!$get_mutable_bit_builder$) {\n"
  449. " $name$_ = new java.util.ArrayList<$boxed_type$>($name$_);\n"
  450. " $set_mutable_bit_builder$;\n"
  451. " }\n"
  452. "}\n");
  453. // Note: We return an unmodifiable list because otherwise the caller
  454. // could hold on to the returned list and modify it after the message
  455. // has been built, thus mutating the message which is supposed to be
  456. // immutable.
  457. printer->Print(variables_,
  458. "$deprecation$public java.util.List<$boxed_type$>\n"
  459. " get$capitalized_name$List() {\n"
  460. " return java.util.Collections.unmodifiableList($name$_);\n"
  461. "}\n"
  462. "$deprecation$public int get$capitalized_name$Count() {\n"
  463. " return $name$_.size();\n"
  464. "}\n"
  465. "$deprecation$public $type$ get$capitalized_name$(int index) {\n"
  466. " return $name$_.get(index);\n"
  467. "}\n"
  468. "$deprecation$public Builder set$capitalized_name$(\n"
  469. " int index, $type$ value) {\n"
  470. "$null_check$"
  471. " ensure$capitalized_name$IsMutable();\n"
  472. " $name$_.set(index, value);\n"
  473. " $on_changed$\n"
  474. " return this;\n"
  475. "}\n"
  476. "$deprecation$public Builder add$capitalized_name$($type$ value) {\n"
  477. "$null_check$"
  478. " ensure$capitalized_name$IsMutable();\n"
  479. " $name$_.add(value);\n"
  480. " $on_changed$\n"
  481. " return this;\n"
  482. "}\n"
  483. "$deprecation$public Builder addAll$capitalized_name$(\n"
  484. " java.lang.Iterable<? extends $boxed_type$> values) {\n"
  485. " ensure$capitalized_name$IsMutable();\n"
  486. " super.addAll(values, $name$_);\n"
  487. " $on_changed$\n"
  488. " return this;\n"
  489. "}\n"
  490. "$deprecation$public Builder clear$capitalized_name$() {\n"
  491. " $name$_ = $empty_list$;\n"
  492. " $clear_mutable_bit_builder$;\n"
  493. " $on_changed$\n"
  494. " return this;\n"
  495. "}\n");
  496. }
  497. void RepeatedPrimitiveFieldGenerator::
  498. GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
  499. // noop for primitives
  500. }
  501. void RepeatedPrimitiveFieldGenerator::
  502. GenerateInitializationCode(io::Printer* printer) const {
  503. printer->Print(variables_, "$name$_ = $empty_list$;\n");
  504. }
  505. void RepeatedPrimitiveFieldGenerator::
  506. GenerateBuilderClearCode(io::Printer* printer) const {
  507. printer->Print(variables_,
  508. "$name$_ = $empty_list$;\n"
  509. "$clear_mutable_bit_builder$;\n");
  510. }
  511. void RepeatedPrimitiveFieldGenerator::
  512. GenerateMergingCode(io::Printer* printer) const {
  513. // The code below does two optimizations:
  514. // 1. If the other list is empty, there's nothing to do. This ensures we
  515. // don't allocate a new array if we already have an immutable one.
  516. // 2. If the other list is non-empty and our current list is empty, we can
  517. // reuse the other list which is guaranteed to be immutable.
  518. printer->Print(variables_,
  519. "if (!other.$name$_.isEmpty()) {\n"
  520. " if ($name$_.isEmpty()) {\n"
  521. " $name$_ = other.$name$_;\n"
  522. " $clear_mutable_bit_builder$;\n"
  523. " } else {\n"
  524. " ensure$capitalized_name$IsMutable();\n"
  525. " $name$_.addAll(other.$name$_);\n"
  526. " }\n"
  527. " $on_changed$\n"
  528. "}\n");
  529. }
  530. void RepeatedPrimitiveFieldGenerator::
  531. GenerateBuildingCode(io::Printer* printer) const {
  532. // The code below ensures that the result has an immutable list. If our
  533. // list is immutable, we can just reuse it. If not, we make it immutable.
  534. printer->Print(variables_,
  535. "if ($get_mutable_bit_builder$) {\n"
  536. " $name$_ = java.util.Collections.unmodifiableList($name$_);\n"
  537. " $clear_mutable_bit_builder$;\n"
  538. "}\n"
  539. "result.$name$_ = $name$_;\n");
  540. }
  541. void RepeatedPrimitiveFieldGenerator::
  542. GenerateParsingCode(io::Printer* printer) const {
  543. printer->Print(variables_,
  544. "ensure$capitalized_name$IsMutable();\n"
  545. "$name$_.add(input.read$capitalized_type$());\n");
  546. }
  547. void RepeatedPrimitiveFieldGenerator::
  548. GenerateParsingCodeFromPacked(io::Printer* printer) const {
  549. printer->Print(variables_,
  550. "int length = input.readRawVarint32();\n"
  551. "int limit = input.pushLimit(length);\n"
  552. "while (input.getBytesUntilLimit() > 0) {\n"
  553. " add$capitalized_name$(input.read$capitalized_type$());\n"
  554. "}\n"
  555. "input.popLimit(limit);\n");
  556. }
  557. void RepeatedPrimitiveFieldGenerator::
  558. GenerateSerializationCode(io::Printer* printer) const {
  559. if (descriptor_->options().packed()) {
  560. printer->Print(variables_,
  561. "if (get$capitalized_name$List().size() > 0) {\n"
  562. " output.writeRawVarint32($tag$);\n"
  563. " output.writeRawVarint32($name$MemoizedSerializedSize);\n"
  564. "}\n"
  565. "for (int i = 0; i < $name$_.size(); i++) {\n"
  566. " output.write$capitalized_type$NoTag($name$_.get(i));\n"
  567. "}\n");
  568. } else {
  569. printer->Print(variables_,
  570. "for (int i = 0; i < $name$_.size(); i++) {\n"
  571. " output.write$capitalized_type$($number$, $name$_.get(i));\n"
  572. "}\n");
  573. }
  574. }
  575. void RepeatedPrimitiveFieldGenerator::
  576. GenerateSerializedSizeCode(io::Printer* printer) const {
  577. printer->Print(variables_,
  578. "{\n"
  579. " int dataSize = 0;\n");
  580. printer->Indent();
  581. if (FixedSize(GetType(descriptor_)) == -1) {
  582. printer->Print(variables_,
  583. "for (int i = 0; i < $name$_.size(); i++) {\n"
  584. " dataSize += com.google.protobuf.CodedOutputStream\n"
  585. " .compute$capitalized_type$SizeNoTag($name$_.get(i));\n"
  586. "}\n");
  587. } else {
  588. printer->Print(variables_,
  589. "dataSize = $fixed_size$ * get$capitalized_name$List().size();\n");
  590. }
  591. printer->Print(
  592. "size += dataSize;\n");
  593. if (descriptor_->options().packed()) {
  594. printer->Print(variables_,
  595. "if (!get$capitalized_name$List().isEmpty()) {\n"
  596. " size += $tag_size$;\n"
  597. " size += com.google.protobuf.CodedOutputStream\n"
  598. " .computeInt32SizeNoTag(dataSize);\n"
  599. "}\n");
  600. } else {
  601. printer->Print(variables_,
  602. "size += $tag_size$ * get$capitalized_name$List().size();\n");
  603. }
  604. // cache the data size for packed fields.
  605. if (descriptor_->options().packed()) {
  606. printer->Print(variables_,
  607. "$name$MemoizedSerializedSize = dataSize;\n");
  608. }
  609. printer->Outdent();
  610. printer->Print("}\n");
  611. }
  612. void RepeatedPrimitiveFieldGenerator::
  613. GenerateEqualsCode(io::Printer* printer) const {
  614. printer->Print(variables_,
  615. "result = result && get$capitalized_name$List()\n"
  616. " .equals(other.get$capitalized_name$List());\n");
  617. }
  618. void RepeatedPrimitiveFieldGenerator::
  619. GenerateHashCode(io::Printer* printer) const {
  620. printer->Print(variables_,
  621. "if (get$capitalized_name$Count() > 0) {\n"
  622. " hash = (37 * hash) + $constant_name$;\n"
  623. " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n"
  624. "}\n");
  625. }
  626. string RepeatedPrimitiveFieldGenerator::GetBoxedType() const {
  627. return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
  628. }
  629. } // namespace java
  630. } // namespace compiler
  631. } // namespace protobuf
  632. } // namespace google