java_enum_lite.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_doc_comment.h>
  37. #include <google/protobuf/compiler/java/java_enum_lite.h>
  38. #include <google/protobuf/compiler/java/java_helpers.h>
  39. #include <google/protobuf/compiler/java/java_name_resolver.h>
  40. #include <google/protobuf/descriptor.pb.h>
  41. #include <google/protobuf/io/printer.h>
  42. #include <google/protobuf/stubs/strutil.h>
  43. namespace google {
  44. namespace protobuf {
  45. namespace compiler {
  46. namespace java {
  47. EnumLiteGenerator::EnumLiteGenerator(const EnumDescriptor* descriptor,
  48. bool immutable_api, Context* context)
  49. : descriptor_(descriptor),
  50. immutable_api_(immutable_api),
  51. context_(context),
  52. name_resolver_(context->GetNameResolver()) {
  53. for (int i = 0; i < descriptor_->value_count(); i++) {
  54. const EnumValueDescriptor* value = descriptor_->value(i);
  55. const EnumValueDescriptor* canonical_value =
  56. descriptor_->FindValueByNumber(value->number());
  57. if (value == canonical_value) {
  58. canonical_values_.push_back(value);
  59. } else {
  60. Alias alias;
  61. alias.value = value;
  62. alias.canonical_value = canonical_value;
  63. aliases_.push_back(alias);
  64. }
  65. }
  66. }
  67. EnumLiteGenerator::~EnumLiteGenerator() {}
  68. void EnumLiteGenerator::Generate(io::Printer* printer) {
  69. WriteEnumDocComment(printer, descriptor_);
  70. MaybePrintGeneratedAnnotation(context_, printer, descriptor_, immutable_api_);
  71. printer->Print(
  72. "public enum $classname$\n"
  73. " implements com.google.protobuf.Internal.EnumLite {\n",
  74. "classname", descriptor_->name());
  75. printer->Annotate("classname", descriptor_);
  76. printer->Indent();
  77. for (int i = 0; i < canonical_values_.size(); i++) {
  78. std::map<string, string> vars;
  79. vars["name"] = canonical_values_[i]->name();
  80. vars["number"] = SimpleItoa(canonical_values_[i]->number());
  81. WriteEnumValueDocComment(printer, canonical_values_[i]);
  82. if (canonical_values_[i]->options().deprecated()) {
  83. printer->Print("@java.lang.Deprecated\n");
  84. }
  85. printer->Print(vars,
  86. "$name$($number$),\n");
  87. printer->Annotate("name", canonical_values_[i]);
  88. }
  89. if (SupportUnknownEnumValue(descriptor_->file())) {
  90. printer->Print("${$UNRECOGNIZED$}$(-1),\n", "{", "", "}", "");
  91. printer->Annotate("{", "}", descriptor_);
  92. }
  93. printer->Print(
  94. ";\n"
  95. "\n");
  96. // -----------------------------------------------------------------
  97. for (int i = 0; i < aliases_.size(); i++) {
  98. std::map<string, string> vars;
  99. vars["classname"] = descriptor_->name();
  100. vars["name"] = aliases_[i].value->name();
  101. vars["canonical_name"] = aliases_[i].canonical_value->name();
  102. WriteEnumValueDocComment(printer, aliases_[i].value);
  103. printer->Print(vars,
  104. "public static final $classname$ $name$ = $canonical_name$;\n");
  105. printer->Annotate("name", aliases_[i].value);
  106. }
  107. for (int i = 0; i < descriptor_->value_count(); i++) {
  108. std::map<string, string> vars;
  109. vars["name"] = descriptor_->value(i)->name();
  110. vars["number"] = SimpleItoa(descriptor_->value(i)->number());
  111. vars["{"] = "";
  112. vars["}"] = "";
  113. WriteEnumValueDocComment(printer, descriptor_->value(i));
  114. printer->Print(vars,
  115. "public static final int ${$$name$_VALUE$}$ = $number$;\n");
  116. printer->Annotate("{", "}", descriptor_->value(i));
  117. }
  118. printer->Print("\n");
  119. // -----------------------------------------------------------------
  120. printer->Print(
  121. "\n"
  122. "@java.lang.Override\n"
  123. "public final int getNumber() {\n");
  124. if (SupportUnknownEnumValue(descriptor_->file())) {
  125. printer->Print(
  126. " if (this == UNRECOGNIZED) {\n"
  127. " throw new java.lang.IllegalArgumentException(\n"
  128. " \"Can't get the number of an unknown enum value.\");\n"
  129. " }\n");
  130. }
  131. printer->Print(
  132. " return value;\n"
  133. "}\n"
  134. "\n"
  135. "/**\n"
  136. " * @deprecated Use {@link #forNumber(int)} instead.\n"
  137. " */\n"
  138. "@java.lang.Deprecated\n"
  139. "public static $classname$ valueOf(int value) {\n"
  140. " return forNumber(value);\n"
  141. "}\n"
  142. "\n"
  143. "public static $classname$ forNumber(int value) {\n"
  144. " switch (value) {\n",
  145. "classname", descriptor_->name());
  146. printer->Indent();
  147. printer->Indent();
  148. for (int i = 0; i < canonical_values_.size(); i++) {
  149. printer->Print(
  150. "case $number$: return $name$;\n",
  151. "name", canonical_values_[i]->name(),
  152. "number", SimpleItoa(canonical_values_[i]->number()));
  153. }
  154. printer->Outdent();
  155. printer->Outdent();
  156. printer->Print(
  157. " default: return null;\n"
  158. " }\n"
  159. "}\n"
  160. "\n"
  161. "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
  162. " internalGetValueMap() {\n"
  163. " return internalValueMap;\n"
  164. "}\n"
  165. "private static final com.google.protobuf.Internal.EnumLiteMap<\n"
  166. " $classname$> internalValueMap =\n"
  167. " new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
  168. " @java.lang.Override\n"
  169. " public $classname$ findValueByNumber(int number) {\n"
  170. " return $classname$.forNumber(number);\n"
  171. " }\n"
  172. " };\n"
  173. "\n",
  174. "classname", descriptor_->name());
  175. printer->Print(
  176. "private final int value;\n\n"
  177. "private $classname$(int value) {\n",
  178. "classname", descriptor_->name());
  179. printer->Print(
  180. " this.value = value;\n"
  181. "}\n");
  182. printer->Print(
  183. "\n"
  184. "// @@protoc_insertion_point(enum_scope:$full_name$)\n",
  185. "full_name", descriptor_->full_name());
  186. printer->Outdent();
  187. printer->Print("}\n\n");
  188. }
  189. bool EnumLiteGenerator::CanUseEnumValues() {
  190. if (canonical_values_.size() != descriptor_->value_count()) {
  191. return false;
  192. }
  193. for (int i = 0; i < descriptor_->value_count(); i++) {
  194. if (descriptor_->value(i)->name() != canonical_values_[i]->name()) {
  195. return false;
  196. }
  197. }
  198. return true;
  199. }
  200. } // namespace java
  201. } // namespace compiler
  202. } // namespace protobuf
  203. } // namespace google