javanano_primitive_field.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 <math.h>
  35. #include <string>
  36. #include <google/protobuf/compiler/javanano/javanano_primitive_field.h>
  37. #include <google/protobuf/stubs/common.h>
  38. #include <google/protobuf/compiler/javanano/javanano_helpers.h>
  39. #include <google/protobuf/io/printer.h>
  40. #include <google/protobuf/wire_format.h>
  41. #include <google/protobuf/stubs/strutil.h>
  42. #include <google/protobuf/stubs/substitute.h>
  43. namespace google {
  44. namespace protobuf {
  45. namespace compiler {
  46. namespace javanano {
  47. using internal::WireFormat;
  48. using internal::WireFormatLite;
  49. namespace {
  50. const char* PrimitiveTypeName(JavaType type) {
  51. switch (type) {
  52. case JAVATYPE_INT : return "int";
  53. case JAVATYPE_LONG : return "long";
  54. case JAVATYPE_FLOAT : return "float";
  55. case JAVATYPE_DOUBLE : return "double";
  56. case JAVATYPE_BOOLEAN: return "boolean";
  57. case JAVATYPE_STRING : return "java.lang.String";
  58. case JAVATYPE_BYTES : return "byte[]";
  59. case JAVATYPE_ENUM : return NULL;
  60. case JAVATYPE_MESSAGE: return NULL;
  61. // No default because we want the compiler to complain if any new
  62. // JavaTypes are added.
  63. }
  64. GOOGLE_LOG(FATAL) << "Can't get here.";
  65. return NULL;
  66. }
  67. bool IsReferenceType(JavaType type) {
  68. switch (type) {
  69. case JAVATYPE_INT : return false;
  70. case JAVATYPE_LONG : return false;
  71. case JAVATYPE_FLOAT : return false;
  72. case JAVATYPE_DOUBLE : return false;
  73. case JAVATYPE_BOOLEAN: return false;
  74. case JAVATYPE_STRING : return true;
  75. case JAVATYPE_BYTES : return true;
  76. case JAVATYPE_ENUM : return false;
  77. case JAVATYPE_MESSAGE: return true;
  78. // No default because we want the compiler to complain if any new
  79. // JavaTypes are added.
  80. }
  81. GOOGLE_LOG(FATAL) << "Can't get here.";
  82. return false;
  83. }
  84. bool IsArrayType(JavaType type) {
  85. switch (type) {
  86. case JAVATYPE_INT : return false;
  87. case JAVATYPE_LONG : return false;
  88. case JAVATYPE_FLOAT : return false;
  89. case JAVATYPE_DOUBLE : return false;
  90. case JAVATYPE_BOOLEAN: return false;
  91. case JAVATYPE_STRING : return false;
  92. case JAVATYPE_BYTES : return true;
  93. case JAVATYPE_ENUM : return false;
  94. case JAVATYPE_MESSAGE: return false;
  95. // No default because we want the compiler to complain if any new
  96. // JavaTypes are added.
  97. }
  98. GOOGLE_LOG(FATAL) << "Can't get here.";
  99. return false;
  100. }
  101. const char* GetCapitalizedType(const FieldDescriptor* field) {
  102. switch (field->type()) {
  103. case FieldDescriptor::TYPE_INT32 : return "Int32" ;
  104. case FieldDescriptor::TYPE_UINT32 : return "UInt32" ;
  105. case FieldDescriptor::TYPE_SINT32 : return "SInt32" ;
  106. case FieldDescriptor::TYPE_FIXED32 : return "Fixed32" ;
  107. case FieldDescriptor::TYPE_SFIXED32: return "SFixed32";
  108. case FieldDescriptor::TYPE_INT64 : return "Int64" ;
  109. case FieldDescriptor::TYPE_UINT64 : return "UInt64" ;
  110. case FieldDescriptor::TYPE_SINT64 : return "SInt64" ;
  111. case FieldDescriptor::TYPE_FIXED64 : return "Fixed64" ;
  112. case FieldDescriptor::TYPE_SFIXED64: return "SFixed64";
  113. case FieldDescriptor::TYPE_FLOAT : return "Float" ;
  114. case FieldDescriptor::TYPE_DOUBLE : return "Double" ;
  115. case FieldDescriptor::TYPE_BOOL : return "Bool" ;
  116. case FieldDescriptor::TYPE_STRING : return "String" ;
  117. case FieldDescriptor::TYPE_BYTES : return "Bytes" ;
  118. case FieldDescriptor::TYPE_ENUM : return "Enum" ;
  119. case FieldDescriptor::TYPE_GROUP : return "Group" ;
  120. case FieldDescriptor::TYPE_MESSAGE : return "Message" ;
  121. // No default because we want the compiler to complain if any new
  122. // types are added.
  123. }
  124. GOOGLE_LOG(FATAL) << "Can't get here.";
  125. return NULL;
  126. }
  127. // For encodings with fixed sizes, returns that size in bytes. Otherwise
  128. // returns -1.
  129. int FixedSize(FieldDescriptor::Type type) {
  130. switch (type) {
  131. case FieldDescriptor::TYPE_INT32 : return -1;
  132. case FieldDescriptor::TYPE_INT64 : return -1;
  133. case FieldDescriptor::TYPE_UINT32 : return -1;
  134. case FieldDescriptor::TYPE_UINT64 : return -1;
  135. case FieldDescriptor::TYPE_SINT32 : return -1;
  136. case FieldDescriptor::TYPE_SINT64 : return -1;
  137. case FieldDescriptor::TYPE_FIXED32 : return WireFormatLite::kFixed32Size;
  138. case FieldDescriptor::TYPE_FIXED64 : return WireFormatLite::kFixed64Size;
  139. case FieldDescriptor::TYPE_SFIXED32: return WireFormatLite::kSFixed32Size;
  140. case FieldDescriptor::TYPE_SFIXED64: return WireFormatLite::kSFixed64Size;
  141. case FieldDescriptor::TYPE_FLOAT : return WireFormatLite::kFloatSize;
  142. case FieldDescriptor::TYPE_DOUBLE : return WireFormatLite::kDoubleSize;
  143. case FieldDescriptor::TYPE_BOOL : return WireFormatLite::kBoolSize;
  144. case FieldDescriptor::TYPE_ENUM : return -1;
  145. case FieldDescriptor::TYPE_STRING : return -1;
  146. case FieldDescriptor::TYPE_BYTES : return -1;
  147. case FieldDescriptor::TYPE_GROUP : return -1;
  148. case FieldDescriptor::TYPE_MESSAGE : return -1;
  149. // No default because we want the compiler to complain if any new
  150. // types are added.
  151. }
  152. GOOGLE_LOG(FATAL) << "Can't get here.";
  153. return -1;
  154. }
  155. // Returns true if the field has a default value equal to NaN.
  156. bool IsDefaultNaN(const FieldDescriptor* field) {
  157. switch (field->type()) {
  158. case FieldDescriptor::TYPE_INT32 : return false;
  159. case FieldDescriptor::TYPE_UINT32 : return false;
  160. case FieldDescriptor::TYPE_SINT32 : return false;
  161. case FieldDescriptor::TYPE_FIXED32 : return false;
  162. case FieldDescriptor::TYPE_SFIXED32: return false;
  163. case FieldDescriptor::TYPE_INT64 : return false;
  164. case FieldDescriptor::TYPE_UINT64 : return false;
  165. case FieldDescriptor::TYPE_SINT64 : return false;
  166. case FieldDescriptor::TYPE_FIXED64 : return false;
  167. case FieldDescriptor::TYPE_SFIXED64: return false;
  168. case FieldDescriptor::TYPE_FLOAT :
  169. return isnan(field->default_value_float());
  170. case FieldDescriptor::TYPE_DOUBLE :
  171. return isnan(field->default_value_double());
  172. case FieldDescriptor::TYPE_BOOL : return false;
  173. case FieldDescriptor::TYPE_STRING : return false;
  174. case FieldDescriptor::TYPE_BYTES : return false;
  175. case FieldDescriptor::TYPE_ENUM : return false;
  176. case FieldDescriptor::TYPE_GROUP : return false;
  177. case FieldDescriptor::TYPE_MESSAGE : return false;
  178. // No default because we want the compiler to complain if any new
  179. // types are added.
  180. }
  181. GOOGLE_LOG(FATAL) << "Can't get here.";
  182. return false;
  183. }
  184. // Return true if the type is a that has variable length
  185. // for instance String's.
  186. bool IsVariableLenType(JavaType type) {
  187. switch (type) {
  188. case JAVATYPE_INT : return false;
  189. case JAVATYPE_LONG : return false;
  190. case JAVATYPE_FLOAT : return false;
  191. case JAVATYPE_DOUBLE : return false;
  192. case JAVATYPE_BOOLEAN: return false;
  193. case JAVATYPE_STRING : return true;
  194. case JAVATYPE_BYTES : return true;
  195. case JAVATYPE_ENUM : return false;
  196. case JAVATYPE_MESSAGE: return true;
  197. // No default because we want the compiler to complain if any new
  198. // JavaTypes are added.
  199. }
  200. GOOGLE_LOG(FATAL) << "Can't get here.";
  201. return false;
  202. }
  203. bool AllAscii(const string& text) {
  204. for (int i = 0; i < text.size(); i++) {
  205. if ((text[i] & 0x80) != 0) {
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. void SetPrimitiveVariables(const FieldDescriptor* descriptor, const Params params,
  212. map<string, string>* variables) {
  213. (*variables)["name"] =
  214. RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
  215. (*variables)["capitalized_name"] =
  216. RenameJavaKeywords(UnderscoresToCapitalizedCamelCase(descriptor));
  217. (*variables)["number"] = SimpleItoa(descriptor->number());
  218. (*variables)["type"] = PrimitiveTypeName(GetJavaType(descriptor));
  219. (*variables)["default"] = DefaultValue(params, descriptor);
  220. (*variables)["default_constant"] = FieldDefaultConstantName(descriptor);
  221. // For C++-string types (string and bytes), we might need to have
  222. // the generated code do the unicode decoding (see comments in
  223. // InternalNano.java for gory details.). We would like to do this
  224. // once into a "private static final" field and re-use that from
  225. // then on.
  226. if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_STRING &&
  227. !descriptor->default_value_string().empty()) {
  228. string default_value;
  229. if (descriptor->type() == FieldDescriptor::TYPE_BYTES) {
  230. default_value = strings::Substitute(
  231. "com.google.protobuf.nano.InternalNano.bytesDefaultValue(\"$0\")",
  232. CEscape(descriptor->default_value_string()));
  233. (*variables)["default_copy_if_needed"] = (*variables)["default"] + ".clone()";
  234. } else {
  235. if (AllAscii(descriptor->default_value_string())) {
  236. // All chars are ASCII. In this case CEscape() works fine.
  237. default_value = "\"" + CEscape(descriptor->default_value_string()) + "\"";
  238. } else {
  239. default_value = strings::Substitute(
  240. "com.google.protobuf.nano.InternalNano.stringDefaultValue(\"$0\")",
  241. CEscape(descriptor->default_value_string()));
  242. }
  243. (*variables)["default_copy_if_needed"] = (*variables)["default"];
  244. }
  245. (*variables)["default_constant_value"] = default_value;
  246. } else {
  247. (*variables)["default_copy_if_needed"] = (*variables)["default"];
  248. }
  249. (*variables)["boxed_type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor));
  250. (*variables)["capitalized_type"] = GetCapitalizedType(descriptor);
  251. (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor));
  252. (*variables)["tag_size"] = SimpleItoa(
  253. WireFormat::TagSize(descriptor->number(), descriptor->type()));
  254. int fixed_size = FixedSize(descriptor->type());
  255. if (fixed_size != -1) {
  256. (*variables)["fixed_size"] = SimpleItoa(fixed_size);
  257. }
  258. (*variables)["message_name"] = descriptor->containing_type()->name();
  259. (*variables)["empty_array_name"] = EmptyArrayName(params, descriptor);
  260. }
  261. } // namespace
  262. // ===================================================================
  263. PrimitiveFieldGenerator::
  264. PrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
  265. : FieldGenerator(params), descriptor_(descriptor) {
  266. SetPrimitiveVariables(descriptor, params, &variables_);
  267. }
  268. PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
  269. void PrimitiveFieldGenerator::
  270. GenerateMembers(io::Printer* printer) const {
  271. if (variables_.find("default_constant_value") != variables_.end()) {
  272. // Those primitive types that need a saved default.
  273. printer->Print(variables_,
  274. "private static final $type$ $default_constant$ = $default_constant_value$;\n");
  275. }
  276. printer->Print(variables_,
  277. "public $type$ $name$ = $default_copy_if_needed$;\n");
  278. if (params_.generate_has()) {
  279. printer->Print(variables_,
  280. "public boolean has$capitalized_name$ = false;\n");
  281. }
  282. }
  283. void PrimitiveFieldGenerator::
  284. GenerateClearCode(io::Printer* printer) const {
  285. printer->Print(variables_,
  286. "$name$ = $default_copy_if_needed$;\n");
  287. if (params_.generate_has()) {
  288. printer->Print(variables_,
  289. "has$capitalized_name$ = false;\n");
  290. }
  291. }
  292. void PrimitiveFieldGenerator::
  293. GenerateMergingCode(io::Printer* printer) const {
  294. printer->Print(variables_,
  295. "this.$name$ = input.read$capitalized_type$();\n");
  296. if (params_.generate_has()) {
  297. printer->Print(variables_,
  298. "has$capitalized_name$ = true;\n");
  299. }
  300. }
  301. void PrimitiveFieldGenerator::
  302. GenerateSerializationConditional(io::Printer* printer) const {
  303. if (params_.generate_has()) {
  304. printer->Print(variables_,
  305. "if (has$capitalized_name$ || ");
  306. } else {
  307. printer->Print(variables_,
  308. "if (");
  309. }
  310. if (IsArrayType(GetJavaType(descriptor_))) {
  311. printer->Print(variables_,
  312. "!java.util.Arrays.equals(this.$name$, $default$)) {\n");
  313. } else if (IsReferenceType(GetJavaType(descriptor_))) {
  314. printer->Print(variables_,
  315. "!this.$name$.equals($default$)) {\n");
  316. } else if (IsDefaultNaN(descriptor_)) {
  317. printer->Print(variables_,
  318. "!$capitalized_type$.isNaN(this.$name$)) {\n");
  319. } else {
  320. printer->Print(variables_,
  321. "this.$name$ != $default$) {\n");
  322. }
  323. }
  324. void PrimitiveFieldGenerator::
  325. GenerateSerializationCode(io::Printer* printer) const {
  326. if (descriptor_->is_required()) {
  327. printer->Print(variables_,
  328. "output.write$capitalized_type$($number$, this.$name$);\n");
  329. } else {
  330. GenerateSerializationConditional(printer);
  331. printer->Print(variables_,
  332. " output.write$capitalized_type$($number$, this.$name$);\n"
  333. "}\n");
  334. }
  335. }
  336. void PrimitiveFieldGenerator::
  337. GenerateSerializedSizeCode(io::Printer* printer) const {
  338. if (descriptor_->is_required()) {
  339. printer->Print(variables_,
  340. "size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
  341. " .compute$capitalized_type$Size($number$, this.$name$);\n");
  342. } else {
  343. GenerateSerializationConditional(printer);
  344. printer->Print(variables_,
  345. " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
  346. " .compute$capitalized_type$Size($number$, this.$name$);\n"
  347. "}\n");
  348. }
  349. }
  350. string PrimitiveFieldGenerator::GetBoxedType() const {
  351. return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
  352. }
  353. // ===================================================================
  354. AccessorPrimitiveFieldGenerator::
  355. AccessorPrimitiveFieldGenerator(const FieldDescriptor* descriptor,
  356. const Params& params, int has_bit_index)
  357. : FieldGenerator(params), descriptor_(descriptor) {
  358. SetPrimitiveVariables(descriptor, params, &variables_);
  359. SetBitOperationVariables("has", has_bit_index, &variables_);
  360. }
  361. AccessorPrimitiveFieldGenerator::~AccessorPrimitiveFieldGenerator() {}
  362. void AccessorPrimitiveFieldGenerator::
  363. GenerateMembers(io::Printer* printer) const {
  364. if (variables_.find("default_constant_value") != variables_.end()) {
  365. printer->Print(variables_,
  366. "private static final $type$ $default_constant$ = $default_constant_value$;\n");
  367. }
  368. printer->Print(variables_,
  369. "private $type$ $name$_ = $default_copy_if_needed$;\n"
  370. "public $type$ get$capitalized_name$() {\n"
  371. " return $name$_;\n"
  372. "}\n"
  373. "public void set$capitalized_name$($type$ value) {\n");
  374. if (IsReferenceType(GetJavaType(descriptor_))) {
  375. printer->Print(variables_,
  376. " if (value == null) throw new java.lang.NullPointerException();\n");
  377. }
  378. printer->Print(variables_,
  379. " $name$_ = value;\n"
  380. " $set_has$;\n"
  381. "}\n"
  382. "public boolean has$capitalized_name$() {\n"
  383. " return $get_has$;\n"
  384. "}\n"
  385. "public void clear$capitalized_name$() {\n"
  386. " $name$_ = $default_copy_if_needed$;\n"
  387. " $clear_has$;\n"
  388. "}\n");
  389. }
  390. void AccessorPrimitiveFieldGenerator::
  391. GenerateClearCode(io::Printer* printer) const {
  392. printer->Print(variables_,
  393. "$name$_ = $default_copy_if_needed$;\n");
  394. }
  395. void AccessorPrimitiveFieldGenerator::
  396. GenerateMergingCode(io::Printer* printer) const {
  397. printer->Print(variables_,
  398. "set$capitalized_name$(input.read$capitalized_type$());\n");
  399. }
  400. void AccessorPrimitiveFieldGenerator::
  401. GenerateSerializationCode(io::Printer* printer) const {
  402. printer->Print(variables_,
  403. "if (has$capitalized_name$()) {\n"
  404. " output.write$capitalized_type$($number$, $name$_);\n"
  405. "}\n");
  406. }
  407. void AccessorPrimitiveFieldGenerator::
  408. GenerateSerializedSizeCode(io::Printer* printer) const {
  409. printer->Print(variables_,
  410. "if (has$capitalized_name$()) {\n"
  411. " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
  412. " .compute$capitalized_type$Size($number$, $name$_);\n"
  413. "}\n");
  414. }
  415. string AccessorPrimitiveFieldGenerator::GetBoxedType() const {
  416. return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
  417. }
  418. // ===================================================================
  419. RepeatedPrimitiveFieldGenerator::
  420. RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
  421. : FieldGenerator(params), descriptor_(descriptor) {
  422. SetPrimitiveVariables(descriptor, params, &variables_);
  423. }
  424. RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {}
  425. void RepeatedPrimitiveFieldGenerator::
  426. GenerateMembers(io::Printer* printer) const {
  427. printer->Print(variables_,
  428. "public $type$[] $name$ = $default$;\n");
  429. }
  430. void RepeatedPrimitiveFieldGenerator::
  431. GenerateClearCode(io::Printer* printer) const {
  432. printer->Print(variables_,
  433. "$name$ = $default$;\n");
  434. }
  435. void RepeatedPrimitiveFieldGenerator::
  436. GenerateMergingCode(io::Printer* printer) const {
  437. // First, figure out the length of the array, then parse.
  438. if (descriptor_->options().packed()) {
  439. printer->Print(variables_,
  440. "int length = input.readRawVarint32();\n"
  441. "int limit = input.pushLimit(length);\n"
  442. "// First pass to compute array length.\n"
  443. "int arrayLength = 0;\n"
  444. "int startPos = input.getPosition();\n"
  445. "while (input.getBytesUntilLimit() > 0) {\n"
  446. " input.read$capitalized_type$();\n"
  447. " arrayLength++;\n"
  448. "}\n"
  449. "input.rewindToPosition(startPos);\n"
  450. "this.$name$ = new $type$[arrayLength];\n"
  451. "for (int i = 0; i < arrayLength; i++) {\n"
  452. " this.$name$[i] = input.read$capitalized_type$();\n"
  453. "}\n"
  454. "input.popLimit(limit);\n");
  455. } else {
  456. printer->Print(variables_,
  457. "int arrayLength = com.google.protobuf.nano.WireFormatNano.getRepeatedFieldArrayLength(input, $tag$);\n"
  458. "int i = this.$name$.length;\n");
  459. if (GetJavaType(descriptor_) == JAVATYPE_BYTES) {
  460. printer->Print(variables_,
  461. "byte[][] newArray = new byte[i + arrayLength][];\n"
  462. "System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
  463. "this.$name$ = newArray;\n");
  464. } else {
  465. printer->Print(variables_,
  466. "$type$[] newArray = new $type$[i + arrayLength];\n"
  467. "System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
  468. "this.$name$ = newArray;\n");
  469. }
  470. printer->Print(variables_,
  471. "for (; i < this.$name$.length - 1; i++) {\n"
  472. " this.$name$[i] = input.read$capitalized_type$();\n"
  473. " input.readTag();\n"
  474. "}\n"
  475. "// Last one without readTag.\n"
  476. "this.$name$[i] = input.read$capitalized_type$();\n");
  477. }
  478. }
  479. void RepeatedPrimitiveFieldGenerator::
  480. GenerateRepeatedDataSizeCode(io::Printer* printer) const {
  481. // Creates a variable dataSize and puts the serialized size in
  482. // there.
  483. if (FixedSize(descriptor_->type()) == -1) {
  484. printer->Print(variables_,
  485. "int dataSize = 0;\n"
  486. "for ($type$ element : this.$name$) {\n"
  487. " dataSize += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
  488. " .compute$capitalized_type$SizeNoTag(element);\n"
  489. "}\n");
  490. } else {
  491. printer->Print(variables_,
  492. "int dataSize = $fixed_size$ * this.$name$.length;\n");
  493. }
  494. }
  495. void RepeatedPrimitiveFieldGenerator::
  496. GenerateSerializationCode(io::Printer* printer) const {
  497. if (descriptor_->options().packed()) {
  498. printer->Print(variables_,
  499. "if (this.$name$.length > 0) {\n");
  500. printer->Indent();
  501. GenerateRepeatedDataSizeCode(printer);
  502. printer->Outdent();
  503. printer->Print(variables_,
  504. " output.writeRawVarint32($tag$);\n"
  505. " output.writeRawVarint32(dataSize);\n"
  506. "}\n");
  507. printer->Print(variables_,
  508. "for ($type$ element : this.$name$) {\n"
  509. " output.write$capitalized_type$NoTag(element);\n"
  510. "}\n");
  511. } else {
  512. printer->Print(variables_,
  513. "for ($type$ element : this.$name$) {\n"
  514. " output.write$capitalized_type$($number$, element);\n"
  515. "}\n");
  516. }
  517. }
  518. void RepeatedPrimitiveFieldGenerator::
  519. GenerateSerializedSizeCode(io::Printer* printer) const {
  520. printer->Print(variables_,
  521. "if (this.$name$.length > 0) {\n");
  522. printer->Indent();
  523. GenerateRepeatedDataSizeCode(printer);
  524. printer->Print(
  525. "size += dataSize;\n");
  526. if (descriptor_->options().packed()) {
  527. printer->Print(variables_,
  528. "size += $tag_size$;\n"
  529. "size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
  530. " .computeRawVarint32Size(dataSize);\n");
  531. } else {
  532. printer->Print(variables_,
  533. "size += $tag_size$ * this.$name$.length;\n");
  534. }
  535. printer->Outdent();
  536. printer->Print(
  537. "}\n");
  538. }
  539. string RepeatedPrimitiveFieldGenerator::GetBoxedType() const {
  540. return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
  541. }
  542. } // namespace javanano
  543. } // namespace compiler
  544. } // namespace protobuf
  545. } // namespace google