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