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_lite.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 #include <google/protobuf/stubs/map_util.h>
47
48 namespace google {
49 namespace protobuf {
50 namespace compiler {
51 namespace java {
52
EnumLiteGenerator(const EnumDescriptor * descriptor,bool immutable_api,Context * context)53 EnumLiteGenerator::EnumLiteGenerator(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
~EnumLiteGenerator()75 EnumLiteGenerator::~EnumLiteGenerator() {}
76
Generate(io::Printer * printer)77 void EnumLiteGenerator::Generate(io::Printer* printer) {
78 WriteEnumDocComment(printer, descriptor_);
79 MaybePrintGeneratedAnnotation(context_, printer, descriptor_, immutable_api_);
80 printer->Print(
81 "public enum $classname$\n"
82 " implements com.google.protobuf.Internal.EnumLite {\n",
83 "classname", descriptor_->name());
84 printer->Annotate("classname", descriptor_);
85 printer->Indent();
86
87 for (int i = 0; i < canonical_values_.size(); i++) {
88 std::map<std::string, std::string> vars;
89 vars["name"] = canonical_values_[i]->name();
90 vars["number"] = StrCat(canonical_values_[i]->number());
91 WriteEnumValueDocComment(printer, canonical_values_[i]);
92 if (canonical_values_[i]->options().deprecated()) {
93 printer->Print("@java.lang.Deprecated\n");
94 }
95 printer->Print(vars, "$name$($number$),\n");
96 printer->Annotate("name", canonical_values_[i]);
97 }
98
99 if (SupportUnknownEnumValue(descriptor_->file())) {
100 printer->Print("${$UNRECOGNIZED$}$(-1),\n", "{", "", "}", "");
101 printer->Annotate("{", "}", descriptor_);
102 }
103
104 printer->Print(
105 ";\n"
106 "\n");
107
108 // -----------------------------------------------------------------
109
110 for (int i = 0; i < aliases_.size(); i++) {
111 std::map<std::string, std::string> vars;
112 vars["classname"] = descriptor_->name();
113 vars["name"] = aliases_[i].value->name();
114 vars["canonical_name"] = aliases_[i].canonical_value->name();
115 WriteEnumValueDocComment(printer, aliases_[i].value);
116 printer->Print(
117 vars, "public static final $classname$ $name$ = $canonical_name$;\n");
118 printer->Annotate("name", aliases_[i].value);
119 }
120
121 for (int i = 0; i < descriptor_->value_count(); i++) {
122 std::map<std::string, std::string> vars;
123 vars["name"] = descriptor_->value(i)->name();
124 vars["number"] = StrCat(descriptor_->value(i)->number());
125 vars["{"] = "";
126 vars["}"] = "";
127 vars["deprecation"] = descriptor_->value(i)->options().deprecated()
128 ? "@java.lang.Deprecated "
129 : "";
130 WriteEnumValueDocComment(printer, descriptor_->value(i));
131 printer->Print(vars,
132 "$deprecation$public static final int ${$$name$_VALUE$}$ = "
133 "$number$;\n");
134 printer->Annotate("{", "}", descriptor_->value(i));
135 }
136 printer->Print("\n");
137
138 // -----------------------------------------------------------------
139
140 printer->Print(
141 "\n"
142 "@java.lang.Override\n"
143 "public final int getNumber() {\n");
144 if (SupportUnknownEnumValue(descriptor_->file())) {
145 printer->Print(
146 " if (this == UNRECOGNIZED) {\n"
147 " throw new java.lang.IllegalArgumentException(\n"
148 " \"Can't get the number of an unknown enum value.\");\n"
149 " }\n");
150 }
151 printer->Print(
152 " return value;\n"
153 "}\n"
154 "\n"
155 "/**\n"
156 " * @param value The number of the enum to look for.\n"
157 " * @return The enum associated with the given number.\n"
158 " * @deprecated Use {@link #forNumber(int)} instead.\n"
159 " */\n"
160 "@java.lang.Deprecated\n"
161 "public static $classname$ valueOf(int value) {\n"
162 " return forNumber(value);\n"
163 "}\n"
164 "\n"
165 "public static $classname$ forNumber(int value) {\n"
166 " switch (value) {\n",
167 "classname", descriptor_->name());
168 printer->Indent();
169 printer->Indent();
170
171 for (int i = 0; i < canonical_values_.size(); i++) {
172 printer->Print("case $number$: return $name$;\n", "name",
173 canonical_values_[i]->name(), "number",
174 StrCat(canonical_values_[i]->number()));
175 }
176
177 printer->Outdent();
178 printer->Outdent();
179 printer->Print(
180 " default: return null;\n"
181 " }\n"
182 "}\n"
183 "\n"
184 "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
185 " internalGetValueMap() {\n"
186 " return internalValueMap;\n"
187 "}\n"
188 "private static final com.google.protobuf.Internal.EnumLiteMap<\n"
189 " $classname$> internalValueMap =\n"
190 " new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
191 " @java.lang.Override\n"
192 " public $classname$ findValueByNumber(int number) {\n"
193 " return $classname$.forNumber(number);\n"
194 " }\n"
195 " };\n"
196 "\n"
197 "public static com.google.protobuf.Internal.EnumVerifier \n"
198 " internalGetVerifier() {\n"
199 " return $classname$Verifier.INSTANCE;\n"
200 "}\n"
201 "\n"
202 "private static final class $classname$Verifier implements \n"
203 " com.google.protobuf.Internal.EnumVerifier { \n"
204 " static final com.google.protobuf.Internal.EnumVerifier "
205 " INSTANCE = new $classname$Verifier();\n"
206 " @java.lang.Override\n"
207 " public boolean isInRange(int number) {\n"
208 " return $classname$.forNumber(number) != null;\n"
209 " }\n"
210 " };\n"
211 "\n",
212 "classname", descriptor_->name());
213
214 printer->Print(
215 "private final int value;\n\n"
216 "private $classname$(int value) {\n",
217 "classname", descriptor_->name());
218 printer->Print(
219 " this.value = value;\n"
220 "}\n");
221
222 printer->Print(
223 "\n"
224 "// @@protoc_insertion_point(enum_scope:$full_name$)\n",
225 "full_name", descriptor_->full_name());
226
227 printer->Outdent();
228 printer->Print("}\n\n");
229 }
230
231 } // namespace java
232 } // namespace compiler
233 } // namespace protobuf
234 } // namespace google
235