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

http://github.com/tomahawk-player/tomahawk · C++ · 610 lines · 444 code · 70 blank · 96 comment · 6 complexity · f4445d1e0f785976c51b48c10b3fb4c1 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. // Author: jonp@google.com (Jon Perlow)
  32. // Based on original Protocol Buffers design by
  33. // Sanjay Ghemawat, Jeff Dean, and others.
  34. #include <map>
  35. #include <string>
  36. #include <google/protobuf/compiler/java/java_string_field.h>
  37. #include <google/protobuf/stubs/common.h>
  38. #include <google/protobuf/compiler/java/java_helpers.h>
  39. #include <google/protobuf/io/printer.h>
  40. #include <google/protobuf/wire_format.h>
  41. #include <google/protobuf/stubs/strutil.h>
  42. namespace google {
  43. namespace protobuf {
  44. namespace compiler {
  45. namespace java {
  46. using internal::WireFormat;
  47. using internal::WireFormatLite;
  48. namespace {
  49. void SetPrimitiveVariables(const FieldDescriptor* descriptor,
  50. int messageBitIndex,
  51. int builderBitIndex,
  52. map<string, string>* variables) {
  53. (*variables)["name"] =
  54. UnderscoresToCamelCase(descriptor);
  55. (*variables)["capitalized_name"] =
  56. UnderscoresToCapitalizedCamelCase(descriptor);
  57. (*variables)["constant_name"] = FieldConstantName(descriptor);
  58. (*variables)["number"] = SimpleItoa(descriptor->number());
  59. (*variables)["empty_list"] = "com.google.protobuf.LazyStringArrayList.EMPTY";
  60. (*variables)["default"] = DefaultValue(descriptor);
  61. (*variables)["default_init"] = ("= " + DefaultValue(descriptor));
  62. (*variables)["capitalized_type"] = "String";
  63. (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor));
  64. (*variables)["tag_size"] = SimpleItoa(
  65. WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
  66. (*variables)["null_check"] =
  67. " if (value == null) {\n"
  68. " throw new NullPointerException();\n"
  69. " }\n";
  70. // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
  71. // by the proto compiler
  72. (*variables)["deprecation"] = descriptor->options().deprecated()
  73. ? "@java.lang.Deprecated " : "";
  74. (*variables)["on_changed"] =
  75. HasDescriptorMethods(descriptor->containing_type()) ? "onChanged();" : "";
  76. // For singular messages and builders, one bit is used for the hasField bit.
  77. (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
  78. (*variables)["get_has_field_bit_builder"] = GenerateGetBit(builderBitIndex);
  79. (*variables)["set_has_field_bit_builder"] = GenerateSetBit(builderBitIndex);
  80. (*variables)["clear_has_field_bit_builder"] =
  81. GenerateClearBit(builderBitIndex);
  82. // For repated builders, one bit is used for whether the array is immutable.
  83. (*variables)["get_mutable_bit_builder"] = GenerateGetBit(builderBitIndex);
  84. (*variables)["set_mutable_bit_builder"] = GenerateSetBit(builderBitIndex);
  85. (*variables)["clear_mutable_bit_builder"] = GenerateClearBit(builderBitIndex);
  86. (*variables)["get_has_field_bit_from_local"] =
  87. GenerateGetBitFromLocal(builderBitIndex);
  88. (*variables)["set_has_field_bit_to_local"] =
  89. GenerateSetBitToLocal(messageBitIndex);
  90. }
  91. } // namespace
  92. // ===================================================================
  93. StringFieldGenerator::
  94. StringFieldGenerator(const FieldDescriptor* descriptor,
  95. int messageBitIndex,
  96. int builderBitIndex)
  97. : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
  98. builderBitIndex_(builderBitIndex) {
  99. SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
  100. &variables_);
  101. }
  102. StringFieldGenerator::~StringFieldGenerator() {}
  103. int StringFieldGenerator::GetNumBitsForMessage() const {
  104. return 1;
  105. }
  106. int StringFieldGenerator::GetNumBitsForBuilder() const {
  107. return 1;
  108. }
  109. // A note about how strings are handled. This code used to just store a String
  110. // in the Message. This had two issues:
  111. //
  112. // 1. It wouldn't roundtrip byte arrays that were not vaid UTF-8 encoded
  113. // strings, but rather fields that were raw bytes incorrectly marked
  114. // as strings in the proto file. This is common because in the proto1
  115. // syntax, string was the way to indicate bytes and C++ engineers can
  116. // easily make this mistake without affecting the C++ API. By converting to
  117. // strings immediately, some java code might corrupt these byte arrays as
  118. // it passes through a java server even if the field was never accessed by
  119. // application code.
  120. //
  121. // 2. There's a performance hit to converting between bytes and strings and
  122. // it many cases, the field is never even read by the application code. This
  123. // avoids unnecessary conversions in the common use cases.
  124. //
  125. // So now, the field for String is maintained as an Object reference which can
  126. // either store a String or a ByteString. The code uses an instanceof check
  127. // to see which one it has and converts to the other one if needed. It remembers
  128. // the last value requested (in a thread safe manner) as this is most likely
  129. // the one needed next. The thread safety is such that if two threads both
  130. // convert the field because the changes made by each thread were not visible to
  131. // the other, they may cause a conversion to happen more times than would
  132. // otherwise be necessary. This was deemed better than adding synchronization
  133. // overhead. It will not cause any corruption issues or affect the behavior of
  134. // the API. The instanceof check is also highly optimized in the JVM and we
  135. // decided it was better to reduce the memory overhead by not having two
  136. // separate fields but rather use dynamic type checking.
  137. //
  138. // For single fields, the logic for this is done inside the generated code. For
  139. // repeated fields, the logic is done in LazyStringArrayList and
  140. // UnmodifiableLazyStringList.
  141. void StringFieldGenerator::
  142. GenerateInterfaceMembers(io::Printer* printer) const {
  143. printer->Print(variables_,
  144. "$deprecation$boolean has$capitalized_name$();\n"
  145. "$deprecation$java.lang.String get$capitalized_name$();\n");
  146. }
  147. void StringFieldGenerator::
  148. GenerateMembers(io::Printer* printer) const {
  149. printer->Print(variables_,
  150. "private java.lang.Object $name$_;\n"
  151. "$deprecation$public boolean has$capitalized_name$() {\n"
  152. " return $get_has_field_bit_message$;\n"
  153. "}\n");
  154. printer->Print(variables_,
  155. "$deprecation$public java.lang.String get$capitalized_name$() {\n"
  156. " java.lang.Object ref = $name$_;\n"
  157. " if (ref instanceof java.lang.String) {\n"
  158. " return (java.lang.String) ref;\n"
  159. " } else {\n"
  160. " com.google.protobuf.ByteString bs = \n"
  161. " (com.google.protobuf.ByteString) ref;\n"
  162. " java.lang.String s = bs.toStringUtf8();\n"
  163. " if (com.google.protobuf.Internal.isValidUtf8(bs)) {\n"
  164. " $name$_ = s;\n"
  165. " }\n"
  166. " return s;\n"
  167. " }\n"
  168. "}\n"
  169. "private com.google.protobuf.ByteString get$capitalized_name$Bytes() {\n"
  170. " java.lang.Object ref = $name$_;\n"
  171. " if (ref instanceof java.lang.String) {\n"
  172. " com.google.protobuf.ByteString b = \n"
  173. " com.google.protobuf.ByteString.copyFromUtf8(\n"
  174. " (java.lang.String) ref);\n"
  175. " $name$_ = b;\n"
  176. " return b;\n"
  177. " } else {\n"
  178. " return (com.google.protobuf.ByteString) ref;\n"
  179. " }\n"
  180. "}\n");
  181. }
  182. void StringFieldGenerator::
  183. GenerateBuilderMembers(io::Printer* printer) const {
  184. printer->Print(variables_,
  185. "private java.lang.Object $name$_ $default_init$;\n"
  186. "$deprecation$public boolean has$capitalized_name$() {\n"
  187. " return $get_has_field_bit_builder$;\n"
  188. "}\n");
  189. printer->Print(variables_,
  190. "$deprecation$public java.lang.String get$capitalized_name$() {\n"
  191. " java.lang.Object ref = $name$_;\n"
  192. " if (!(ref instanceof java.lang.String)) {\n"
  193. " java.lang.String s = ((com.google.protobuf.ByteString) ref)\n"
  194. " .toStringUtf8();\n"
  195. " $name$_ = s;\n"
  196. " return s;\n"
  197. " } else {\n"
  198. " return (java.lang.String) ref;\n"
  199. " }\n"
  200. "}\n");
  201. printer->Print(variables_,
  202. "$deprecation$public Builder set$capitalized_name$(\n"
  203. " java.lang.String value) {\n"
  204. "$null_check$"
  205. " $set_has_field_bit_builder$;\n"
  206. " $name$_ = value;\n"
  207. " $on_changed$\n"
  208. " return this;\n"
  209. "}\n"
  210. "$deprecation$public Builder clear$capitalized_name$() {\n"
  211. " $clear_has_field_bit_builder$;\n");
  212. // The default value is not a simple literal so we want to avoid executing
  213. // it multiple times. Instead, get the default out of the default instance.
  214. printer->Print(variables_,
  215. " $name$_ = getDefaultInstance().get$capitalized_name$();\n");
  216. printer->Print(variables_,
  217. " $on_changed$\n"
  218. " return this;\n"
  219. "}\n");
  220. printer->Print(variables_,
  221. "void set$capitalized_name$(com.google.protobuf.ByteString value) {\n"
  222. " $set_has_field_bit_builder$;\n"
  223. " $name$_ = value;\n"
  224. " $on_changed$\n"
  225. "}\n");
  226. }
  227. void StringFieldGenerator::
  228. GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
  229. // noop for primitives
  230. }
  231. void StringFieldGenerator::
  232. GenerateInitializationCode(io::Printer* printer) const {
  233. printer->Print(variables_, "$name$_ = $default$;\n");
  234. }
  235. void StringFieldGenerator::
  236. GenerateBuilderClearCode(io::Printer* printer) const {
  237. printer->Print(variables_,
  238. "$name$_ = $default$;\n"
  239. "$clear_has_field_bit_builder$;\n");
  240. }
  241. void StringFieldGenerator::
  242. GenerateMergingCode(io::Printer* printer) const {
  243. printer->Print(variables_,
  244. "if (other.has$capitalized_name$()) {\n"
  245. " set$capitalized_name$(other.get$capitalized_name$());\n"
  246. "}\n");
  247. }
  248. void StringFieldGenerator::
  249. GenerateBuildingCode(io::Printer* printer) const {
  250. printer->Print(variables_,
  251. "if ($get_has_field_bit_from_local$) {\n"
  252. " $set_has_field_bit_to_local$;\n"
  253. "}\n"
  254. "result.$name$_ = $name$_;\n");
  255. }
  256. void StringFieldGenerator::
  257. GenerateParsingCode(io::Printer* printer) const {
  258. printer->Print(variables_,
  259. "$set_has_field_bit_builder$;\n"
  260. "$name$_ = input.readBytes();\n");
  261. }
  262. void StringFieldGenerator::
  263. GenerateSerializationCode(io::Printer* printer) const {
  264. printer->Print(variables_,
  265. "if ($get_has_field_bit_message$) {\n"
  266. " output.writeBytes($number$, get$capitalized_name$Bytes());\n"
  267. "}\n");
  268. }
  269. void StringFieldGenerator::
  270. GenerateSerializedSizeCode(io::Printer* printer) const {
  271. printer->Print(variables_,
  272. "if ($get_has_field_bit_message$) {\n"
  273. " size += com.google.protobuf.CodedOutputStream\n"
  274. " .computeBytesSize($number$, get$capitalized_name$Bytes());\n"
  275. "}\n");
  276. }
  277. void StringFieldGenerator::
  278. GenerateEqualsCode(io::Printer* printer) const {
  279. printer->Print(variables_,
  280. "result = result && get$capitalized_name$()\n"
  281. " .equals(other.get$capitalized_name$());\n");
  282. }
  283. void StringFieldGenerator::
  284. GenerateHashCode(io::Printer* printer) const {
  285. printer->Print(variables_,
  286. "hash = (37 * hash) + $constant_name$;\n");
  287. printer->Print(variables_,
  288. "hash = (53 * hash) + get$capitalized_name$().hashCode();\n");
  289. }
  290. string StringFieldGenerator::GetBoxedType() const {
  291. return "java.lang.String";
  292. }
  293. // ===================================================================
  294. RepeatedStringFieldGenerator::
  295. RepeatedStringFieldGenerator(const FieldDescriptor* descriptor,
  296. int messageBitIndex,
  297. int builderBitIndex)
  298. : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
  299. builderBitIndex_(builderBitIndex) {
  300. SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
  301. &variables_);
  302. }
  303. RepeatedStringFieldGenerator::~RepeatedStringFieldGenerator() {}
  304. int RepeatedStringFieldGenerator::GetNumBitsForMessage() const {
  305. return 0;
  306. }
  307. int RepeatedStringFieldGenerator::GetNumBitsForBuilder() const {
  308. return 1;
  309. }
  310. void RepeatedStringFieldGenerator::
  311. GenerateInterfaceMembers(io::Printer* printer) const {
  312. printer->Print(variables_,
  313. "$deprecation$java.util.List<java.lang.String>\n"
  314. " get$capitalized_name$List();\n"
  315. "$deprecation$int get$capitalized_name$Count();\n"
  316. "$deprecation$java.lang.String get$capitalized_name$(int index);\n");
  317. }
  318. void RepeatedStringFieldGenerator::
  319. GenerateMembers(io::Printer* printer) const {
  320. printer->Print(variables_,
  321. "private com.google.protobuf.LazyStringList $name$_;\n"
  322. "$deprecation$public java.util.List<java.lang.String>\n"
  323. " get$capitalized_name$List() {\n"
  324. " return $name$_;\n" // note: unmodifiable list
  325. "}\n"
  326. "$deprecation$public int get$capitalized_name$Count() {\n"
  327. " return $name$_.size();\n"
  328. "}\n"
  329. "$deprecation$public java.lang.String get$capitalized_name$(int index) {\n"
  330. " return $name$_.get(index);\n"
  331. "}\n");
  332. if (descriptor_->options().packed() &&
  333. HasGeneratedMethods(descriptor_->containing_type())) {
  334. printer->Print(variables_,
  335. "private int $name$MemoizedSerializedSize = -1;\n");
  336. }
  337. }
  338. void RepeatedStringFieldGenerator::
  339. GenerateBuilderMembers(io::Printer* printer) const {
  340. // One field is the list and the bit field keeps track of whether the
  341. // list is immutable. If it's immutable, the invariant is that it must
  342. // either an instance of Collections.emptyList() or it's an ArrayList
  343. // wrapped in a Collections.unmodifiableList() wrapper and nobody else has
  344. // a refererence to the underlying ArrayList. This invariant allows us to
  345. // share instances of lists between protocol buffers avoiding expensive
  346. // memory allocations. Note, immutable is a strong guarantee here -- not
  347. // just that the list cannot be modified via the reference but that the
  348. // list can never be modified.
  349. printer->Print(variables_,
  350. "private com.google.protobuf.LazyStringList $name$_ = $empty_list$;\n");
  351. printer->Print(variables_,
  352. "private void ensure$capitalized_name$IsMutable() {\n"
  353. " if (!$get_mutable_bit_builder$) {\n"
  354. " $name$_ = new com.google.protobuf.LazyStringArrayList($name$_);\n"
  355. " $set_mutable_bit_builder$;\n"
  356. " }\n"
  357. "}\n");
  358. // Note: We return an unmodifiable list because otherwise the caller
  359. // could hold on to the returned list and modify it after the message
  360. // has been built, thus mutating the message which is supposed to be
  361. // immutable.
  362. printer->Print(variables_,
  363. "$deprecation$public java.util.List<java.lang.String>\n"
  364. " get$capitalized_name$List() {\n"
  365. " return java.util.Collections.unmodifiableList($name$_);\n"
  366. "}\n"
  367. "$deprecation$public int get$capitalized_name$Count() {\n"
  368. " return $name$_.size();\n"
  369. "}\n"
  370. "$deprecation$public java.lang.String get$capitalized_name$(int index) {\n"
  371. " return $name$_.get(index);\n"
  372. "}\n"
  373. "$deprecation$public Builder set$capitalized_name$(\n"
  374. " int index, java.lang.String value) {\n"
  375. "$null_check$"
  376. " ensure$capitalized_name$IsMutable();\n"
  377. " $name$_.set(index, value);\n"
  378. " $on_changed$\n"
  379. " return this;\n"
  380. "}\n"
  381. "$deprecation$public Builder add$capitalized_name$(\n"
  382. " java.lang.String value) {\n"
  383. "$null_check$"
  384. " ensure$capitalized_name$IsMutable();\n"
  385. " $name$_.add(value);\n"
  386. " $on_changed$\n"
  387. " return this;\n"
  388. "}\n"
  389. "$deprecation$public Builder addAll$capitalized_name$(\n"
  390. " java.lang.Iterable<java.lang.String> values) {\n"
  391. " ensure$capitalized_name$IsMutable();\n"
  392. " super.addAll(values, $name$_);\n"
  393. " $on_changed$\n"
  394. " return this;\n"
  395. "}\n"
  396. "$deprecation$public Builder clear$capitalized_name$() {\n"
  397. " $name$_ = $empty_list$;\n"
  398. " $clear_mutable_bit_builder$;\n"
  399. " $on_changed$\n"
  400. " return this;\n"
  401. "}\n");
  402. printer->Print(variables_,
  403. "void add$capitalized_name$(com.google.protobuf.ByteString value) {\n"
  404. " ensure$capitalized_name$IsMutable();\n"
  405. " $name$_.add(value);\n"
  406. " $on_changed$\n"
  407. "}\n");
  408. }
  409. void RepeatedStringFieldGenerator::
  410. GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
  411. // noop for primitives
  412. }
  413. void RepeatedStringFieldGenerator::
  414. GenerateInitializationCode(io::Printer* printer) const {
  415. printer->Print(variables_, "$name$_ = $empty_list$;\n");
  416. }
  417. void RepeatedStringFieldGenerator::
  418. GenerateBuilderClearCode(io::Printer* printer) const {
  419. printer->Print(variables_,
  420. "$name$_ = $empty_list$;\n"
  421. "$clear_mutable_bit_builder$;\n");
  422. }
  423. void RepeatedStringFieldGenerator::
  424. GenerateMergingCode(io::Printer* printer) const {
  425. // The code below does two optimizations:
  426. // 1. If the other list is empty, there's nothing to do. This ensures we
  427. // don't allocate a new array if we already have an immutable one.
  428. // 2. If the other list is non-empty and our current list is empty, we can
  429. // reuse the other list which is guaranteed to be immutable.
  430. printer->Print(variables_,
  431. "if (!other.$name$_.isEmpty()) {\n"
  432. " if ($name$_.isEmpty()) {\n"
  433. " $name$_ = other.$name$_;\n"
  434. " $clear_mutable_bit_builder$;\n"
  435. " } else {\n"
  436. " ensure$capitalized_name$IsMutable();\n"
  437. " $name$_.addAll(other.$name$_);\n"
  438. " }\n"
  439. " $on_changed$\n"
  440. "}\n");
  441. }
  442. void RepeatedStringFieldGenerator::
  443. GenerateBuildingCode(io::Printer* printer) const {
  444. // The code below ensures that the result has an immutable list. If our
  445. // list is immutable, we can just reuse it. If not, we make it immutable.
  446. printer->Print(variables_,
  447. "if ($get_mutable_bit_builder$) {\n"
  448. " $name$_ = new com.google.protobuf.UnmodifiableLazyStringList(\n"
  449. " $name$_);\n"
  450. " $clear_mutable_bit_builder$;\n"
  451. "}\n"
  452. "result.$name$_ = $name$_;\n");
  453. }
  454. void RepeatedStringFieldGenerator::
  455. GenerateParsingCode(io::Printer* printer) const {
  456. printer->Print(variables_,
  457. "ensure$capitalized_name$IsMutable();\n"
  458. "$name$_.add(input.readBytes());\n");
  459. }
  460. void RepeatedStringFieldGenerator::
  461. GenerateParsingCodeFromPacked(io::Printer* printer) const {
  462. printer->Print(variables_,
  463. "int length = input.readRawVarint32();\n"
  464. "int limit = input.pushLimit(length);\n"
  465. "while (input.getBytesUntilLimit() > 0) {\n"
  466. " add$capitalized_name$(input.read$capitalized_type$());\n"
  467. "}\n"
  468. "input.popLimit(limit);\n");
  469. }
  470. void RepeatedStringFieldGenerator::
  471. GenerateSerializationCode(io::Printer* printer) const {
  472. if (descriptor_->options().packed()) {
  473. printer->Print(variables_,
  474. "if (get$capitalized_name$List().size() > 0) {\n"
  475. " output.writeRawVarint32($tag$);\n"
  476. " output.writeRawVarint32($name$MemoizedSerializedSize);\n"
  477. "}\n"
  478. "for (int i = 0; i < $name$_.size(); i++) {\n"
  479. " output.write$capitalized_type$NoTag($name$_.get(i));\n"
  480. "}\n");
  481. } else {
  482. printer->Print(variables_,
  483. "for (int i = 0; i < $name$_.size(); i++) {\n"
  484. " output.writeBytes($number$, $name$_.getByteString(i));\n"
  485. "}\n");
  486. }
  487. }
  488. void RepeatedStringFieldGenerator::
  489. GenerateSerializedSizeCode(io::Printer* printer) const {
  490. printer->Print(variables_,
  491. "{\n"
  492. " int dataSize = 0;\n");
  493. printer->Indent();
  494. printer->Print(variables_,
  495. "for (int i = 0; i < $name$_.size(); i++) {\n"
  496. " dataSize += com.google.protobuf.CodedOutputStream\n"
  497. " .computeBytesSizeNoTag($name$_.getByteString(i));\n"
  498. "}\n");
  499. printer->Print(
  500. "size += dataSize;\n");
  501. if (descriptor_->options().packed()) {
  502. printer->Print(variables_,
  503. "if (!get$capitalized_name$List().isEmpty()) {\n"
  504. " size += $tag_size$;\n"
  505. " size += com.google.protobuf.CodedOutputStream\n"
  506. " .computeInt32SizeNoTag(dataSize);\n"
  507. "}\n");
  508. } else {
  509. printer->Print(variables_,
  510. "size += $tag_size$ * get$capitalized_name$List().size();\n");
  511. }
  512. // cache the data size for packed fields.
  513. if (descriptor_->options().packed()) {
  514. printer->Print(variables_,
  515. "$name$MemoizedSerializedSize = dataSize;\n");
  516. }
  517. printer->Outdent();
  518. printer->Print("}\n");
  519. }
  520. void RepeatedStringFieldGenerator::
  521. GenerateEqualsCode(io::Printer* printer) const {
  522. printer->Print(variables_,
  523. "result = result && get$capitalized_name$List()\n"
  524. " .equals(other.get$capitalized_name$List());\n");
  525. }
  526. void RepeatedStringFieldGenerator::
  527. GenerateHashCode(io::Printer* printer) const {
  528. printer->Print(variables_,
  529. "if (get$capitalized_name$Count() > 0) {\n"
  530. " hash = (37 * hash) + $constant_name$;\n"
  531. " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n"
  532. "}\n");
  533. }
  534. string RepeatedStringFieldGenerator::GetBoxedType() const {
  535. return "String";
  536. }
  537. } // namespace java
  538. } // namespace compiler
  539. } // namespace protobuf
  540. } // namespace google