PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/external/protobuf/src/google/protobuf/compiler/java/java_message_field.cc

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C++ | 335 lines | 257 code | 36 blank | 42 comment | 7 complexity | 1837afa67db0058e309df72fd6700590 MD5 | raw file
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // 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/java/java_message_field.h>
  36. #include <google/protobuf/compiler/java/java_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 java {
  44. namespace {
  45. // TODO(kenton): Factor out a "SetCommonFieldVariables()" to get rid of
  46. // repeat code between this and the other field types.
  47. void SetMessageVariables(const FieldDescriptor* descriptor,
  48. map<string, string>* variables) {
  49. (*variables)["name"] =
  50. UnderscoresToCamelCase(descriptor);
  51. (*variables)["capitalized_name"] =
  52. UnderscoresToCapitalizedCamelCase(descriptor);
  53. (*variables)["number"] = SimpleItoa(descriptor->number());
  54. (*variables)["type"] = ClassName(descriptor->message_type());
  55. (*variables)["group_or_message"] =
  56. (GetType(descriptor) == FieldDescriptor::TYPE_GROUP) ?
  57. "Group" : "Message";
  58. }
  59. } // namespace
  60. // ===================================================================
  61. MessageFieldGenerator::
  62. MessageFieldGenerator(const FieldDescriptor* descriptor)
  63. : descriptor_(descriptor) {
  64. SetMessageVariables(descriptor, &variables_);
  65. }
  66. MessageFieldGenerator::~MessageFieldGenerator() {}
  67. void MessageFieldGenerator::
  68. GenerateMembers(io::Printer* printer) const {
  69. printer->Print(variables_,
  70. "private boolean has$capitalized_name$;\n"
  71. "private $type$ $name$_;\n"
  72. "public boolean has$capitalized_name$() { return has$capitalized_name$; }\n"
  73. "public $type$ get$capitalized_name$() { return $name$_; }\n");
  74. }
  75. void MessageFieldGenerator::
  76. GenerateBuilderMembers(io::Printer* printer) const {
  77. printer->Print(variables_,
  78. "public boolean has$capitalized_name$() {\n"
  79. " return result.has$capitalized_name$();\n"
  80. "}\n"
  81. "public $type$ get$capitalized_name$() {\n"
  82. " return result.get$capitalized_name$();\n"
  83. "}\n"
  84. "public Builder set$capitalized_name$($type$ value) {\n"
  85. " if (value == null) {\n"
  86. " throw new NullPointerException();\n"
  87. " }\n"
  88. " result.has$capitalized_name$ = true;\n"
  89. " result.$name$_ = value;\n"
  90. " return this;\n"
  91. "}\n"
  92. "public Builder set$capitalized_name$($type$.Builder builderForValue) {\n"
  93. " result.has$capitalized_name$ = true;\n"
  94. " result.$name$_ = builderForValue.build();\n"
  95. " return this;\n"
  96. "}\n"
  97. "public Builder merge$capitalized_name$($type$ value) {\n"
  98. " if (result.has$capitalized_name$() &&\n"
  99. " result.$name$_ != $type$.getDefaultInstance()) {\n"
  100. " result.$name$_ =\n"
  101. " $type$.newBuilder(result.$name$_).mergeFrom(value).buildPartial();\n"
  102. " } else {\n"
  103. " result.$name$_ = value;\n"
  104. " }\n"
  105. " result.has$capitalized_name$ = true;\n"
  106. " return this;\n"
  107. "}\n"
  108. "public Builder clear$capitalized_name$() {\n"
  109. " result.has$capitalized_name$ = false;\n"
  110. " result.$name$_ = $type$.getDefaultInstance();\n"
  111. " return this;\n"
  112. "}\n");
  113. }
  114. void MessageFieldGenerator::
  115. GenerateInitializationCode(io::Printer* printer) const {
  116. printer->Print(variables_, "$name$_ = $type$.getDefaultInstance();\n");
  117. }
  118. void MessageFieldGenerator::
  119. GenerateMergingCode(io::Printer* printer) const {
  120. printer->Print(variables_,
  121. "if (other.has$capitalized_name$()) {\n"
  122. " merge$capitalized_name$(other.get$capitalized_name$());\n"
  123. "}\n");
  124. }
  125. void MessageFieldGenerator::
  126. GenerateBuildingCode(io::Printer* printer) const {
  127. // Nothing to do for singular fields.
  128. }
  129. void MessageFieldGenerator::
  130. GenerateParsingCode(io::Printer* printer) const {
  131. printer->Print(variables_,
  132. "$type$.Builder subBuilder = $type$.newBuilder();\n"
  133. "if (has$capitalized_name$()) {\n"
  134. " subBuilder.mergeFrom(get$capitalized_name$());\n"
  135. "}\n");
  136. if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) {
  137. printer->Print(variables_,
  138. "input.readGroup($number$, subBuilder, extensionRegistry);\n");
  139. } else {
  140. printer->Print(variables_,
  141. "input.readMessage(subBuilder, extensionRegistry);\n");
  142. }
  143. printer->Print(variables_,
  144. "set$capitalized_name$(subBuilder.buildPartial());\n");
  145. }
  146. void MessageFieldGenerator::
  147. GenerateSerializationCode(io::Printer* printer) const {
  148. printer->Print(variables_,
  149. "if (has$capitalized_name$()) {\n"
  150. " output.write$group_or_message$($number$, get$capitalized_name$());\n"
  151. "}\n");
  152. }
  153. void MessageFieldGenerator::
  154. GenerateSerializedSizeCode(io::Printer* printer) const {
  155. printer->Print(variables_,
  156. "if (has$capitalized_name$()) {\n"
  157. " size += com.google.protobuf.CodedOutputStream\n"
  158. " .compute$group_or_message$Size($number$, get$capitalized_name$());\n"
  159. "}\n");
  160. }
  161. string MessageFieldGenerator::GetBoxedType() const {
  162. return ClassName(descriptor_->message_type());
  163. }
  164. // ===================================================================
  165. RepeatedMessageFieldGenerator::
  166. RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor)
  167. : descriptor_(descriptor) {
  168. SetMessageVariables(descriptor, &variables_);
  169. }
  170. RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {}
  171. void RepeatedMessageFieldGenerator::
  172. GenerateMembers(io::Printer* printer) const {
  173. printer->Print(variables_,
  174. "private java.util.List<$type$> $name$_ =\n"
  175. " java.util.Collections.emptyList();\n"
  176. "public java.util.List<$type$> get$capitalized_name$List() {\n"
  177. " return $name$_;\n" // note: unmodifiable list
  178. "}\n"
  179. "public int get$capitalized_name$Count() { return $name$_.size(); }\n"
  180. "public $type$ get$capitalized_name$(int index) {\n"
  181. " return $name$_.get(index);\n"
  182. "}\n");
  183. }
  184. void RepeatedMessageFieldGenerator::
  185. GenerateBuilderMembers(io::Printer* printer) const {
  186. printer->Print(variables_,
  187. // Note: We return an unmodifiable list because otherwise the caller
  188. // could hold on to the returned list and modify it after the message
  189. // has been built, thus mutating the message which is supposed to be
  190. // immutable.
  191. "public java.util.List<$type$> get$capitalized_name$List() {\n"
  192. " return java.util.Collections.unmodifiableList(result.$name$_);\n"
  193. "}\n"
  194. "public int get$capitalized_name$Count() {\n"
  195. " return result.get$capitalized_name$Count();\n"
  196. "}\n"
  197. "public $type$ get$capitalized_name$(int index) {\n"
  198. " return result.get$capitalized_name$(index);\n"
  199. "}\n"
  200. "public Builder set$capitalized_name$(int index, $type$ value) {\n"
  201. " if (value == null) {\n"
  202. " throw new NullPointerException();\n"
  203. " }\n"
  204. " result.$name$_.set(index, value);\n"
  205. " return this;\n"
  206. "}\n"
  207. "public Builder set$capitalized_name$(int index, "
  208. "$type$.Builder builderForValue) {\n"
  209. " result.$name$_.set(index, builderForValue.build());\n"
  210. " return this;\n"
  211. "}\n"
  212. "public Builder add$capitalized_name$($type$ value) {\n"
  213. " if (value == null) {\n"
  214. " throw new NullPointerException();\n"
  215. " }\n"
  216. " if (result.$name$_.isEmpty()) {\n"
  217. " result.$name$_ = new java.util.ArrayList<$type$>();\n"
  218. " }\n"
  219. " result.$name$_.add(value);\n"
  220. " return this;\n"
  221. "}\n"
  222. "public Builder add$capitalized_name$($type$.Builder builderForValue) {\n"
  223. " if (result.$name$_.isEmpty()) {\n"
  224. " result.$name$_ = new java.util.ArrayList<$type$>();\n"
  225. " }\n"
  226. " result.$name$_.add(builderForValue.build());\n"
  227. " return this;\n"
  228. "}\n"
  229. "public Builder addAll$capitalized_name$(\n"
  230. " java.lang.Iterable<? extends $type$> values) {\n"
  231. " if (result.$name$_.isEmpty()) {\n"
  232. " result.$name$_ = new java.util.ArrayList<$type$>();\n"
  233. " }\n"
  234. " super.addAll(values, result.$name$_);\n"
  235. " return this;\n"
  236. "}\n"
  237. "public Builder clear$capitalized_name$() {\n"
  238. " result.$name$_ = java.util.Collections.emptyList();\n"
  239. " return this;\n"
  240. "}\n");
  241. }
  242. void RepeatedMessageFieldGenerator::
  243. GenerateInitializationCode(io::Printer* printer) const {
  244. // Initialized inline.
  245. }
  246. void RepeatedMessageFieldGenerator::
  247. GenerateMergingCode(io::Printer* printer) const {
  248. printer->Print(variables_,
  249. "if (!other.$name$_.isEmpty()) {\n"
  250. " if (result.$name$_.isEmpty()) {\n"
  251. " result.$name$_ = new java.util.ArrayList<$type$>();\n"
  252. " }\n"
  253. " result.$name$_.addAll(other.$name$_);\n"
  254. "}\n");
  255. }
  256. void RepeatedMessageFieldGenerator::
  257. GenerateBuildingCode(io::Printer* printer) const {
  258. printer->Print(variables_,
  259. "if (result.$name$_ != java.util.Collections.EMPTY_LIST) {\n"
  260. " result.$name$_ =\n"
  261. " java.util.Collections.unmodifiableList(result.$name$_);\n"
  262. "}\n");
  263. }
  264. void RepeatedMessageFieldGenerator::
  265. GenerateParsingCode(io::Printer* printer) const {
  266. printer->Print(variables_,
  267. "$type$.Builder subBuilder = $type$.newBuilder();\n");
  268. if (GetType(descriptor_) == FieldDescriptor::TYPE_GROUP) {
  269. printer->Print(variables_,
  270. "input.readGroup($number$, subBuilder, extensionRegistry);\n");
  271. } else {
  272. printer->Print(variables_,
  273. "input.readMessage(subBuilder, extensionRegistry);\n");
  274. }
  275. printer->Print(variables_,
  276. "add$capitalized_name$(subBuilder.buildPartial());\n");
  277. }
  278. void RepeatedMessageFieldGenerator::
  279. GenerateSerializationCode(io::Printer* printer) const {
  280. printer->Print(variables_,
  281. "for ($type$ element : get$capitalized_name$List()) {\n"
  282. " output.write$group_or_message$($number$, element);\n"
  283. "}\n");
  284. }
  285. void RepeatedMessageFieldGenerator::
  286. GenerateSerializedSizeCode(io::Printer* printer) const {
  287. printer->Print(variables_,
  288. "for ($type$ element : get$capitalized_name$List()) {\n"
  289. " size += com.google.protobuf.CodedOutputStream\n"
  290. " .compute$group_or_message$Size($number$, element);\n"
  291. "}\n");
  292. }
  293. string RepeatedMessageFieldGenerator::GetBoxedType() const {
  294. return ClassName(descriptor_->message_type());
  295. }
  296. } // namespace java
  297. } // namespace compiler
  298. } // namespace protobuf
  299. } // namespace google