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