java_enum_lite.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 
48 
49 namespace google {
50 namespace protobuf {
51 namespace compiler {
52 namespace java {
53 
55  bool immutable_api, Context* context)
57  immutable_api_(immutable_api),
58  context_(context),
59  name_resolver_(context->GetNameResolver()) {
60  for (int i = 0; i < descriptor_->value_count(); i++) {
62  const EnumValueDescriptor* canonical_value =
64 
65  if (value == canonical_value) {
66  canonical_values_.push_back(value);
67  } else {
68  Alias alias;
69  alias.value = value;
70  alias.canonical_value = canonical_value;
71  aliases_.push_back(alias);
72  }
73  }
74 }
75 
77 
81  printer->Print(
82  "public enum $classname$\n"
83  " implements com.google.protobuf.Internal.EnumLite {\n",
84  "classname", descriptor_->name());
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());
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 
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();
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["}"] = "";
129  printer->Print(vars,
130  "public static final int ${$$name$_VALUE$}$ = $number$;\n");
131  printer->Annotate("{", "}", descriptor_->value(i));
132  }
133  printer->Print("\n");
134 
135  // -----------------------------------------------------------------
136 
137  printer->Print(
138  "\n"
139  "@java.lang.Override\n"
140  "public final int getNumber() {\n");
142  printer->Print(
143  " if (this == UNRECOGNIZED) {\n"
144  " throw new java.lang.IllegalArgumentException(\n"
145  " \"Can't get the number of an unknown enum value.\");\n"
146  " }\n");
147  }
148  printer->Print(
149  " return value;\n"
150  "}\n"
151  "\n"
152  "/**\n"
153  " * @deprecated Use {@link #forNumber(int)} instead.\n"
154  " */\n"
155  "@java.lang.Deprecated\n"
156  "public static $classname$ valueOf(int value) {\n"
157  " return forNumber(value);\n"
158  "}\n"
159  "\n"
160  "public static $classname$ forNumber(int value) {\n"
161  " switch (value) {\n",
162  "classname", descriptor_->name());
163  printer->Indent();
164  printer->Indent();
165 
166  for (int i = 0; i < canonical_values_.size(); i++) {
167  printer->Print("case $number$: return $name$;\n", "name",
168  canonical_values_[i]->name(), "number",
170  }
171 
172  printer->Outdent();
173  printer->Outdent();
174  printer->Print(
175  " default: return null;\n"
176  " }\n"
177  "}\n"
178  "\n"
179  "public static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
180  " internalGetValueMap() {\n"
181  " return internalValueMap;\n"
182  "}\n"
183  "private static final com.google.protobuf.Internal.EnumLiteMap<\n"
184  " $classname$> internalValueMap =\n"
185  " new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
186  " @java.lang.Override\n"
187  " public $classname$ findValueByNumber(int number) {\n"
188  " return $classname$.forNumber(number);\n"
189  " }\n"
190  " };\n"
191  "\n"
192  "public static com.google.protobuf.Internal.EnumVerifier \n"
193  " internalGetVerifier() {\n"
194  " return $classname$Verifier.INSTANCE;\n"
195  "}\n"
196  "\n"
197  "private static final class $classname$Verifier implements \n"
198  " com.google.protobuf.Internal.EnumVerifier { \n"
199  " static final com.google.protobuf.Internal.EnumVerifier "
200  " INSTANCE = new $classname$Verifier();\n"
201  " @java.lang.Override\n"
202  " public boolean isInRange(int number) {\n"
203  " return $classname$.forNumber(number) != null;\n"
204  " }\n"
205  " };\n"
206  "\n",
207  "classname", descriptor_->name());
208 
209  printer->Print(
210  "private final int value;\n\n"
211  "private $classname$(int value) {\n",
212  "classname", descriptor_->name());
213  printer->Print(
214  " this.value = value;\n"
215  "}\n");
216 
217  printer->Print(
218  "\n"
219  "// @@protoc_insertion_point(enum_scope:$full_name$)\n",
220  "full_name", descriptor_->full_name());
221 
222  printer->Outdent();
223  printer->Print("}\n\n");
224 }
225 
226 } // namespace java
227 } // namespace compiler
228 } // namespace protobuf
229 } // 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::EnumLiteGenerator::canonical_values_
std::vector< const EnumValueDescriptor * > canonical_values_
Definition: java_enum_lite.h:77
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
map_util.h
java_name_resolver.h
java_doc_comment.h
google::protobuf::compiler::java::EnumLiteGenerator::immutable_api_
bool immutable_api_
Definition: java_enum_lite.h:85
google::protobuf::compiler::java::EnumLiteGenerator::Alias::canonical_value
const EnumValueDescriptor * canonical_value
Definition: java_enum_lite.h:81
java_helpers.h
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
java_enum_lite.h
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
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::EnumLiteGenerator::EnumLiteGenerator
EnumLiteGenerator(const EnumDescriptor *descriptor, bool immutable_api, Context *context)
Definition: java_enum_lite.cc:54
google::protobuf::compiler::java::Context
Definition: java_context.h:65
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::compiler::java::EnumLiteGenerator::descriptor_
const EnumDescriptor * descriptor_
Definition: java_enum_lite.h:70
google::protobuf::EnumValueDescriptor::name
const std::string & name() const
google::protobuf::compiler::java::EnumLiteGenerator::~EnumLiteGenerator
~EnumLiteGenerator()
Definition: java_enum_lite.cc:76
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::EnumDescriptor::name
const std::string & name() const
google::protobuf::compiler::java::EnumLiteGenerator::context_
Context * context_
Definition: java_enum_lite.h:87
google::protobuf::compiler::java::SupportUnknownEnumValue
bool SupportUnknownEnumValue(const FileDescriptor *descriptor)
Definition: java_helpers.h:368
strutil.h
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
google::protobuf::compiler::java::EnumLiteGenerator::Generate
void Generate(io::Printer *printer)
Definition: java_enum_lite.cc:78
printer.h
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::EnumDescriptor::full_name
const std::string & full_name() const
google::protobuf::EnumValueDescriptor
Definition: src/google/protobuf/descriptor.h:1075
java_context.h
google::protobuf::compiler::java::EnumLiteGenerator::aliases_
std::vector< Alias > aliases_
Definition: java_enum_lite.h:83
google::protobuf::compiler::java::EnumLiteGenerator::Alias
Definition: java_enum_lite.h:79
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::io::Printer::Outdent
void Outdent()
Definition: printer.cc:187
google::protobuf::compiler::java::EnumLiteGenerator::Alias::value
const EnumValueDescriptor * value
Definition: java_enum_lite.h:80
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