java_enum.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/compiler/java/java_context.h>
  36. #include <google/protobuf/compiler/java/java_enum.h>
  37. #include <google/protobuf/compiler/java/java_doc_comment.h>
  38. #include <google/protobuf/compiler/java/java_helpers.h>
  39. #include <google/protobuf/compiler/java/java_name_resolver.h>
  40. #include <google/protobuf/io/printer.h>
  41. #include <google/protobuf/descriptor.pb.h>
  42. #include <google/protobuf/stubs/strutil.h>
  43. namespace google {
  44. namespace protobuf {
  45. namespace compiler {
  46. namespace java {
  47. namespace {
  48. bool EnumHasCustomOptions(const EnumDescriptor* descriptor) {
  49. if (descriptor->options().unknown_fields().field_count() > 0) return true;
  50. for (int i = 0; i < descriptor->value_count(); ++i) {
  51. const EnumValueDescriptor* value = descriptor->value(i);
  52. if (value->options().unknown_fields().field_count() > 0) return true;
  53. }
  54. return false;
  55. }
  56. } // namespace
  57. EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor,
  58. bool immutable_api,
  59. Context* context)
  60. : descriptor_(descriptor), immutable_api_(immutable_api),
  61. name_resolver_(context->GetNameResolver()) {
  62. for (int i = 0; i < descriptor_->value_count(); i++) {
  63. const EnumValueDescriptor* value = descriptor_->value(i);
  64. const EnumValueDescriptor* canonical_value =
  65. descriptor_->FindValueByNumber(value->number());
  66. if (value == canonical_value) {
  67. canonical_values_.push_back(value);
  68. } else {
  69. Alias alias;
  70. alias.value = value;
  71. alias.canonical_value = canonical_value;
  72. aliases_.push_back(alias);
  73. }
  74. }
  75. }
  76. EnumGenerator::~EnumGenerator() {}
  77. void EnumGenerator::Generate(io::Printer* printer) {
  78. WriteEnumDocComment(printer, descriptor_);
  79. printer->Print(
  80. "public enum $classname$\n"
  81. " implements com.google.protobuf.ProtocolMessageEnum {\n",
  82. "classname", descriptor_->name());
  83. printer->Indent();
  84. for (int i = 0; i < canonical_values_.size(); i++) {
  85. map<string, string> vars;
  86. vars["name"] = canonical_values_[i]->name();
  87. vars["index"] = SimpleItoa(canonical_values_[i]->index());
  88. vars["number"] = SimpleItoa(canonical_values_[i]->number());
  89. WriteEnumValueDocComment(printer, canonical_values_[i]);
  90. if (canonical_values_[i]->options().deprecated()) {
  91. printer->Print("@java.lang.Deprecated\n");
  92. }
  93. printer->Print(vars,
  94. "$name$($index$, $number$),\n");
  95. }
  96. if (SupportUnknownEnumValue(descriptor_->file())) {
  97. printer->Print("UNRECOGNIZED(-1, -1),\n");
  98. }
  99. printer->Print(
  100. ";\n"
  101. "\n");
  102. // -----------------------------------------------------------------
  103. for (int i = 0; i < aliases_.size(); i++) {
  104. map<string, string> vars;
  105. vars["classname"] = descriptor_->name();
  106. vars["name"] = aliases_[i].value->name();
  107. vars["canonical_name"] = aliases_[i].canonical_value->name();
  108. WriteEnumValueDocComment(printer, aliases_[i].value);
  109. printer->Print(vars,
  110. "public static final $classname$ $name$ = $canonical_name$;\n");
  111. }
  112. for (int i = 0; i < descriptor_->value_count(); i++) {
  113. map<string, string> vars;
  114. vars["name"] = descriptor_->value(i)->name();
  115. vars["number"] = SimpleItoa(descriptor_->value(i)->number());
  116. WriteEnumValueDocComment(printer, descriptor_->value(i));
  117. printer->Print(vars,
  118. "public static final int $name$_VALUE = $number$;\n");
  119. }
  120. printer->Print("\n");
  121. // -----------------------------------------------------------------
  122. printer->Print(
  123. "\n"
  124. "public final int getNumber() {\n");
  125. if (SupportUnknownEnumValue(descriptor_->file())) {
  126. printer->Print(
  127. " if (index == -1) {\n"
  128. " throw new java.lang.IllegalArgumentException(\n"
  129. " \"Can't get the number of an unknown enum value.\");\n"
  130. " }\n");
  131. }
  132. printer->Print(
  133. " return value;\n"
  134. "}\n"
  135. "\n"
  136. "public static $classname$ valueOf(int value) {\n"
  137. " return forNumber(value);\n"
  138. "}\n"
  139. "\n"
  140. "public static $classname$ forNumber(int value) {\n"
  141. " switch (value) {\n",
  142. "classname", descriptor_->name());
  143. printer->Indent();
  144. printer->Indent();
  145. for (int i = 0; i < canonical_values_.size(); i++) {
  146. printer->Print(
  147. "case $number$: return $name$;\n",
  148. "name", canonical_values_[i]->name(),
  149. "number", SimpleItoa(canonical_values_[i]->number()));
  150. }
  151. printer->Outdent();
  152. printer->Outdent();
  153. printer->Print(
  154. " default: return null;\n"
  155. " }\n"
  156. "}\n"
  157. "\n"
  158. "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
  159. " internalGetValueMap() {\n"
  160. " return internalValueMap;\n"
  161. "}\n"
  162. "private static final com.google.protobuf.Internal.EnumLiteMap<\n"
  163. " $classname$> internalValueMap =\n"
  164. " new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
  165. " public $classname$ findValueByNumber(int number) {\n"
  166. " return $classname$.forNumber(number);\n"
  167. " }\n"
  168. " };\n"
  169. "\n",
  170. "classname", descriptor_->name());
  171. // -----------------------------------------------------------------
  172. // Reflection
  173. if (HasDescriptorMethods(descriptor_)) {
  174. printer->Print(
  175. "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n"
  176. " getValueDescriptor() {\n"
  177. " return getDescriptor().getValues().get(index);\n"
  178. "}\n"
  179. "public final com.google.protobuf.Descriptors.EnumDescriptor\n"
  180. " getDescriptorForType() {\n"
  181. " return getDescriptor();\n"
  182. "}\n"
  183. "public static final com.google.protobuf.Descriptors.EnumDescriptor\n"
  184. " getDescriptor() {\n");
  185. // TODO(kenton): Cache statically? Note that we can't access descriptors
  186. // at module init time because it wouldn't work with descriptor.proto, but
  187. // we can cache the value the first time getDescriptor() is called.
  188. if (descriptor_->containing_type() == NULL) {
  189. if (!MultipleJavaFiles(descriptor_->file(), immutable_api_)) {
  190. printer->Print(
  191. " return $file$.getDescriptor().getEnumTypes().get($index$);\n",
  192. "file", name_resolver_->GetClassName(descriptor_->file(),
  193. immutable_api_),
  194. "index", SimpleItoa(descriptor_->index()));
  195. } else {
  196. printer->Indent();
  197. if (EnumHasCustomOptions(descriptor_)) {
  198. // We need to load the immutable classes in order to parse custom
  199. // options. However, since file level enums (no outer class) are
  200. // shared by immutable code and mutable code, the immutable classes
  201. // may not exist. So we try to use Java reflection to retrieve the
  202. // descriptor from immutable classes.
  203. printer->Print(
  204. "try {\n"
  205. " java.lang.Class immutableFileClass =\n"
  206. " java.lang.Class.forName(\"$immutable_file_class_name$\");\n"
  207. " @java.lang.SuppressWarnings(\"unchecked\")\n"
  208. " java.lang.reflect.Method m =\n"
  209. " immutableFileClass.getMethod(\"getDescriptor\");\n"
  210. " com.google.protobuf.Descriptors.FileDescriptor file =\n"
  211. " (com.google.protobuf.Descriptors.FileDescriptor)\n"
  212. " m.invoke(immutableFileClass);\n"
  213. " return file.getEnumTypes().get($index$);\n"
  214. "} catch (Exception e) {\n"
  215. // Immutable classes cannot be found. Proceed as if custom options
  216. // don't exist.
  217. "}\n",
  218. "immutable_file_class_name",
  219. name_resolver_->GetImmutableClassName(descriptor_->file()),
  220. "index", SimpleItoa(descriptor_->index()));
  221. }
  222. printer->Print(
  223. "return $immutable_package$.$descriptor_class$.$descriptor$\n"
  224. " .getEnumTypes().get($index$);\n",
  225. "immutable_package", FileJavaPackage(descriptor_->file(), true),
  226. "descriptor_class",
  227. name_resolver_->GetDescriptorClassName(descriptor_->file()),
  228. "descriptor", "getDescriptor()",
  229. "index", SimpleItoa(descriptor_->index()));
  230. printer->Outdent();
  231. }
  232. } else {
  233. printer->Print(
  234. " return $parent$.$descriptor$.getEnumTypes().get($index$);\n",
  235. "parent", name_resolver_->GetClassName(descriptor_->containing_type(),
  236. immutable_api_),
  237. "descriptor", descriptor_->containing_type()->options()
  238. .no_standard_descriptor_accessor()
  239. ? "getDefaultInstance().getDescriptorForType()"
  240. : "getDescriptor()",
  241. "index", SimpleItoa(descriptor_->index()));
  242. }
  243. printer->Print(
  244. "}\n"
  245. "\n"
  246. "private static final $classname$[] VALUES = ",
  247. "classname", descriptor_->name());
  248. if (CanUseEnumValues()) {
  249. // If the constants we are going to output are exactly the ones we
  250. // have declared in the Java enum in the same order, then we can use
  251. // the values() method that the Java compiler automatically generates
  252. // for every enum.
  253. printer->Print("values();\n");
  254. } else {
  255. printer->Print(
  256. "{\n"
  257. " ");
  258. for (int i = 0; i < descriptor_->value_count(); i++) {
  259. printer->Print("$name$, ",
  260. "name", descriptor_->value(i)->name());
  261. }
  262. printer->Print(
  263. "\n"
  264. "};\n");
  265. }
  266. printer->Print(
  267. "\n"
  268. "public static $classname$ valueOf(\n"
  269. " com.google.protobuf.Descriptors.EnumValueDescriptor desc) {\n"
  270. " if (desc.getType() != getDescriptor()) {\n"
  271. " throw new java.lang.IllegalArgumentException(\n"
  272. " \"EnumValueDescriptor is not for this type.\");\n"
  273. " }\n",
  274. "classname", descriptor_->name());
  275. if (SupportUnknownEnumValue(descriptor_->file())) {
  276. printer->Print(
  277. " if (desc.getIndex() == -1) {\n"
  278. " return UNRECOGNIZED;\n"
  279. " }\n");
  280. }
  281. printer->Print(
  282. " return VALUES[desc.getIndex()];\n"
  283. "}\n"
  284. "\n");
  285. printer->Print("private final int index;\n");
  286. }
  287. // -----------------------------------------------------------------
  288. printer->Print(
  289. "private final int value;\n\n"
  290. "private $classname$(int index, int value) {\n",
  291. "classname", descriptor_->name());
  292. if (HasDescriptorMethods(descriptor_)) {
  293. printer->Print(" this.index = index;\n");
  294. }
  295. printer->Print(
  296. " this.value = value;\n"
  297. "}\n");
  298. printer->Print(
  299. "\n"
  300. "// @@protoc_insertion_point(enum_scope:$full_name$)\n",
  301. "full_name", descriptor_->full_name());
  302. printer->Outdent();
  303. printer->Print("}\n\n");
  304. }
  305. bool EnumGenerator::CanUseEnumValues() {
  306. if (canonical_values_.size() != descriptor_->value_count()) {
  307. return false;
  308. }
  309. for (int i = 0; i < descriptor_->value_count(); i++) {
  310. if (descriptor_->value(i)->name() != canonical_values_[i]->name()) {
  311. return false;
  312. }
  313. }
  314. return true;
  315. }
  316. } // namespace java
  317. } // namespace compiler
  318. } // namespace protobuf
  319. } // namespace google