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