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