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