protobuf/src/google/protobuf/compiler/java/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 
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 
53  bool immutable_api, Context* context)
55  immutable_api_(immutable_api),
57  name_resolver_(context->GetNameResolver()) {
58  for (int i = 0; i < descriptor_->value_count(); i++) {
60  const EnumValueDescriptor* canonical_value =
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 
75 
76 void EnumGenerator::Generate(io::Printer* printer) {
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());
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["}"] = "";
146  vars["deprecation"] = descriptor_->value(i)->options().deprecated()
147  ? "@java.lang.Deprecated "
148  : "";
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");
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",
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 
234  printer->Print(
235  "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n"
236  " getValueDescriptor() {\n");
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",
276  "index", StrCat(descriptor_->index()));
277  } else {
278  printer->Print(
279  " return $parent$.$descriptor$.getEnumTypes().get($index$);\n",
280  "parent",
283  "descriptor",
285  ->options()
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());
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  }
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 
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
google::protobuf::compiler::java::WriteEnumDocComment
void WriteEnumDocComment(io::Printer *printer, const EnumDescriptor *enum_)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_doc_comment.cc:374
google::protobuf::compiler::java::MaybePrintGeneratedAnnotation
void MaybePrintGeneratedAnnotation(Context *context, io::Printer *printer, Descriptor *descriptor, bool immutable, const std::string &suffix="")
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.h:187
descriptor_
string_view descriptor_
Definition: elf.cc:154
google::protobuf::value
const Descriptor::ReservedRange value
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1954
google::protobuf::EnumValueDescriptor::options
const EnumValueOptions & options() const
google::protobuf::compiler::java::HasDescriptorMethods
bool HasDescriptorMethods(const Descriptor *descriptor, bool enforce_lite)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.h:242
google::protobuf::compiler::java::EnumGenerator::descriptor_
const EnumDescriptor * descriptor_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.h:70
google::protobuf::compiler::java::Context::EnforceLite
bool EnforceLite() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_context.h:87
google::protobuf::compiler::java::EnumGenerator::name_resolver_
ClassNameResolver * name_resolver_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.h:88
google::protobuf::EnumDescriptor::index
int index() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:2107
google::protobuf::EnumDescriptor::value_count
int value_count() const
google::protobuf::compiler::java::WriteEnumValueDocComment
void WriteEnumValueDocComment(io::Printer *printer, const EnumValueDescriptor *value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_doc_comment.cc:383
options
double_dict options[]
Definition: capstone_test.c:55
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc::protobuf::io::Printer
GRPC_CUSTOM_PRINTER Printer
Definition: src/compiler/config.h:54
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
setup.name
name
Definition: setup.py:542
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:1482
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: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:1694
google::protobuf::compiler::java::EnumGenerator::context_
Context * context_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.h:87
google::protobuf::compiler::java::EnumGenerator::EnumGenerator
EnumGenerator(const EnumDescriptor *descriptor, bool immutable_api, Context *context)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.cc:52
google::protobuf::compiler::java::EnumGenerator::canonical_values_
std::vector< const EnumValueDescriptor * > canonical_values_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.h:77
google::protobuf::EnumDescriptor::name
const std::string & name() const
google::protobuf::compiler::java::SupportUnknownEnumValue
bool SupportUnknownEnumValue(const FileDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.h:368
context_
ScopedContext * context_
Definition: filter_fuzzer.cc:559
Alias
@ Alias
Definition: upb/benchmarks/benchmark.cc:201
google::protobuf::compiler::java::EnumGenerator::~EnumGenerator
~EnumGenerator()
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.cc:74
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
number
int32_t number
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:850
EnumValueDescriptor
Definition: protobuf/php/ext/google/protobuf/def.c:63
google::protobuf::compiler::java::EnumGenerator::Generate
void Generate(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.cc:76
EnumDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:143
google::protobuf::compiler::java::EnumGenerator::immutable_api_
bool immutable_api_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.h:85
java
EnumValueOptions::deprecated
bool deprecated() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:12571
google::protobuf::EnumDescriptor::containing_type
const Descriptor * containing_type() const
google::protobuf::EnumDescriptor::full_name
const std::string & full_name() const
google::protobuf::EnumDescriptor::options
const EnumOptions & options() const
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
MessageOptions::no_standard_descriptor_accessor
bool no_standard_descriptor_accessor() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:12089
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
google::protobuf::compiler::java::ClassNameResolver::GetClassName
std::string GetClassName(const Descriptor *descriptor, bool immutable)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_name_resolver.cc:227
google::protobuf::compiler::java::EnumGenerator::aliases_
std::vector< Alias > aliases_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.h:83
google::protobuf::compiler::java::EnumGenerator::CanUseEnumValues
bool CanUseEnumValues()
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.cc:346
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
compiler
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.pb.cc:21
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
EnumOptions::deprecated
bool deprecated() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:12500


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:08