javanano_primitive_field.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. if (params.use_reference_types_for_primitives()
  219. && !descriptor->is_repeated()) {
  220. (*variables)["type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor));
  221. } else {
  222. (*variables)["type"] = PrimitiveTypeName(GetJavaType(descriptor));
  223. }
  224. (*variables)["default"] = DefaultValue(params, descriptor);
  225. (*variables)["default_constant"] = FieldDefaultConstantName(descriptor);
  226. // For C++-string types (string and bytes), we might need to have
  227. // the generated code do the unicode decoding (see comments in
  228. // InternalNano.java for gory details.). We would like to do this
  229. // once into a "private static final" field and re-use that from
  230. // then on.
  231. if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_STRING &&
  232. !descriptor->default_value_string().empty() &&
  233. !params.use_reference_types_for_primitives()) {
  234. string default_value;
  235. if (descriptor->type() == FieldDescriptor::TYPE_BYTES) {
  236. default_value = strings::Substitute(
  237. "com.google.protobuf.nano.InternalNano.bytesDefaultValue(\"$0\")",
  238. CEscape(descriptor->default_value_string()));
  239. (*variables)["default_copy_if_needed"] = (*variables)["default"] + ".clone()";
  240. } else {
  241. if (AllAscii(descriptor->default_value_string())) {
  242. // All chars are ASCII. In this case CEscape() works fine.
  243. default_value = "\"" + CEscape(descriptor->default_value_string()) + "\"";
  244. } else {
  245. default_value = strings::Substitute(
  246. "com.google.protobuf.nano.InternalNano.stringDefaultValue(\"$0\")",
  247. CEscape(descriptor->default_value_string()));
  248. }
  249. (*variables)["default_copy_if_needed"] = (*variables)["default"];
  250. }
  251. (*variables)["default_constant_value"] = default_value;
  252. } else {
  253. (*variables)["default_copy_if_needed"] = (*variables)["default"];
  254. }
  255. (*variables)["boxed_type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor));
  256. (*variables)["capitalized_type"] = GetCapitalizedType(descriptor);
  257. (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor));
  258. (*variables)["tag_size"] = SimpleItoa(
  259. WireFormat::TagSize(descriptor->number(), descriptor->type()));
  260. int fixed_size = FixedSize(descriptor->type());
  261. if (fixed_size != -1) {
  262. (*variables)["fixed_size"] = SimpleItoa(fixed_size);
  263. }
  264. (*variables)["message_name"] = descriptor->containing_type()->name();
  265. (*variables)["empty_array_name"] = EmptyArrayName(params, descriptor);
  266. }
  267. } // namespace
  268. // ===================================================================
  269. PrimitiveFieldGenerator::
  270. PrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
  271. : FieldGenerator(params), descriptor_(descriptor) {
  272. SetPrimitiveVariables(descriptor, params, &variables_);
  273. }
  274. PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
  275. void PrimitiveFieldGenerator::
  276. GenerateMembers(io::Printer* printer) const {
  277. if (variables_.find("default_constant_value") != variables_.end()) {
  278. // Those primitive types that need a saved default.
  279. printer->Print(variables_,
  280. "private static final $type$ $default_constant$ = $default_constant_value$;\n");
  281. }
  282. printer->Print(variables_,
  283. "public $type$ $name$;\n");
  284. if (params_.generate_has()) {
  285. printer->Print(variables_,
  286. "public boolean has$capitalized_name$ = false;\n");
  287. }
  288. }
  289. void PrimitiveFieldGenerator::
  290. GenerateClearCode(io::Printer* printer) const {
  291. printer->Print(variables_,
  292. "$name$ = $default_copy_if_needed$;\n");
  293. if (params_.generate_has()) {
  294. printer->Print(variables_,
  295. "has$capitalized_name$ = false;\n");
  296. }
  297. }
  298. void PrimitiveFieldGenerator::
  299. GenerateMergingCode(io::Printer* printer) const {
  300. printer->Print(variables_,
  301. "this.$name$ = input.read$capitalized_type$();\n");
  302. if (params_.generate_has()) {
  303. printer->Print(variables_,
  304. "has$capitalized_name$ = true;\n");
  305. }
  306. }
  307. void PrimitiveFieldGenerator::
  308. GenerateSerializationConditional(io::Printer* printer) const {
  309. if (params_.use_reference_types_for_primitives()) {
  310. // For reference type mode, serialize based on equality
  311. // to null.
  312. printer->Print(variables_,
  313. "if (this.$name$ != null) {\n");
  314. return;
  315. }
  316. if (params_.generate_has()) {
  317. printer->Print(variables_,
  318. "if (has$capitalized_name$ || ");
  319. } else {
  320. printer->Print(variables_,
  321. "if (");
  322. }
  323. if (IsArrayType(GetJavaType(descriptor_))) {
  324. printer->Print(variables_,
  325. "!java.util.Arrays.equals(this.$name$, $default$)) {\n");
  326. } else if (IsReferenceType(GetJavaType(descriptor_))) {
  327. printer->Print(variables_,
  328. "!this.$name$.equals($default$)) {\n");
  329. } else if (IsDefaultNaN(descriptor_)) {
  330. printer->Print(variables_,
  331. "!$capitalized_type$.isNaN(this.$name$)) {\n");
  332. } else {
  333. printer->Print(variables_,
  334. "this.$name$ != $default$) {\n");
  335. }
  336. }
  337. void PrimitiveFieldGenerator::
  338. GenerateSerializationCode(io::Printer* printer) const {
  339. if (descriptor_->is_required()) {
  340. printer->Print(variables_,
  341. "output.write$capitalized_type$($number$, this.$name$);\n");
  342. } else {
  343. GenerateSerializationConditional(printer);
  344. printer->Print(variables_,
  345. " output.write$capitalized_type$($number$, this.$name$);\n"
  346. "}\n");
  347. }
  348. }
  349. void PrimitiveFieldGenerator::
  350. GenerateSerializedSizeCode(io::Printer* printer) const {
  351. if (descriptor_->is_required()) {
  352. printer->Print(variables_,
  353. "size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
  354. " .compute$capitalized_type$Size($number$, this.$name$);\n");
  355. } else {
  356. GenerateSerializationConditional(printer);
  357. printer->Print(variables_,
  358. " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
  359. " .compute$capitalized_type$Size($number$, this.$name$);\n"
  360. "}\n");
  361. }
  362. }
  363. string PrimitiveFieldGenerator::GetBoxedType() const {
  364. return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
  365. }
  366. // ===================================================================
  367. AccessorPrimitiveFieldGenerator::
  368. AccessorPrimitiveFieldGenerator(const FieldDescriptor* descriptor,
  369. const Params& params, int has_bit_index)
  370. : FieldGenerator(params), descriptor_(descriptor) {
  371. SetPrimitiveVariables(descriptor, params, &variables_);
  372. SetBitOperationVariables("has", has_bit_index, &variables_);
  373. }
  374. AccessorPrimitiveFieldGenerator::~AccessorPrimitiveFieldGenerator() {}
  375. void AccessorPrimitiveFieldGenerator::
  376. GenerateMembers(io::Printer* printer) const {
  377. if (variables_.find("default_constant_value") != variables_.end()) {
  378. printer->Print(variables_,
  379. "private static final $type$ $default_constant$ = $default_constant_value$;\n");
  380. }
  381. printer->Print(variables_,
  382. "private $type$ $name$_;\n"
  383. "public $type$ get$capitalized_name$() {\n"
  384. " return $name$_;\n"
  385. "}\n"
  386. "public void set$capitalized_name$($type$ value) {\n");
  387. if (IsReferenceType(GetJavaType(descriptor_))) {
  388. printer->Print(variables_,
  389. " if (value == null) throw new java.lang.NullPointerException();\n");
  390. }
  391. printer->Print(variables_,
  392. " $name$_ = value;\n"
  393. " $set_has$;\n"
  394. "}\n"
  395. "public boolean has$capitalized_name$() {\n"
  396. " return $get_has$;\n"
  397. "}\n"
  398. "public void clear$capitalized_name$() {\n"
  399. " $name$_ = $default_copy_if_needed$;\n"
  400. " $clear_has$;\n"
  401. "}\n");
  402. }
  403. void AccessorPrimitiveFieldGenerator::
  404. GenerateClearCode(io::Printer* printer) const {
  405. printer->Print(variables_,
  406. "$name$_ = $default_copy_if_needed$;\n");
  407. }
  408. void AccessorPrimitiveFieldGenerator::
  409. GenerateMergingCode(io::Printer* printer) const {
  410. printer->Print(variables_,
  411. "set$capitalized_name$(input.read$capitalized_type$());\n");
  412. }
  413. void AccessorPrimitiveFieldGenerator::
  414. GenerateSerializationCode(io::Printer* printer) const {
  415. printer->Print(variables_,
  416. "if (has$capitalized_name$()) {\n"
  417. " output.write$capitalized_type$($number$, $name$_);\n"
  418. "}\n");
  419. }
  420. void AccessorPrimitiveFieldGenerator::
  421. GenerateSerializedSizeCode(io::Printer* printer) const {
  422. printer->Print(variables_,
  423. "if (has$capitalized_name$()) {\n"
  424. " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
  425. " .compute$capitalized_type$Size($number$, $name$_);\n"
  426. "}\n");
  427. }
  428. string AccessorPrimitiveFieldGenerator::GetBoxedType() const {
  429. return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
  430. }
  431. // ===================================================================
  432. RepeatedPrimitiveFieldGenerator::
  433. RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
  434. : FieldGenerator(params), descriptor_(descriptor) {
  435. SetPrimitiveVariables(descriptor, params, &variables_);
  436. }
  437. RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {}
  438. void RepeatedPrimitiveFieldGenerator::
  439. GenerateMembers(io::Printer* printer) const {
  440. printer->Print(variables_,
  441. "public $type$[] $name$;\n");
  442. }
  443. void RepeatedPrimitiveFieldGenerator::
  444. GenerateClearCode(io::Printer* printer) const {
  445. printer->Print(variables_,
  446. "$name$ = $default$;\n");
  447. }
  448. void RepeatedPrimitiveFieldGenerator::
  449. GenerateMergingCode(io::Printer* printer) const {
  450. // First, figure out the length of the array, then parse.
  451. if (descriptor_->options().packed()) {
  452. printer->Print(variables_,
  453. "int length = input.readRawVarint32();\n"
  454. "int limit = input.pushLimit(length);\n"
  455. "// First pass to compute array length.\n"
  456. "int arrayLength = 0;\n"
  457. "int startPos = input.getPosition();\n"
  458. "while (input.getBytesUntilLimit() > 0) {\n"
  459. " input.read$capitalized_type$();\n"
  460. " arrayLength++;\n"
  461. "}\n"
  462. "input.rewindToPosition(startPos);\n"
  463. "int i = this.$name$ == null ? 0 : this.$name$.length;\n"
  464. "$type$[] newArray = new $type$[i + arrayLength];\n"
  465. "if (i != 0) {\n"
  466. " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
  467. "}\n"
  468. "for (; i < newArray.length; i++) {\n"
  469. " newArray[i] = input.read$capitalized_type$();\n"
  470. "}\n"
  471. "this.$name$ = newArray;\n"
  472. "input.popLimit(limit);\n");
  473. } else {
  474. printer->Print(variables_,
  475. "int arrayLength = com.google.protobuf.nano.WireFormatNano\n"
  476. " .getRepeatedFieldArrayLength(input, $tag$);\n"
  477. "int i = this.$name$ == null ? 0 : this.$name$.length;\n");
  478. if (GetJavaType(descriptor_) == JAVATYPE_BYTES) {
  479. printer->Print(variables_,
  480. "byte[][] newArray = new byte[i + arrayLength][];\n");
  481. } else {
  482. printer->Print(variables_,
  483. "$type$[] newArray = new $type$[i + arrayLength];\n");
  484. }
  485. printer->Print(variables_,
  486. "if (i != 0) {\n"
  487. " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
  488. "}\n"
  489. "for (; i < newArray.length - 1; i++) {\n"
  490. " newArray[i] = input.read$capitalized_type$();\n"
  491. " input.readTag();\n"
  492. "}\n"
  493. "// Last one without readTag.\n"
  494. "newArray[i] = input.read$capitalized_type$();\n"
  495. "this.$name$ = newArray;\n");
  496. }
  497. }
  498. void RepeatedPrimitiveFieldGenerator::
  499. GenerateRepeatedDataSizeCode(io::Printer* printer) const {
  500. // Creates a variable dataSize and puts the serialized size in
  501. // there.
  502. if (FixedSize(descriptor_->type()) == -1) {
  503. printer->Print(variables_,
  504. "int dataSize = 0;\n"
  505. "for ($type$ element : this.$name$) {\n"
  506. " dataSize += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
  507. " .compute$capitalized_type$SizeNoTag(element);\n"
  508. "}\n");
  509. } else {
  510. printer->Print(variables_,
  511. "int dataSize = $fixed_size$ * this.$name$.length;\n");
  512. }
  513. }
  514. void RepeatedPrimitiveFieldGenerator::
  515. GenerateSerializationCode(io::Printer* printer) const {
  516. printer->Print(variables_,
  517. "if (this.$name$ != null && this.$name$.length > 0) {\n");
  518. printer->Indent();
  519. if (descriptor_->options().packed()) {
  520. GenerateRepeatedDataSizeCode(printer);
  521. printer->Print(variables_,
  522. "output.writeRawVarint32($tag$);\n"
  523. "output.writeRawVarint32(dataSize);\n"
  524. "for ($type$ element : this.$name$) {\n"
  525. " output.write$capitalized_type$NoTag(element);\n"
  526. "}\n");
  527. } else {
  528. printer->Print(variables_,
  529. "for ($type$ element : this.$name$) {\n"
  530. " output.write$capitalized_type$($number$, element);\n"
  531. "}\n");
  532. }
  533. printer->Outdent();
  534. printer->Print("}\n");
  535. }
  536. void RepeatedPrimitiveFieldGenerator::
  537. GenerateSerializedSizeCode(io::Printer* printer) const {
  538. printer->Print(variables_,
  539. "if (this.$name$ != null && this.$name$.length > 0) {\n");
  540. printer->Indent();
  541. GenerateRepeatedDataSizeCode(printer);
  542. printer->Print(
  543. "size += dataSize;\n");
  544. if (descriptor_->options().packed()) {
  545. printer->Print(variables_,
  546. "size += $tag_size$;\n"
  547. "size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
  548. " .computeRawVarint32Size(dataSize);\n");
  549. } else {
  550. printer->Print(variables_,
  551. "size += $tag_size$ * this.$name$.length;\n");
  552. }
  553. printer->Outdent();
  554. printer->Print(
  555. "}\n");
  556. }
  557. string RepeatedPrimitiveFieldGenerator::GetBoxedType() const {
  558. return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
  559. }
  560. } // namespace javanano
  561. } // namespace compiler
  562. } // namespace protobuf
  563. } // namespace google