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