java_primitive_field.cc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  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/stubs/logging.h>
  36. #include <google/protobuf/stubs/common.h>
  37. #include <google/protobuf/compiler/java/java_context.h>
  38. #include <google/protobuf/compiler/java/java_doc_comment.h>
  39. #include <google/protobuf/compiler/java/java_helpers.h>
  40. #include <google/protobuf/compiler/java/java_name_resolver.h>
  41. #include <google/protobuf/compiler/java/java_primitive_field.h>
  42. #include <google/protobuf/io/printer.h>
  43. #include <google/protobuf/wire_format.h>
  44. #include <google/protobuf/stubs/strutil.h>
  45. namespace google {
  46. namespace protobuf {
  47. namespace compiler {
  48. namespace java {
  49. using internal::WireFormat;
  50. using internal::WireFormatLite;
  51. namespace {
  52. void SetPrimitiveVariables(const FieldDescriptor* descriptor,
  53. int messageBitIndex,
  54. int builderBitIndex,
  55. const FieldGeneratorInfo* info,
  56. ClassNameResolver* name_resolver,
  57. std::map<string, string>* variables) {
  58. SetCommonFieldVariables(descriptor, info, variables);
  59. (*variables)["type"] = PrimitiveTypeName(GetJavaType(descriptor));
  60. (*variables)["boxed_type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor));
  61. (*variables)["field_type"] = (*variables)["type"];
  62. (*variables)["field_list_type"] = "java.util.List<" +
  63. (*variables)["boxed_type"] + ">";
  64. (*variables)["empty_list"] = "java.util.Collections.emptyList()";
  65. (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver);
  66. (*variables)["default_init"] = IsDefaultValueJavaDefault(descriptor) ?
  67. "" : ("= " + ImmutableDefaultValue(descriptor, name_resolver));
  68. (*variables)["capitalized_type"] =
  69. GetCapitalizedType(descriptor, /* immutable = */ true);
  70. (*variables)["tag"] =
  71. SimpleItoa(static_cast<int32>(WireFormat::MakeTag(descriptor)));
  72. (*variables)["tag_size"] = SimpleItoa(
  73. WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
  74. if (IsReferenceType(GetJavaType(descriptor))) {
  75. (*variables)["null_check"] =
  76. " if (value == null) {\n"
  77. " throw new NullPointerException();\n"
  78. " }\n";
  79. } else {
  80. (*variables)["null_check"] = "";
  81. }
  82. // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
  83. // by the proto compiler
  84. (*variables)["deprecation"] = descriptor->options().deprecated()
  85. ? "@java.lang.Deprecated " : "";
  86. int fixed_size = FixedSize(GetType(descriptor));
  87. if (fixed_size != -1) {
  88. (*variables)["fixed_size"] = SimpleItoa(fixed_size);
  89. }
  90. (*variables)["on_changed"] = "onChanged();";
  91. if (SupportFieldPresence(descriptor->file())) {
  92. // For singular messages and builders, one bit is used for the hasField bit.
  93. (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
  94. (*variables)["get_has_field_bit_builder"] = GenerateGetBit(builderBitIndex);
  95. // Note that these have a trailing ";".
  96. (*variables)["set_has_field_bit_message"] =
  97. GenerateSetBit(messageBitIndex) + ";";
  98. (*variables)["set_has_field_bit_builder"] =
  99. GenerateSetBit(builderBitIndex) + ";";
  100. (*variables)["clear_has_field_bit_builder"] =
  101. GenerateClearBit(builderBitIndex) + ";";
  102. (*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
  103. } else {
  104. (*variables)["set_has_field_bit_message"] = "";
  105. (*variables)["set_has_field_bit_builder"] = "";
  106. (*variables)["clear_has_field_bit_builder"] = "";
  107. if (descriptor->type() == FieldDescriptor::TYPE_BYTES) {
  108. (*variables)["is_field_present_message"] =
  109. "!" + (*variables)["name"] + "_.isEmpty()";
  110. } else {
  111. (*variables)["is_field_present_message"] =
  112. (*variables)["name"] + "_ != " + (*variables)["default"];
  113. }
  114. }
  115. // For repated builders, one bit is used for whether the array is immutable.
  116. (*variables)["get_mutable_bit_builder"] = GenerateGetBit(builderBitIndex);
  117. (*variables)["set_mutable_bit_builder"] = GenerateSetBit(builderBitIndex);
  118. (*variables)["clear_mutable_bit_builder"] = GenerateClearBit(builderBitIndex);
  119. // For repeated fields, one bit is used for whether the array is immutable
  120. // in the parsing constructor.
  121. (*variables)["get_mutable_bit_parser"] =
  122. GenerateGetBitMutableLocal(builderBitIndex);
  123. (*variables)["set_mutable_bit_parser"] =
  124. GenerateSetBitMutableLocal(builderBitIndex);
  125. (*variables)["get_has_field_bit_from_local"] =
  126. GenerateGetBitFromLocal(builderBitIndex);
  127. (*variables)["set_has_field_bit_to_local"] =
  128. GenerateSetBitToLocal(messageBitIndex);
  129. }
  130. } // namespace
  131. // ===================================================================
  132. ImmutablePrimitiveFieldGenerator::
  133. ImmutablePrimitiveFieldGenerator(const FieldDescriptor* descriptor,
  134. int messageBitIndex,
  135. int builderBitIndex,
  136. Context* context)
  137. : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
  138. builderBitIndex_(builderBitIndex), context_(context),
  139. name_resolver_(context->GetNameResolver()) {
  140. SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
  141. context->GetFieldGeneratorInfo(descriptor),
  142. name_resolver_, &variables_);
  143. }
  144. ImmutablePrimitiveFieldGenerator::~ImmutablePrimitiveFieldGenerator() {}
  145. int ImmutablePrimitiveFieldGenerator::GetNumBitsForMessage() const {
  146. return 1;
  147. }
  148. int ImmutablePrimitiveFieldGenerator::GetNumBitsForBuilder() const {
  149. return 1;
  150. }
  151. void ImmutablePrimitiveFieldGenerator::
  152. GenerateInterfaceMembers(io::Printer* printer) const {
  153. if (SupportFieldPresence(descriptor_->file())) {
  154. WriteFieldDocComment(printer, descriptor_);
  155. printer->Print(variables_,
  156. "$deprecation$boolean has$capitalized_name$();\n");
  157. }
  158. WriteFieldDocComment(printer, descriptor_);
  159. printer->Print(variables_,
  160. "$deprecation$$type$ get$capitalized_name$();\n");
  161. }
  162. void ImmutablePrimitiveFieldGenerator::
  163. GenerateMembers(io::Printer* printer) const {
  164. printer->Print(variables_,
  165. "private $field_type$ $name$_;\n");
  166. PrintExtraFieldInfo(variables_, printer);
  167. if (SupportFieldPresence(descriptor_->file())) {
  168. WriteFieldDocComment(printer, descriptor_);
  169. printer->Print(variables_,
  170. "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
  171. " return $get_has_field_bit_message$;\n"
  172. "}\n");
  173. printer->Annotate("{", "}", descriptor_);
  174. }
  175. WriteFieldDocComment(printer, descriptor_);
  176. printer->Print(variables_,
  177. "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n"
  178. " return $name$_;\n"
  179. "}\n");
  180. printer->Annotate("{", "}", descriptor_);
  181. }
  182. void ImmutablePrimitiveFieldGenerator::
  183. GenerateBuilderMembers(io::Printer* printer) const {
  184. printer->Print(variables_,
  185. "private $field_type$ $name$_ $default_init$;\n");
  186. if (SupportFieldPresence(descriptor_->file())) {
  187. WriteFieldDocComment(printer, descriptor_);
  188. printer->Print(variables_,
  189. "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
  190. " return $get_has_field_bit_builder$;\n"
  191. "}\n");
  192. printer->Annotate("{", "}", descriptor_);
  193. }
  194. WriteFieldDocComment(printer, descriptor_);
  195. printer->Print(variables_,
  196. "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n"
  197. " return $name$_;\n"
  198. "}\n");
  199. printer->Annotate("{", "}", descriptor_);
  200. WriteFieldDocComment(printer, descriptor_);
  201. printer->Print(variables_,
  202. "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n"
  203. "$null_check$"
  204. " $set_has_field_bit_builder$\n"
  205. " $name$_ = value;\n"
  206. " $on_changed$\n"
  207. " return this;\n"
  208. "}\n");
  209. printer->Annotate("{", "}", descriptor_);
  210. WriteFieldDocComment(printer, descriptor_);
  211. printer->Print(variables_,
  212. "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
  213. " $clear_has_field_bit_builder$\n");
  214. printer->Annotate("{", "}", descriptor_);
  215. JavaType type = GetJavaType(descriptor_);
  216. if (type == JAVATYPE_STRING || type == JAVATYPE_BYTES) {
  217. // The default value is not a simple literal so we want to avoid executing
  218. // it multiple times. Instead, get the default out of the default instance.
  219. printer->Print(variables_,
  220. " $name$_ = getDefaultInstance().get$capitalized_name$();\n");
  221. } else {
  222. printer->Print(variables_,
  223. " $name$_ = $default$;\n");
  224. }
  225. printer->Print(variables_,
  226. " $on_changed$\n"
  227. " return this;\n"
  228. "}\n");
  229. }
  230. void ImmutablePrimitiveFieldGenerator::
  231. GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
  232. // noop for primitives
  233. }
  234. void ImmutablePrimitiveFieldGenerator::
  235. GenerateInitializationCode(io::Printer* printer) const {
  236. printer->Print(variables_, "$name$_ = $default$;\n");
  237. }
  238. void ImmutablePrimitiveFieldGenerator::
  239. GenerateBuilderClearCode(io::Printer* printer) const {
  240. printer->Print(variables_,
  241. "$name$_ = $default$;\n"
  242. "$clear_has_field_bit_builder$\n");
  243. }
  244. void ImmutablePrimitiveFieldGenerator::
  245. GenerateMergingCode(io::Printer* printer) const {
  246. if (SupportFieldPresence(descriptor_->file())) {
  247. printer->Print(variables_,
  248. "if (other.has$capitalized_name$()) {\n"
  249. " set$capitalized_name$(other.get$capitalized_name$());\n"
  250. "}\n");
  251. } else {
  252. printer->Print(variables_,
  253. "if (other.get$capitalized_name$() != $default$) {\n"
  254. " set$capitalized_name$(other.get$capitalized_name$());\n"
  255. "}\n");
  256. }
  257. }
  258. void ImmutablePrimitiveFieldGenerator::
  259. GenerateBuildingCode(io::Printer* printer) const {
  260. if (SupportFieldPresence(descriptor_->file())) {
  261. printer->Print(variables_,
  262. "if ($get_has_field_bit_from_local$) {\n"
  263. " $set_has_field_bit_to_local$;\n"
  264. "}\n");
  265. }
  266. printer->Print(variables_,
  267. "result.$name$_ = $name$_;\n");
  268. }
  269. void ImmutablePrimitiveFieldGenerator::
  270. GenerateParsingCode(io::Printer* printer) const {
  271. printer->Print(variables_,
  272. "$set_has_field_bit_message$\n"
  273. "$name$_ = input.read$capitalized_type$();\n");
  274. }
  275. void ImmutablePrimitiveFieldGenerator::
  276. GenerateParsingDoneCode(io::Printer* printer) const {
  277. // noop for primitives.
  278. }
  279. void ImmutablePrimitiveFieldGenerator::
  280. GenerateSerializationCode(io::Printer* printer) const {
  281. printer->Print(variables_,
  282. "if ($is_field_present_message$) {\n"
  283. " output.write$capitalized_type$($number$, $name$_);\n"
  284. "}\n");
  285. }
  286. void ImmutablePrimitiveFieldGenerator::
  287. GenerateSerializedSizeCode(io::Printer* printer) const {
  288. printer->Print(variables_,
  289. "if ($is_field_present_message$) {\n"
  290. " size += com.google.protobuf.CodedOutputStream\n"
  291. " .compute$capitalized_type$Size($number$, $name$_);\n"
  292. "}\n");
  293. }
  294. void ImmutablePrimitiveFieldGenerator::
  295. GenerateEqualsCode(io::Printer* printer) const {
  296. switch (GetJavaType(descriptor_)) {
  297. case JAVATYPE_INT:
  298. case JAVATYPE_LONG:
  299. case JAVATYPE_BOOLEAN:
  300. printer->Print(variables_,
  301. "result = result && (get$capitalized_name$()\n"
  302. " == other.get$capitalized_name$());\n");
  303. break;
  304. case JAVATYPE_FLOAT:
  305. printer->Print(variables_,
  306. "result = result && (\n"
  307. " java.lang.Float.floatToIntBits(get$capitalized_name$())\n"
  308. " == java.lang.Float.floatToIntBits(\n"
  309. " other.get$capitalized_name$()));\n");
  310. break;
  311. case JAVATYPE_DOUBLE:
  312. printer->Print(variables_,
  313. "result = result && (\n"
  314. " java.lang.Double.doubleToLongBits(get$capitalized_name$())\n"
  315. " == java.lang.Double.doubleToLongBits(\n"
  316. " other.get$capitalized_name$()));\n");
  317. break;
  318. case JAVATYPE_STRING:
  319. case JAVATYPE_BYTES:
  320. printer->Print(variables_,
  321. "result = result && get$capitalized_name$()\n"
  322. " .equals(other.get$capitalized_name$());\n");
  323. break;
  324. case JAVATYPE_ENUM:
  325. case JAVATYPE_MESSAGE:
  326. default:
  327. GOOGLE_LOG(FATAL) << "Can't get here.";
  328. break;
  329. }
  330. }
  331. void ImmutablePrimitiveFieldGenerator::
  332. GenerateHashCode(io::Printer* printer) const {
  333. printer->Print(variables_,
  334. "hash = (37 * hash) + $constant_name$;\n");
  335. switch (GetJavaType(descriptor_)) {
  336. case JAVATYPE_INT:
  337. printer->Print(variables_,
  338. "hash = (53 * hash) + get$capitalized_name$();\n");
  339. break;
  340. case JAVATYPE_LONG:
  341. printer->Print(variables_,
  342. "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n"
  343. " get$capitalized_name$());\n");
  344. break;
  345. case JAVATYPE_BOOLEAN:
  346. printer->Print(variables_,
  347. "hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean(\n"
  348. " get$capitalized_name$());\n");
  349. break;
  350. case JAVATYPE_FLOAT:
  351. printer->Print(variables_,
  352. "hash = (53 * hash) + java.lang.Float.floatToIntBits(\n"
  353. " get$capitalized_name$());\n");
  354. break;
  355. case JAVATYPE_DOUBLE:
  356. printer->Print(variables_,
  357. "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n"
  358. " java.lang.Double.doubleToLongBits(get$capitalized_name$()));\n");
  359. break;
  360. case JAVATYPE_STRING:
  361. case JAVATYPE_BYTES:
  362. printer->Print(variables_,
  363. "hash = (53 * hash) + get$capitalized_name$().hashCode();\n");
  364. break;
  365. case JAVATYPE_ENUM:
  366. case JAVATYPE_MESSAGE:
  367. default:
  368. GOOGLE_LOG(FATAL) << "Can't get here.";
  369. break;
  370. }
  371. }
  372. string ImmutablePrimitiveFieldGenerator::GetBoxedType() const {
  373. return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
  374. }
  375. // ===================================================================
  376. ImmutablePrimitiveOneofFieldGenerator::
  377. ImmutablePrimitiveOneofFieldGenerator(const FieldDescriptor* descriptor,
  378. int messageBitIndex,
  379. int builderBitIndex,
  380. Context* context)
  381. : ImmutablePrimitiveFieldGenerator(
  382. descriptor, messageBitIndex, builderBitIndex, context) {
  383. const OneofGeneratorInfo* info =
  384. context->GetOneofGeneratorInfo(descriptor->containing_oneof());
  385. SetCommonOneofVariables(descriptor, info, &variables_);
  386. }
  387. ImmutablePrimitiveOneofFieldGenerator::
  388. ~ImmutablePrimitiveOneofFieldGenerator() {}
  389. void ImmutablePrimitiveOneofFieldGenerator::
  390. GenerateMembers(io::Printer* printer) const {
  391. PrintExtraFieldInfo(variables_, printer);
  392. if (SupportFieldPresence(descriptor_->file())) {
  393. WriteFieldDocComment(printer, descriptor_);
  394. printer->Print(variables_,
  395. "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
  396. " return $has_oneof_case_message$;\n"
  397. "}\n");
  398. printer->Annotate("{", "}", descriptor_);
  399. }
  400. WriteFieldDocComment(printer, descriptor_);
  401. printer->Print(variables_,
  402. "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n"
  403. " if ($has_oneof_case_message$) {\n"
  404. " return ($boxed_type$) $oneof_name$_;\n"
  405. " }\n"
  406. " return $default$;\n"
  407. "}\n");
  408. printer->Annotate("{", "}", descriptor_);
  409. }
  410. void ImmutablePrimitiveOneofFieldGenerator::
  411. GenerateBuilderMembers(io::Printer* printer) const {
  412. if (SupportFieldPresence(descriptor_->file())) {
  413. WriteFieldDocComment(printer, descriptor_);
  414. printer->Print(variables_,
  415. "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
  416. " return $has_oneof_case_message$;\n"
  417. "}\n");
  418. printer->Annotate("{", "}", descriptor_);
  419. }
  420. WriteFieldDocComment(printer, descriptor_);
  421. printer->Print(variables_,
  422. "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n"
  423. " if ($has_oneof_case_message$) {\n"
  424. " return ($boxed_type$) $oneof_name$_;\n"
  425. " }\n"
  426. " return $default$;\n"
  427. "}\n");
  428. printer->Annotate("{", "}", descriptor_);
  429. WriteFieldDocComment(printer, descriptor_);
  430. printer->Print(variables_,
  431. "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n"
  432. "$null_check$"
  433. " $set_oneof_case_message$;\n"
  434. " $oneof_name$_ = value;\n"
  435. " $on_changed$\n"
  436. " return this;\n"
  437. "}\n");
  438. printer->Annotate("{", "}", descriptor_);
  439. WriteFieldDocComment(printer, descriptor_);
  440. printer->Print(variables_,
  441. "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
  442. " if ($has_oneof_case_message$) {\n"
  443. " $clear_oneof_case_message$;\n"
  444. " $oneof_name$_ = null;\n"
  445. " $on_changed$\n"
  446. " }\n"
  447. " return this;\n"
  448. "}\n");
  449. printer->Annotate("{", "}", descriptor_);
  450. }
  451. void ImmutablePrimitiveOneofFieldGenerator::
  452. GenerateBuildingCode(io::Printer* printer) const {
  453. printer->Print(variables_,
  454. "if ($has_oneof_case_message$) {\n"
  455. " result.$oneof_name$_ = $oneof_name$_;\n"
  456. "}\n");
  457. }
  458. void ImmutablePrimitiveOneofFieldGenerator::
  459. GenerateMergingCode(io::Printer* printer) const {
  460. printer->Print(variables_,
  461. "set$capitalized_name$(other.get$capitalized_name$());\n");
  462. }
  463. void ImmutablePrimitiveOneofFieldGenerator::
  464. GenerateParsingCode(io::Printer* printer) const {
  465. printer->Print(variables_,
  466. "$set_oneof_case_message$;\n"
  467. "$oneof_name$_ = input.read$capitalized_type$();\n");
  468. }
  469. void ImmutablePrimitiveOneofFieldGenerator::
  470. GenerateSerializationCode(io::Printer* printer) const {
  471. printer->Print(variables_,
  472. "if ($has_oneof_case_message$) {\n"
  473. " output.write$capitalized_type$(\n");
  474. // $type$ and $boxed_type$ is the same for bytes fields so we don't need to
  475. // do redundant casts.
  476. if (GetJavaType(descriptor_) == JAVATYPE_BYTES) {
  477. printer->Print(variables_,
  478. " $number$, ($type$) $oneof_name$_);\n");
  479. } else {
  480. printer->Print(variables_,
  481. " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n");
  482. }
  483. printer->Print(
  484. "}\n");
  485. }
  486. void ImmutablePrimitiveOneofFieldGenerator::
  487. GenerateSerializedSizeCode(io::Printer* printer) const {
  488. printer->Print(variables_,
  489. "if ($has_oneof_case_message$) {\n"
  490. " size += com.google.protobuf.CodedOutputStream\n"
  491. " .compute$capitalized_type$Size(\n");
  492. // $type$ and $boxed_type$ is the same for bytes fields so we don't need to
  493. // do redundant casts.
  494. if (GetJavaType(descriptor_) == JAVATYPE_BYTES) {
  495. printer->Print(variables_,
  496. " $number$, ($type$) $oneof_name$_);\n");
  497. } else {
  498. printer->Print(variables_,
  499. " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n");
  500. }
  501. printer->Print(
  502. "}\n");
  503. }
  504. // ===================================================================
  505. RepeatedImmutablePrimitiveFieldGenerator::
  506. RepeatedImmutablePrimitiveFieldGenerator(const FieldDescriptor* descriptor,
  507. int messageBitIndex,
  508. int builderBitIndex,
  509. Context* context)
  510. : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
  511. builderBitIndex_(builderBitIndex), context_(context),
  512. name_resolver_(context->GetNameResolver()) {
  513. SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
  514. context->GetFieldGeneratorInfo(descriptor),
  515. name_resolver_, &variables_);
  516. }
  517. RepeatedImmutablePrimitiveFieldGenerator::
  518. ~RepeatedImmutablePrimitiveFieldGenerator() {}
  519. int RepeatedImmutablePrimitiveFieldGenerator::GetNumBitsForMessage() const {
  520. return 0;
  521. }
  522. int RepeatedImmutablePrimitiveFieldGenerator::GetNumBitsForBuilder() const {
  523. return 1;
  524. }
  525. void RepeatedImmutablePrimitiveFieldGenerator::
  526. GenerateInterfaceMembers(io::Printer* printer) const {
  527. WriteFieldDocComment(printer, descriptor_);
  528. printer->Print(variables_,
  529. "$deprecation$java.util.List<$boxed_type$> get$capitalized_name$List();\n");
  530. WriteFieldDocComment(printer, descriptor_);
  531. printer->Print(variables_,
  532. "$deprecation$int get$capitalized_name$Count();\n");
  533. WriteFieldDocComment(printer, descriptor_);
  534. printer->Print(variables_,
  535. "$deprecation$$type$ get$capitalized_name$(int index);\n");
  536. }
  537. void RepeatedImmutablePrimitiveFieldGenerator::
  538. GenerateMembers(io::Printer* printer) const {
  539. printer->Print(variables_,
  540. "private $field_list_type$ $name$_;\n");
  541. PrintExtraFieldInfo(variables_, printer);
  542. WriteFieldDocComment(printer, descriptor_);
  543. printer->Print(variables_,
  544. "$deprecation$public java.util.List<$boxed_type$>\n"
  545. " ${$get$capitalized_name$List$}$() {\n"
  546. " return $name$_;\n" // note: unmodifiable list
  547. "}\n");
  548. printer->Annotate("{", "}", descriptor_);
  549. WriteFieldDocComment(printer, descriptor_);
  550. printer->Print(variables_,
  551. "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n"
  552. " return $name$_.size();\n"
  553. "}\n");
  554. printer->Annotate("{", "}", descriptor_);
  555. WriteFieldDocComment(printer, descriptor_);
  556. printer->Print(variables_,
  557. "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n"
  558. " return $name$_.get(index);\n"
  559. "}\n");
  560. printer->Annotate("{", "}", descriptor_);
  561. if (descriptor_->is_packed()) {
  562. printer->Print(variables_,
  563. "private int $name$MemoizedSerializedSize = -1;\n");
  564. }
  565. }
  566. void RepeatedImmutablePrimitiveFieldGenerator::
  567. GenerateBuilderMembers(io::Printer* printer) const {
  568. // One field is the list and the bit field keeps track of whether the
  569. // list is immutable. If it's immutable, the invariant is that it must
  570. // either an instance of Collections.emptyList() or it's an ArrayList
  571. // wrapped in a Collections.unmodifiableList() wrapper and nobody else has
  572. // a refererence to the underlying ArrayList. This invariant allows us to
  573. // share instances of lists between protocol buffers avoiding expensive
  574. // memory allocations. Note, immutable is a strong guarantee here -- not
  575. // just that the list cannot be modified via the reference but that the
  576. // list can never be modified.
  577. printer->Print(variables_,
  578. "private $field_list_type$ $name$_ = $empty_list$;\n");
  579. printer->Print(variables_,
  580. "private void ensure$capitalized_name$IsMutable() {\n"
  581. " if (!$get_mutable_bit_builder$) {\n"
  582. " $name$_ = new java.util.ArrayList<$boxed_type$>($name$_);\n"
  583. " $set_mutable_bit_builder$;\n"
  584. " }\n"
  585. "}\n");
  586. // Note: We return an unmodifiable list because otherwise the caller
  587. // could hold on to the returned list and modify it after the message
  588. // has been built, thus mutating the message which is supposed to be
  589. // immutable.
  590. WriteFieldDocComment(printer, descriptor_);
  591. printer->Print(variables_,
  592. "$deprecation$public java.util.List<$boxed_type$>\n"
  593. " ${$get$capitalized_name$List$}$() {\n"
  594. " return java.util.Collections.unmodifiableList($name$_);\n"
  595. "}\n");
  596. printer->Annotate("{", "}", descriptor_);
  597. WriteFieldDocComment(printer, descriptor_);
  598. printer->Print(variables_,
  599. "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n"
  600. " return $name$_.size();\n"
  601. "}\n");
  602. printer->Annotate("{", "}", descriptor_);
  603. WriteFieldDocComment(printer, descriptor_);
  604. printer->Print(variables_,
  605. "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n"
  606. " return $name$_.get(index);\n"
  607. "}\n");
  608. printer->Annotate("{", "}", descriptor_);
  609. WriteFieldDocComment(printer, descriptor_);
  610. printer->Print(variables_,
  611. "$deprecation$public Builder ${$set$capitalized_name$$}$(\n"
  612. " int index, $type$ value) {\n"
  613. "$null_check$"
  614. " ensure$capitalized_name$IsMutable();\n"
  615. " $name$_.set(index, value);\n"
  616. " $on_changed$\n"
  617. " return this;\n"
  618. "}\n");
  619. printer->Annotate("{", "}", descriptor_);
  620. WriteFieldDocComment(printer, descriptor_);
  621. printer->Print(variables_,
  622. "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n"
  623. "$null_check$"
  624. " ensure$capitalized_name$IsMutable();\n"
  625. " $name$_.add(value);\n"
  626. " $on_changed$\n"
  627. " return this;\n"
  628. "}\n");
  629. printer->Annotate("{", "}", descriptor_);
  630. WriteFieldDocComment(printer, descriptor_);
  631. printer->Print(variables_,
  632. "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n"
  633. " java.lang.Iterable<? extends $boxed_type$> values) {\n"
  634. " ensure$capitalized_name$IsMutable();\n"
  635. " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n"
  636. " values, $name$_);\n"
  637. " $on_changed$\n"
  638. " return this;\n"
  639. "}\n");
  640. printer->Annotate("{", "}", descriptor_);
  641. WriteFieldDocComment(printer, descriptor_);
  642. printer->Print(variables_,
  643. "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
  644. " $name$_ = $empty_list$;\n"
  645. " $clear_mutable_bit_builder$;\n"
  646. " $on_changed$\n"
  647. " return this;\n"
  648. "}\n");
  649. printer->Annotate("{", "}", descriptor_);
  650. }
  651. void RepeatedImmutablePrimitiveFieldGenerator::
  652. GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
  653. // noop for primitives
  654. }
  655. void RepeatedImmutablePrimitiveFieldGenerator::
  656. GenerateInitializationCode(io::Printer* printer) const {
  657. printer->Print(variables_, "$name$_ = $empty_list$;\n");
  658. }
  659. void RepeatedImmutablePrimitiveFieldGenerator::
  660. GenerateBuilderClearCode(io::Printer* printer) const {
  661. printer->Print(variables_,
  662. "$name$_ = $empty_list$;\n"
  663. "$clear_mutable_bit_builder$;\n");
  664. }
  665. void RepeatedImmutablePrimitiveFieldGenerator::
  666. GenerateMergingCode(io::Printer* printer) const {
  667. // The code below does two optimizations:
  668. // 1. If the other list is empty, there's nothing to do. This ensures we
  669. // don't allocate a new array if we already have an immutable one.
  670. // 2. If the other list is non-empty and our current list is empty, we can
  671. // reuse the other list which is guaranteed to be immutable.
  672. printer->Print(variables_,
  673. "if (!other.$name$_.isEmpty()) {\n"
  674. " if ($name$_.isEmpty()) {\n"
  675. " $name$_ = other.$name$_;\n"
  676. " $clear_mutable_bit_builder$;\n"
  677. " } else {\n"
  678. " ensure$capitalized_name$IsMutable();\n"
  679. " $name$_.addAll(other.$name$_);\n"
  680. " }\n"
  681. " $on_changed$\n"
  682. "}\n");
  683. }
  684. void RepeatedImmutablePrimitiveFieldGenerator::
  685. GenerateBuildingCode(io::Printer* printer) const {
  686. // The code below ensures that the result has an immutable list. If our
  687. // list is immutable, we can just reuse it. If not, we make it immutable.
  688. printer->Print(variables_,
  689. "if ($get_mutable_bit_builder$) {\n"
  690. " $name$_ = java.util.Collections.unmodifiableList($name$_);\n"
  691. " $clear_mutable_bit_builder$;\n"
  692. "}\n"
  693. "result.$name$_ = $name$_;\n");
  694. }
  695. void RepeatedImmutablePrimitiveFieldGenerator::
  696. GenerateParsingCode(io::Printer* printer) const {
  697. printer->Print(variables_,
  698. "if (!$get_mutable_bit_parser$) {\n"
  699. " $name$_ = new java.util.ArrayList<$boxed_type$>();\n"
  700. " $set_mutable_bit_parser$;\n"
  701. "}\n"
  702. "$name$_.add(input.read$capitalized_type$());\n");
  703. }
  704. void RepeatedImmutablePrimitiveFieldGenerator::
  705. GenerateParsingCodeFromPacked(io::Printer* printer) const {
  706. printer->Print(variables_,
  707. "int length = input.readRawVarint32();\n"
  708. "int limit = input.pushLimit(length);\n"
  709. "if (!$get_mutable_bit_parser$ && input.getBytesUntilLimit() > 0) {\n"
  710. " $name$_ = new java.util.ArrayList<$boxed_type$>();\n"
  711. " $set_mutable_bit_parser$;\n"
  712. "}\n"
  713. "while (input.getBytesUntilLimit() > 0) {\n"
  714. " $name$_.add(input.read$capitalized_type$());\n"
  715. "}\n"
  716. "input.popLimit(limit);\n");
  717. }
  718. void RepeatedImmutablePrimitiveFieldGenerator::
  719. GenerateParsingDoneCode(io::Printer* printer) const {
  720. printer->Print(variables_,
  721. "if ($get_mutable_bit_parser$) {\n"
  722. " $name$_ = java.util.Collections.unmodifiableList($name$_);\n"
  723. "}\n");
  724. }
  725. void RepeatedImmutablePrimitiveFieldGenerator::
  726. GenerateSerializationCode(io::Printer* printer) const {
  727. if (descriptor_->is_packed()) {
  728. // We invoke getSerializedSize in writeTo for messages that have packed
  729. // fields in ImmutableMessageGenerator::GenerateMessageSerializationMethods.
  730. // That makes it safe to rely on the memoized size here.
  731. printer->Print(variables_,
  732. "if (get$capitalized_name$List().size() > 0) {\n"
  733. " output.writeUInt32NoTag($tag$);\n"
  734. " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n"
  735. "}\n"
  736. "for (int i = 0; i < $name$_.size(); i++) {\n"
  737. " output.write$capitalized_type$NoTag($name$_.get(i));\n"
  738. "}\n");
  739. } else {
  740. printer->Print(variables_,
  741. "for (int i = 0; i < $name$_.size(); i++) {\n"
  742. " output.write$capitalized_type$($number$, $name$_.get(i));\n"
  743. "}\n");
  744. }
  745. }
  746. void RepeatedImmutablePrimitiveFieldGenerator::
  747. GenerateSerializedSizeCode(io::Printer* printer) const {
  748. printer->Print(variables_,
  749. "{\n"
  750. " int dataSize = 0;\n");
  751. printer->Indent();
  752. if (FixedSize(GetType(descriptor_)) == -1) {
  753. printer->Print(variables_,
  754. "for (int i = 0; i < $name$_.size(); i++) {\n"
  755. " dataSize += com.google.protobuf.CodedOutputStream\n"
  756. " .compute$capitalized_type$SizeNoTag($name$_.get(i));\n"
  757. "}\n");
  758. } else {
  759. printer->Print(variables_,
  760. "dataSize = $fixed_size$ * get$capitalized_name$List().size();\n");
  761. }
  762. printer->Print(
  763. "size += dataSize;\n");
  764. if (descriptor_->is_packed()) {
  765. printer->Print(variables_,
  766. "if (!get$capitalized_name$List().isEmpty()) {\n"
  767. " size += $tag_size$;\n"
  768. " size += com.google.protobuf.CodedOutputStream\n"
  769. " .computeInt32SizeNoTag(dataSize);\n"
  770. "}\n");
  771. } else {
  772. printer->Print(variables_,
  773. "size += $tag_size$ * get$capitalized_name$List().size();\n");
  774. }
  775. // cache the data size for packed fields.
  776. if (descriptor_->is_packed()) {
  777. printer->Print(variables_,
  778. "$name$MemoizedSerializedSize = dataSize;\n");
  779. }
  780. printer->Outdent();
  781. printer->Print("}\n");
  782. }
  783. void RepeatedImmutablePrimitiveFieldGenerator::
  784. GenerateEqualsCode(io::Printer* printer) const {
  785. printer->Print(variables_,
  786. "result = result && get$capitalized_name$List()\n"
  787. " .equals(other.get$capitalized_name$List());\n");
  788. }
  789. void RepeatedImmutablePrimitiveFieldGenerator::
  790. GenerateHashCode(io::Printer* printer) const {
  791. printer->Print(variables_,
  792. "if (get$capitalized_name$Count() > 0) {\n"
  793. " hash = (37 * hash) + $constant_name$;\n"
  794. " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n"
  795. "}\n");
  796. }
  797. string RepeatedImmutablePrimitiveFieldGenerator::GetBoxedType() const {
  798. return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
  799. }
  800. } // namespace java
  801. } // namespace compiler
  802. } // namespace protobuf
  803. } // namespace google