• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <google/protobuf/compiler/java/java_enum.h>
36 
37 #include <map>
38 #include <string>
39 
40 #include <google/protobuf/io/printer.h>
41 #include <google/protobuf/stubs/strutil.h>
42 #include <google/protobuf/compiler/java/java_context.h>
43 #include <google/protobuf/compiler/java/java_doc_comment.h>
44 #include <google/protobuf/compiler/java/java_helpers.h>
45 #include <google/protobuf/compiler/java/java_name_resolver.h>
46 #include <google/protobuf/descriptor.pb.h>
47 
48 namespace google {
49 namespace protobuf {
50 namespace compiler {
51 namespace java {
52 
EnumGenerator(const EnumDescriptor * descriptor,bool immutable_api,Context * context)53 EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor,
54                              bool immutable_api, Context* context)
55     : descriptor_(descriptor),
56       immutable_api_(immutable_api),
57       context_(context),
58       name_resolver_(context->GetNameResolver()) {
59   for (int i = 0; i < descriptor_->value_count(); i++) {
60     const EnumValueDescriptor* value = descriptor_->value(i);
61     const EnumValueDescriptor* canonical_value =
62         descriptor_->FindValueByNumber(value->number());
63 
64     if (value == canonical_value) {
65       canonical_values_.push_back(value);
66     } else {
67       Alias alias;
68       alias.value = value;
69       alias.canonical_value = canonical_value;
70       aliases_.push_back(alias);
71     }
72   }
73 }
74 
~EnumGenerator()75 EnumGenerator::~EnumGenerator() {}
76 
Generate(io::Printer * printer)77 void EnumGenerator::Generate(io::Printer* printer) {
78   WriteEnumDocComment(printer, descriptor_);
79   MaybePrintGeneratedAnnotation(context_, printer, descriptor_, immutable_api_);
80   printer->Print(
81       "$deprecation$public enum $classname$\n"
82       "    implements com.google.protobuf.ProtocolMessageEnum {\n",
83       "classname", descriptor_->name(), "deprecation",
84       descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "");
85   printer->Annotate("classname", descriptor_);
86   printer->Indent();
87 
88   bool ordinal_is_index = true;
89   std::string index_text = "ordinal()";
90   for (int i = 0; i < canonical_values_.size(); i++) {
91     if (canonical_values_[i]->index() != i) {
92       ordinal_is_index = false;
93       index_text = "index";
94       break;
95     }
96   }
97 
98   for (int i = 0; i < canonical_values_.size(); i++) {
99     std::map<std::string, std::string> vars;
100     vars["name"] = canonical_values_[i]->name();
101     vars["index"] = StrCat(canonical_values_[i]->index());
102     vars["number"] = StrCat(canonical_values_[i]->number());
103     WriteEnumValueDocComment(printer, canonical_values_[i]);
104     if (canonical_values_[i]->options().deprecated()) {
105       printer->Print("@java.lang.Deprecated\n");
106     }
107     if (ordinal_is_index) {
108       printer->Print(vars, "$name$($number$),\n");
109     } else {
110       printer->Print(vars, "$name$($index$, $number$),\n");
111     }
112     printer->Annotate("name", canonical_values_[i]);
113   }
114 
115   if (SupportUnknownEnumValue(descriptor_->file())) {
116     if (ordinal_is_index) {
117       printer->Print("${$UNRECOGNIZED$}$(-1),\n", "{", "", "}", "");
118     } else {
119       printer->Print("${$UNRECOGNIZED$}$(-1, -1),\n", "{", "", "}", "");
120     }
121     printer->Annotate("{", "}", descriptor_);
122   }
123 
124   printer->Print(
125       ";\n"
126       "\n");
127 
128   // -----------------------------------------------------------------
129 
130   for (int i = 0; i < aliases_.size(); i++) {
131     std::map<std::string, std::string> vars;
132     vars["classname"] = descriptor_->name();
133     vars["name"] = aliases_[i].value->name();
134     vars["canonical_name"] = aliases_[i].canonical_value->name();
135     WriteEnumValueDocComment(printer, aliases_[i].value);
136     printer->Print(
137         vars, "public static final $classname$ $name$ = $canonical_name$;\n");
138     printer->Annotate("name", aliases_[i].value);
139   }
140 
141   for (int i = 0; i < descriptor_->value_count(); i++) {
142     std::map<std::string, std::string> vars;
143     vars["name"] = descriptor_->value(i)->name();
144     vars["number"] = StrCat(descriptor_->value(i)->number());
145     vars["{"] = "";
146     vars["}"] = "";
147     vars["deprecation"] = descriptor_->value(i)->options().deprecated()
148                               ? "@java.lang.Deprecated "
149                               : "";
150     WriteEnumValueDocComment(printer, descriptor_->value(i));
151     printer->Print(vars,
152                    "$deprecation$public static final int ${$$name$_VALUE$}$ = "
153                    "$number$;\n");
154     printer->Annotate("{", "}", descriptor_->value(i));
155   }
156   printer->Print("\n");
157 
158   // -----------------------------------------------------------------
159 
160   printer->Print(
161       "\n"
162       "public final int getNumber() {\n");
163   if (SupportUnknownEnumValue(descriptor_->file())) {
164     if (ordinal_is_index) {
165       printer->Print(
166           "  if (this == UNRECOGNIZED) {\n"
167           "    throw new java.lang.IllegalArgumentException(\n"
168           "        \"Can't get the number of an unknown enum value.\");\n"
169           "  }\n");
170     } else {
171       printer->Print(
172           "  if (index == -1) {\n"
173           "    throw new java.lang.IllegalArgumentException(\n"
174           "        \"Can't get the number of an unknown enum value.\");\n"
175           "  }\n");
176     }
177   }
178   printer->Print(
179       "  return value;\n"
180       "}\n"
181       "\n"
182       "/**\n"
183       " * @param value The numeric wire value of the corresponding enum "
184       "entry.\n"
185       " * @return The enum associated with the given numeric wire value.\n"
186       " * @deprecated Use {@link #forNumber(int)} instead.\n"
187       " */\n"
188       "@java.lang.Deprecated\n"
189       "public static $classname$ valueOf(int value) {\n"
190       "  return forNumber(value);\n"
191       "}\n"
192       "\n"
193       "/**\n"
194       " * @param value The numeric wire value of the corresponding enum "
195       "entry.\n"
196       " * @return The enum associated with the given numeric wire value.\n"
197       " */\n"
198       "public static $classname$ forNumber(int value) {\n"
199       "  switch (value) {\n",
200       "classname", descriptor_->name());
201   printer->Indent();
202   printer->Indent();
203 
204   for (int i = 0; i < canonical_values_.size(); i++) {
205     printer->Print("case $number$: return $name$;\n", "name",
206                    canonical_values_[i]->name(), "number",
207                    StrCat(canonical_values_[i]->number()));
208   }
209 
210   printer->Outdent();
211   printer->Outdent();
212   printer->Print(
213       "    default: return null;\n"
214       "  }\n"
215       "}\n"
216       "\n"
217       "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
218       "    internalGetValueMap() {\n"
219       "  return internalValueMap;\n"
220       "}\n"
221       "private static final com.google.protobuf.Internal.EnumLiteMap<\n"
222       "    $classname$> internalValueMap =\n"
223       "      new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
224       "        public $classname$ findValueByNumber(int number) {\n"
225       "          return $classname$.forNumber(number);\n"
226       "        }\n"
227       "      };\n"
228       "\n",
229       "classname", descriptor_->name());
230 
231   // -----------------------------------------------------------------
232   // Reflection
233 
234   if (HasDescriptorMethods(descriptor_, context_->EnforceLite())) {
235     printer->Print(
236         "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n"
237         "    getValueDescriptor() {\n");
238     if (SupportUnknownEnumValue(descriptor_->file())) {
239       if (ordinal_is_index) {
240         printer->Print(
241             "  if (this == UNRECOGNIZED) {\n"
242             "    throw new java.lang.IllegalStateException(\n"
243             "        \"Can't get the descriptor of an unrecognized enum "
244             "value.\");\n"
245             "  }\n");
246       } else {
247         printer->Print(
248             "  if (index == -1) {\n"
249             "    throw new java.lang.IllegalStateException(\n"
250             "        \"Can't get the descriptor of an unrecognized enum "
251             "value.\");\n"
252             "  }\n");
253       }
254     }
255     printer->Print(
256         "  return getDescriptor().getValues().get($index_text$);\n"
257         "}\n"
258         "public final com.google.protobuf.Descriptors.EnumDescriptor\n"
259         "    getDescriptorForType() {\n"
260         "  return getDescriptor();\n"
261         "}\n"
262         "public static final com.google.protobuf.Descriptors.EnumDescriptor\n"
263         "    getDescriptor() {\n",
264         "index_text", index_text);
265 
266     // TODO(kenton):  Cache statically?  Note that we can't access descriptors
267     //   at module init time because it wouldn't work with descriptor.proto, but
268     //   we can cache the value the first time getDescriptor() is called.
269     if (descriptor_->containing_type() == NULL) {
270       // The class generated for the File fully populates the descriptor with
271       // extensions in both the mutable and immutable cases. (In the mutable api
272       // this is accomplished by attempting to load the immutable outer class).
273       printer->Print(
274           "  return $file$.getDescriptor().getEnumTypes().get($index$);\n",
275           "file",
276           name_resolver_->GetClassName(descriptor_->file(), immutable_api_),
277           "index", StrCat(descriptor_->index()));
278     } else {
279       printer->Print(
280           "  return $parent$.$descriptor$.getEnumTypes().get($index$);\n",
281           "parent",
282           name_resolver_->GetClassName(descriptor_->containing_type(),
283                                        immutable_api_),
284           "descriptor",
285           descriptor_->containing_type()
286                   ->options()
287                   .no_standard_descriptor_accessor()
288               ? "getDefaultInstance().getDescriptorForType()"
289               : "getDescriptor()",
290           "index", StrCat(descriptor_->index()));
291     }
292 
293     printer->Print(
294         "}\n"
295         "\n"
296         "private static final $classname$[] VALUES = ",
297         "classname", descriptor_->name());
298 
299     if (CanUseEnumValues()) {
300       // If the constants we are going to output are exactly the ones we
301       // have declared in the Java enum in the same order, then we can use
302       // the values() method that the Java compiler automatically generates
303       // for every enum.
304       printer->Print("values();\n");
305     } else {
306       printer->Print("getStaticValuesArray();\n");
307       printer->Print("private static $classname$[] getStaticValuesArray() {\n",
308                      "classname", descriptor_->name());
309       printer->Indent();
310       printer->Print(
311           "return new $classname$[] {\n"
312           "  ",
313           "classname", descriptor_->name());
314       for (int i = 0; i < descriptor_->value_count(); i++) {
315         printer->Print("$name$, ", "name", descriptor_->value(i)->name());
316       }
317       printer->Print(
318           "\n"
319           "};\n");
320       printer->Outdent();
321       printer->Print("}");
322     }
323 
324     printer->Print(
325         "\n"
326         "public static $classname$ valueOf(\n"
327         "    com.google.protobuf.Descriptors.EnumValueDescriptor desc) {\n"
328         "  if (desc.getType() != getDescriptor()) {\n"
329         "    throw new java.lang.IllegalArgumentException(\n"
330         "      \"EnumValueDescriptor is not for this type.\");\n"
331         "  }\n",
332         "classname", descriptor_->name());
333     if (SupportUnknownEnumValue(descriptor_->file())) {
334       printer->Print(
335           "  if (desc.getIndex() == -1) {\n"
336           "    return UNRECOGNIZED;\n"
337           "  }\n");
338     }
339     printer->Print(
340         "  return VALUES[desc.getIndex()];\n"
341         "}\n"
342         "\n");
343 
344     if (!ordinal_is_index) {
345       printer->Print("private final int index;\n");
346     }
347   }
348 
349   // -----------------------------------------------------------------
350 
351   printer->Print("private final int value;\n\n");
352 
353   if (ordinal_is_index) {
354     printer->Print("private $classname$(int value) {\n", "classname",
355                    descriptor_->name());
356   } else {
357     printer->Print("private $classname$(int index, int value) {\n", "classname",
358                    descriptor_->name());
359   }
360   if (HasDescriptorMethods(descriptor_, context_->EnforceLite()) &&
361       !ordinal_is_index) {
362     printer->Print("  this.index = index;\n");
363   }
364   printer->Print(
365       "  this.value = value;\n"
366       "}\n");
367 
368   printer->Print(
369       "\n"
370       "// @@protoc_insertion_point(enum_scope:$full_name$)\n",
371       "full_name", descriptor_->full_name());
372 
373   printer->Outdent();
374   printer->Print("}\n\n");
375 }
376 
CanUseEnumValues()377 bool EnumGenerator::CanUseEnumValues() {
378   if (canonical_values_.size() != descriptor_->value_count()) {
379     return false;
380   }
381   for (int i = 0; i < descriptor_->value_count(); i++) {
382     if (descriptor_->value(i)->name() != canonical_values_[i]->name()) {
383       return false;
384     }
385   }
386   return true;
387 }
388 
389 }  // namespace java
390 }  // namespace compiler
391 }  // namespace protobuf
392 }  // namespace google
393