java_enum.cc
Go to the documentation of this file.
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 
46 
47 
48 namespace google {
49 namespace protobuf {
50 namespace compiler {
51 namespace java {
52 
54  bool immutable_api, Context* context)
56  immutable_api_(immutable_api),
57  context_(context),
58  name_resolver_(context->GetNameResolver()) {
59  for (int i = 0; i < descriptor_->value_count(); i++) {
61  const EnumValueDescriptor* canonical_value =
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 
76 
80  printer->Print(
81  "public enum $classname$\n"
82  " implements com.google.protobuf.ProtocolMessageEnum {\n",
83  "classname", descriptor_->name());
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());
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 
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();
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["}"] = "";
147  printer->Print(vars,
148  "public static final int ${$$name$_VALUE$}$ = $number$;\n");
149  printer->Annotate("{", "}", descriptor_->value(i));
150  }
151  printer->Print("\n");
152 
153  // -----------------------------------------------------------------
154 
155  printer->Print(
156  "\n"
157  "public final int getNumber() {\n");
159  if (ordinal_is_index) {
160  printer->Print(
161  " if (this == UNRECOGNIZED) {\n"
162  " throw new java.lang.IllegalArgumentException(\n"
163  " \"Can't get the number of an unknown enum value.\");\n"
164  " }\n");
165  } else {
166  printer->Print(
167  " if (index == -1) {\n"
168  " throw new java.lang.IllegalArgumentException(\n"
169  " \"Can't get the number of an unknown enum value.\");\n"
170  " }\n");
171  }
172  }
173  printer->Print(
174  " return value;\n"
175  "}\n"
176  "\n"
177  "/**\n"
178  " * @deprecated Use {@link #forNumber(int)} instead.\n"
179  " */\n"
180  "@java.lang.Deprecated\n"
181  "public static $classname$ valueOf(int value) {\n"
182  " return forNumber(value);\n"
183  "}\n"
184  "\n"
185  "public static $classname$ forNumber(int value) {\n"
186  " switch (value) {\n",
187  "classname", descriptor_->name());
188  printer->Indent();
189  printer->Indent();
190 
191  for (int i = 0; i < canonical_values_.size(); i++) {
192  printer->Print("case $number$: return $name$;\n", "name",
193  canonical_values_[i]->name(), "number",
195  }
196 
197  printer->Outdent();
198  printer->Outdent();
199  printer->Print(
200  " default: return null;\n"
201  " }\n"
202  "}\n"
203  "\n"
204  "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
205  " internalGetValueMap() {\n"
206  " return internalValueMap;\n"
207  "}\n"
208  "private static final com.google.protobuf.Internal.EnumLiteMap<\n"
209  " $classname$> internalValueMap =\n"
210  " new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
211  " public $classname$ findValueByNumber(int number) {\n"
212  " return $classname$.forNumber(number);\n"
213  " }\n"
214  " };\n"
215  "\n",
216  "classname", descriptor_->name());
217 
218  // -----------------------------------------------------------------
219  // Reflection
220 
222  printer->Print(
223  "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n"
224  " getValueDescriptor() {\n"
225  " return getDescriptor().getValues().get($index_text$);\n"
226  "}\n"
227  "public final com.google.protobuf.Descriptors.EnumDescriptor\n"
228  " getDescriptorForType() {\n"
229  " return getDescriptor();\n"
230  "}\n"
231  "public static final com.google.protobuf.Descriptors.EnumDescriptor\n"
232  " getDescriptor() {\n",
233  "index_text", index_text);
234 
235  // TODO(kenton): Cache statically? Note that we can't access descriptors
236  // at module init time because it wouldn't work with descriptor.proto, but
237  // we can cache the value the first time getDescriptor() is called.
238  if (descriptor_->containing_type() == NULL) {
239  // The class generated for the File fully populates the descriptor with
240  // extensions in both the mutable and immutable cases. (In the mutable api
241  // this is accomplished by attempting to load the immutable outer class).
242  printer->Print(
243  " return $file$.getDescriptor().getEnumTypes().get($index$);\n",
244  "file",
246  "index", StrCat(descriptor_->index()));
247  } else {
248  printer->Print(
249  " return $parent$.$descriptor$.getEnumTypes().get($index$);\n",
250  "parent",
253  "descriptor",
255  ->options()
257  ? "getDefaultInstance().getDescriptorForType()"
258  : "getDescriptor()",
259  "index", StrCat(descriptor_->index()));
260  }
261 
262  printer->Print(
263  "}\n"
264  "\n"
265  "private static final $classname$[] VALUES = ",
266  "classname", descriptor_->name());
267 
268  if (CanUseEnumValues()) {
269  // If the constants we are going to output are exactly the ones we
270  // have declared in the Java enum in the same order, then we can use
271  // the values() method that the Java compiler automatically generates
272  // for every enum.
273  printer->Print("values();\n");
274  } else {
275  printer->Print(
276  "{\n"
277  " ");
278  for (int i = 0; i < descriptor_->value_count(); i++) {
279  printer->Print("$name$, ", "name", descriptor_->value(i)->name());
280  }
281  printer->Print(
282  "\n"
283  "};\n");
284  }
285 
286  printer->Print(
287  "\n"
288  "public static $classname$ valueOf(\n"
289  " com.google.protobuf.Descriptors.EnumValueDescriptor desc) {\n"
290  " if (desc.getType() != getDescriptor()) {\n"
291  " throw new java.lang.IllegalArgumentException(\n"
292  " \"EnumValueDescriptor is not for this type.\");\n"
293  " }\n",
294  "classname", descriptor_->name());
296  printer->Print(
297  " if (desc.getIndex() == -1) {\n"
298  " return UNRECOGNIZED;\n"
299  " }\n");
300  }
301  printer->Print(
302  " return VALUES[desc.getIndex()];\n"
303  "}\n"
304  "\n");
305 
306  if (!ordinal_is_index) {
307  printer->Print("private final int index;\n");
308  }
309  }
310 
311  // -----------------------------------------------------------------
312 
313  printer->Print("private final int value;\n\n");
314 
315  if (ordinal_is_index) {
316  printer->Print("private $classname$(int value) {\n", "classname",
317  descriptor_->name());
318  } else {
319  printer->Print("private $classname$(int index, int value) {\n", "classname",
320  descriptor_->name());
321  }
323  !ordinal_is_index) {
324  printer->Print(" this.index = index;\n");
325  }
326  printer->Print(
327  " this.value = value;\n"
328  "}\n");
329 
330  printer->Print(
331  "\n"
332  "// @@protoc_insertion_point(enum_scope:$full_name$)\n",
333  "full_name", descriptor_->full_name());
334 
335  printer->Outdent();
336  printer->Print("}\n\n");
337 }
338 
340  if (canonical_values_.size() != descriptor_->value_count()) {
341  return false;
342  }
343  for (int i = 0; i < descriptor_->value_count(); i++) {
344  if (descriptor_->value(i)->name() != canonical_values_[i]->name()) {
345  return false;
346  }
347  }
348  return true;
349 }
350 
351 } // namespace java
352 } // namespace compiler
353 } // namespace protobuf
354 } // namespace google
google::protobuf::io::Printer::Print
void Print(const std::map< std::string, std::string > &variables, const char *text)
Definition: printer.cc:112
google::protobuf::compiler::java::WriteEnumDocComment
void WriteEnumDocComment(io::Printer *printer, const EnumDescriptor *enum_)
Definition: java_doc_comment.cc:191
context_
MockGeneratorContext context_
Definition: csharp_bootstrap_unittest.cc:125
google::protobuf::compiler::java::MaybePrintGeneratedAnnotation
void MaybePrintGeneratedAnnotation(Context *context, io::Printer *printer, Descriptor *descriptor, bool immutable, const std::string &suffix="")
Definition: java_helpers.h:187
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
google::protobuf::value
const Descriptor::ReservedRange value
Definition: src/google/protobuf/descriptor.h:1954
java_name_resolver.h
java_doc_comment.h
google::protobuf::compiler::java::HasDescriptorMethods
bool HasDescriptorMethods(const Descriptor *descriptor, bool enforce_lite)
Definition: java_helpers.h:242
google::protobuf::compiler::java::Context::EnforceLite
bool EnforceLite() const
Definition: java_context.h:87
java_helpers.h
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf::compiler::java::EnumGenerator::Alias::value
const EnumValueDescriptor * value
Definition: java_enum.h:80
google::protobuf::EnumDescriptor::index
int index() const
Definition: src/google/protobuf/descriptor.h:2107
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
google::protobuf::compiler::java::EnumGenerator::canonical_values_
std::vector< const EnumValueDescriptor * > canonical_values_
Definition: java_enum.h:77
google::protobuf::EnumDescriptor::value_count
int value_count() const
google::protobuf::compiler::java::WriteEnumValueDocComment
void WriteEnumValueDocComment(io::Printer *printer, const EnumValueDescriptor *value)
Definition: java_doc_comment.cc:200
google::protobuf::compiler::java::Context
Definition: java_context.h:65
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::Descriptor::options
const MessageOptions & options() const
google::protobuf::EnumValueDescriptor::name
const std::string & name() const
google::protobuf::EnumDescriptor::FindValueByNumber
const EnumValueDescriptor * FindValueByNumber(int number) const
Definition: src/google/protobuf/descriptor.cc:1714
google::protobuf::io::Printer::Indent
void Indent()
Definition: printer.cc:185
google::protobuf::compiler::java::EnumGenerator::EnumGenerator
EnumGenerator(const EnumDescriptor *descriptor, bool immutable_api, Context *context)
Definition: java_enum.cc:53
google::protobuf::compiler::java::EnumGenerator::aliases_
std::vector< Alias > aliases_
Definition: java_enum.h:83
google::protobuf::EnumDescriptor::name
const std::string & name() const
google::protobuf::compiler::java::SupportUnknownEnumValue
bool SupportUnknownEnumValue(const FileDescriptor *descriptor)
Definition: java_helpers.h:368
strutil.h
google::protobuf::compiler::java::EnumGenerator::~EnumGenerator
~EnumGenerator()
Definition: java_enum.cc:75
google::protobuf::EnumDescriptor::file
const FileDescriptor * file() const
google::protobuf::EnumValueDescriptor::number
int number() const
google::protobuf::EnumDescriptor::value
const EnumValueDescriptor * value(int index) const
printer.h
google::protobuf::compiler::java::EnumGenerator::name_resolver_
ClassNameResolver * name_resolver_
Definition: java_enum.h:88
google::protobuf::compiler::java::EnumGenerator::Generate
void Generate(io::Printer *printer)
Definition: java_enum.cc:77
google::protobuf::compiler::java::EnumGenerator::immutable_api_
bool immutable_api_
Definition: java_enum.h:85
google::protobuf::io::Printer::Annotate
void Annotate(const char *varname, const SomeDescriptor *descriptor)
Definition: printer.h:199
google::protobuf::io::Printer
Definition: printer.h:181
i
int i
Definition: gmock-matchers_test.cc:764
java
google::protobuf::compiler::java::EnumGenerator::descriptor_
const EnumDescriptor * descriptor_
Definition: java_enum.h:70
google::protobuf::EnumDescriptor::containing_type
const Descriptor * containing_type() const
google::protobuf::EnumDescriptor::full_name
const std::string & full_name() const
google::protobuf::compiler::java::EnumGenerator::Alias
Definition: java_enum.h:79
google::protobuf::EnumValueDescriptor
Definition: src/google/protobuf/descriptor.h:1075
java_enum.h
google::protobuf::compiler::java::EnumGenerator::context_
Context * context_
Definition: java_enum.h:87
java_context.h
MessageOptions::no_standard_descriptor_accessor
bool no_standard_descriptor_accessor() const
Definition: descriptor.pb.h:10478
google::protobuf::compiler::java::ClassNameResolver::GetClassName
std::string GetClassName(const Descriptor *descriptor, bool immutable)
Definition: java_name_resolver.cc:229
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
descriptor_
const Descriptor * descriptor_
Definition: field_comparator_test.cc:56
descriptor.pb.h
google::protobuf::EnumDescriptor
Definition: src/google/protobuf/descriptor.h:918
google::protobuf::compiler::java::EnumGenerator::Alias::canonical_value
const EnumValueDescriptor * canonical_value
Definition: java_enum.h:81
google::protobuf::compiler::java::EnumGenerator::CanUseEnumValues
bool CanUseEnumValues()
Definition: java_enum.cc:339
index
GLuint index
Definition: glcorearb.h:3055
google::protobuf::io::Printer::Outdent
void Outdent()
Definition: printer.cc:187
number
double number
Definition: cJSON.h:326
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:54