javanano_message_field.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <map>
  34. #include <string>
  35. #include <google/protobuf/compiler/javanano/javanano_message_field.h>
  36. #include <google/protobuf/compiler/javanano/javanano_helpers.h>
  37. #include <google/protobuf/io/printer.h>
  38. #include <google/protobuf/wire_format.h>
  39. #include <google/protobuf/stubs/strutil.h>
  40. namespace google {
  41. namespace protobuf {
  42. namespace compiler {
  43. namespace javanano {
  44. using internal::WireFormat;
  45. using internal::WireFormatLite;
  46. namespace {
  47. // TODO(kenton): Factor out a "SetCommonFieldVariables()" to get rid of
  48. // repeat code between this and the other field types.
  49. void SetMessageVariables(const Params& params,
  50. const FieldDescriptor* descriptor, map<string, string>* variables) {
  51. (*variables)["name"] =
  52. RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
  53. (*variables)["capitalized_name"] =
  54. RenameJavaKeywords(UnderscoresToCapitalizedCamelCase(descriptor));
  55. (*variables)["number"] = SimpleItoa(descriptor->number());
  56. (*variables)["type"] = ClassName(params, descriptor->message_type());
  57. (*variables)["group_or_message"] =
  58. (descriptor->type() == FieldDescriptor::TYPE_GROUP) ?
  59. "Group" : "Message";
  60. (*variables)["message_name"] = descriptor->containing_type()->name();
  61. //(*variables)["message_type"] = descriptor->message_type()->name();
  62. (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor));
  63. }
  64. } // namespace
  65. // ===================================================================
  66. MessageFieldGenerator::
  67. MessageFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
  68. : FieldGenerator(params), descriptor_(descriptor) {
  69. SetMessageVariables(params, descriptor, &variables_);
  70. }
  71. MessageFieldGenerator::~MessageFieldGenerator() {}
  72. void MessageFieldGenerator::
  73. GenerateMembers(io::Printer* printer) const {
  74. printer->Print(variables_,
  75. "public $type$ $name$;\n");
  76. }
  77. void MessageFieldGenerator::
  78. GenerateClearCode(io::Printer* printer) const {
  79. printer->Print(variables_,
  80. "$name$ = null;\n");
  81. }
  82. void MessageFieldGenerator::
  83. GenerateMergingCode(io::Printer* printer) const {
  84. printer->Print(variables_,
  85. "if (this.$name$ == null) {\n"
  86. " this.$name$ = new $type$();\n"
  87. "}\n");
  88. if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
  89. printer->Print(variables_,
  90. "input.readGroup(this.$name$, $number$);\n");
  91. } else {
  92. printer->Print(variables_,
  93. "input.readMessage(this.$name$);\n");
  94. }
  95. }
  96. void MessageFieldGenerator::
  97. GenerateSerializationCode(io::Printer* printer) const {
  98. printer->Print(variables_,
  99. "if (this.$name$ != null) {\n"
  100. " output.write$group_or_message$($number$, this.$name$);\n"
  101. "}\n");
  102. }
  103. void MessageFieldGenerator::
  104. GenerateSerializedSizeCode(io::Printer* printer) const {
  105. printer->Print(variables_,
  106. "if (this.$name$ != null) {\n"
  107. " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
  108. " .compute$group_or_message$Size($number$, this.$name$);\n"
  109. "}\n");
  110. }
  111. void MessageFieldGenerator::
  112. GenerateEqualsCode(io::Printer* printer) const {
  113. printer->Print(variables_,
  114. "if (this.$name$ == null) { \n"
  115. " if (other.$name$ != null) {\n"
  116. " return false;\n"
  117. " }\n"
  118. "} else {\n"
  119. " if (!this.$name$.equals(other.$name$)) {\n"
  120. " return false;\n"
  121. " }\n"
  122. "}\n");
  123. }
  124. void MessageFieldGenerator::
  125. GenerateHashCodeCode(io::Printer* printer) const {
  126. printer->Print(variables_,
  127. "result = 31 * result +\n"
  128. " (this.$name$ == null ? 0 : this.$name$.hashCode());\n");
  129. }
  130. // ===================================================================
  131. AccessorMessageFieldGenerator::
  132. AccessorMessageFieldGenerator(
  133. const FieldDescriptor* descriptor, const Params& params)
  134. : FieldGenerator(params), descriptor_(descriptor) {
  135. SetMessageVariables(params, descriptor, &variables_);
  136. }
  137. AccessorMessageFieldGenerator::~AccessorMessageFieldGenerator() {}
  138. void AccessorMessageFieldGenerator::
  139. GenerateMembers(io::Printer* printer) const {
  140. printer->Print(variables_,
  141. "private $type$ $name$_;\n"
  142. "public $type$ get$capitalized_name$() {\n"
  143. " return $name$_;\n"
  144. "}\n"
  145. "public $message_name$ set$capitalized_name$($type$ value) {\n"
  146. " if (value == null) {\n"
  147. " throw new java.lang.NullPointerException();\n"
  148. " }\n"
  149. " $name$_ = value;\n"
  150. " return this;\n"
  151. "}\n"
  152. "public boolean has$capitalized_name$() {\n"
  153. " return $name$_ != null;\n"
  154. "}\n"
  155. "public $message_name$ clear$capitalized_name$() {\n"
  156. " $name$_ = null;\n"
  157. " return this;"
  158. "}\n");
  159. }
  160. void AccessorMessageFieldGenerator::
  161. GenerateClearCode(io::Printer* printer) const {
  162. printer->Print(variables_,
  163. "$name$_ = null;\n");
  164. }
  165. void AccessorMessageFieldGenerator::
  166. GenerateMergingCode(io::Printer* printer) const {
  167. printer->Print(variables_,
  168. "if ($name$_ == null) {\n"
  169. " $name$_ = new $type$();\n"
  170. "}\n");
  171. if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
  172. printer->Print(variables_,
  173. "input.readGroup($name$_, $number$);\n");
  174. } else {
  175. printer->Print(variables_,
  176. "input.readMessage($name$_);\n");
  177. }
  178. }
  179. void AccessorMessageFieldGenerator::
  180. GenerateSerializationCode(io::Printer* printer) const {
  181. printer->Print(variables_,
  182. "if ($name$_ != null) {\n"
  183. " output.write$group_or_message$($number$, $name$_);\n"
  184. "}\n");
  185. }
  186. void AccessorMessageFieldGenerator::
  187. GenerateSerializedSizeCode(io::Printer* printer) const {
  188. printer->Print(variables_,
  189. "if ($name$_ != null) {\n"
  190. " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
  191. " .compute$group_or_message$Size($number$, $name$_);\n"
  192. "}\n");
  193. }
  194. void AccessorMessageFieldGenerator::
  195. GenerateEqualsCode(io::Printer* printer) const {
  196. printer->Print(variables_,
  197. "if ($name$_ == null) {\n"
  198. " if (other.$name$_ != null) {\n"
  199. " return false;\n"
  200. " }\n"
  201. "} else if (!$name$_.equals(other.$name$_)) {\n"
  202. " return false;\n"
  203. "}\n");
  204. }
  205. void AccessorMessageFieldGenerator::
  206. GenerateHashCodeCode(io::Printer* printer) const {
  207. printer->Print(variables_,
  208. "result = 31 * result + ($name$_ == null ? 0 : $name$_.hashCode());\n");
  209. }
  210. // ===================================================================
  211. RepeatedMessageFieldGenerator::
  212. RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
  213. : FieldGenerator(params), descriptor_(descriptor) {
  214. SetMessageVariables(params, descriptor, &variables_);
  215. }
  216. RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {}
  217. void RepeatedMessageFieldGenerator::
  218. GenerateMembers(io::Printer* printer) const {
  219. printer->Print(variables_,
  220. "public $type$[] $name$;\n");
  221. }
  222. void RepeatedMessageFieldGenerator::
  223. GenerateClearCode(io::Printer* printer) const {
  224. printer->Print(variables_,
  225. "$name$ = $type$.EMPTY_ARRAY;\n");
  226. }
  227. void RepeatedMessageFieldGenerator::
  228. GenerateMergingCode(io::Printer* printer) const {
  229. // First, figure out the length of the array, then parse.
  230. printer->Print(variables_,
  231. "int arrayLength = com.google.protobuf.nano.WireFormatNano\n"
  232. " .getRepeatedFieldArrayLength(input, $tag$);\n"
  233. "int i = this.$name$ == null ? 0 : this.$name$.length;\n"
  234. "$type$[] newArray =\n"
  235. " new $type$[i + arrayLength];\n"
  236. "if (i != 0) {\n"
  237. " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
  238. "}\n"
  239. "for (; i < newArray.length - 1; i++) {\n"
  240. " newArray[i] = new $type$();\n");
  241. if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
  242. printer->Print(variables_,
  243. " input.readGroup(newArray[i], $number$);\n");
  244. } else {
  245. printer->Print(variables_,
  246. " input.readMessage(newArray[i]);\n");
  247. }
  248. printer->Print(variables_,
  249. " input.readTag();\n"
  250. "}\n"
  251. "// Last one without readTag.\n"
  252. "newArray[i] = new $type$();\n");
  253. if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
  254. printer->Print(variables_,
  255. "input.readGroup(newArray[i], $number$);\n");
  256. } else {
  257. printer->Print(variables_,
  258. "input.readMessage(newArray[i]);\n");
  259. }
  260. printer->Print(variables_,
  261. "this.$name$ = newArray;\n");
  262. }
  263. void RepeatedMessageFieldGenerator::
  264. GenerateSerializationCode(io::Printer* printer) const {
  265. printer->Print(variables_,
  266. "if (this.$name$ != null && this.$name$.length > 0) {\n"
  267. " for (int i = 0; i < this.$name$.length; i++) {\n"
  268. " $type$ element = this.$name$[i];\n"
  269. " if (element != null) {\n"
  270. " output.write$group_or_message$($number$, element);\n"
  271. " }\n"
  272. " }\n"
  273. "}\n");
  274. }
  275. void RepeatedMessageFieldGenerator::
  276. GenerateSerializedSizeCode(io::Printer* printer) const {
  277. printer->Print(variables_,
  278. "if (this.$name$ != null && this.$name$.length > 0) {\n"
  279. " for (int i = 0; i < this.$name$.length; i++) {\n"
  280. " $type$ element = this.$name$[i];\n"
  281. " if (element != null) {\n"
  282. " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
  283. " .compute$group_or_message$Size($number$, element);\n"
  284. " }\n"
  285. " }\n"
  286. "}\n");
  287. }
  288. void RepeatedMessageFieldGenerator::
  289. GenerateEqualsCode(io::Printer* printer) const {
  290. printer->Print(variables_,
  291. "if (!com.google.protobuf.nano.InternalNano.equals(\n"
  292. " this.$name$, other.$name$)) {\n"
  293. " return false;\n"
  294. "}\n");
  295. }
  296. void RepeatedMessageFieldGenerator::
  297. GenerateHashCodeCode(io::Printer* printer) const {
  298. printer->Print(variables_,
  299. "result = 31 * result\n"
  300. " + com.google.protobuf.nano.InternalNano.hashCode(this.$name$);\n");
  301. }
  302. } // namespace javanano
  303. } // namespace compiler
  304. } // namespace protobuf
  305. } // namespace google