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_lite.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
EnumLiteGenerator(const EnumDescriptor * descriptor,bool immutable_api,Context * context)63 EnumLiteGenerator::EnumLiteGenerator(const EnumDescriptor* descriptor,
64 bool immutable_api, Context* context)
65 : descriptor_(descriptor),
66 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
~EnumLiteGenerator()85 EnumLiteGenerator::~EnumLiteGenerator() {}
86
Generate(io::Printer * printer)87 void EnumLiteGenerator::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.Internal.EnumLite {\n",
93 "classname", descriptor_->name());
94 printer->Annotate("classname", descriptor_);
95 printer->Indent();
96
97 for (int i = 0; i < canonical_values_.size(); i++) {
98 map<string, string> vars;
99 vars["name"] = canonical_values_[i]->name();
100 vars["number"] = SimpleItoa(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 printer->Print(vars,
106 "$name$($number$),\n");
107 }
108
109 if (SupportUnknownEnumValue(descriptor_->file())) {
110 printer->Print("UNRECOGNIZED(-1),\n");
111 }
112
113 printer->Print(
114 ";\n"
115 "\n");
116
117 // -----------------------------------------------------------------
118
119 for (int i = 0; i < aliases_.size(); i++) {
120 map<string, string> vars;
121 vars["classname"] = descriptor_->name();
122 vars["name"] = aliases_[i].value->name();
123 vars["canonical_name"] = aliases_[i].canonical_value->name();
124 WriteEnumValueDocComment(printer, aliases_[i].value);
125 printer->Print(vars,
126 "public static final $classname$ $name$ = $canonical_name$;\n");
127 }
128
129 for (int i = 0; i < descriptor_->value_count(); i++) {
130 map<string, string> vars;
131 vars["name"] = descriptor_->value(i)->name();
132 vars["number"] = SimpleItoa(descriptor_->value(i)->number());
133 WriteEnumValueDocComment(printer, descriptor_->value(i));
134 printer->Print(vars,
135 "public static final int $name$_VALUE = $number$;\n");
136 }
137 printer->Print("\n");
138
139 // -----------------------------------------------------------------
140
141 printer->Print(
142 "\n"
143 "public final int getNumber() {\n"
144 " return value;\n"
145 "}\n"
146 "\n"
147 "/**\n"
148 " * @deprecated Use {@link #forNumber(int)} instead.\n"
149 " */\n"
150 "@java.lang.Deprecated\n"
151 "public static $classname$ valueOf(int value) {\n"
152 " return forNumber(value);\n"
153 "}\n"
154 "\n"
155 "public static $classname$ forNumber(int value) {\n"
156 " switch (value) {\n",
157 "classname", descriptor_->name());
158 printer->Indent();
159 printer->Indent();
160
161 for (int i = 0; i < canonical_values_.size(); i++) {
162 printer->Print(
163 "case $number$: return $name$;\n",
164 "name", canonical_values_[i]->name(),
165 "number", SimpleItoa(canonical_values_[i]->number()));
166 }
167
168 printer->Outdent();
169 printer->Outdent();
170 printer->Print(
171 " default: return null;\n"
172 " }\n"
173 "}\n"
174 "\n"
175 "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
176 " internalGetValueMap() {\n"
177 " return internalValueMap;\n"
178 "}\n"
179 "private static final com.google.protobuf.Internal.EnumLiteMap<\n"
180 " $classname$> internalValueMap =\n"
181 " new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
182 " public $classname$ findValueByNumber(int number) {\n"
183 " return $classname$.forNumber(number);\n"
184 " }\n"
185 " };\n"
186 "\n",
187 "classname", descriptor_->name());
188
189 printer->Print(
190 "private final int value;\n\n"
191 "private $classname$(int value) {\n",
192 "classname", descriptor_->name());
193 printer->Print(
194 " this.value = value;\n"
195 "}\n");
196
197 printer->Print(
198 "\n"
199 "// @@protoc_insertion_point(enum_scope:$full_name$)\n",
200 "full_name", descriptor_->full_name());
201
202 printer->Outdent();
203 printer->Print("}\n\n");
204 }
205
CanUseEnumValues()206 bool EnumLiteGenerator::CanUseEnumValues() {
207 if (canonical_values_.size() != descriptor_->value_count()) {
208 return false;
209 }
210 for (int i = 0; i < descriptor_->value_count(); i++) {
211 if (descriptor_->value(i)->name() != canonical_values_[i]->name()) {
212 return false;
213 }
214 }
215 return true;
216 }
217
218 } // namespace java
219 } // namespace compiler
220 } // namespace protobuf
221 } // namespace google
222