aboutsummaryrefslogtreecommitdiff
path: root/src/google/protobuf/compiler/java/java_enum_lite.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/compiler/java/java_enum_lite.cc')
-rw-r--r--src/google/protobuf/compiler/java/java_enum_lite.cc67
1 files changed, 36 insertions, 31 deletions
diff --git a/src/google/protobuf/compiler/java/java_enum_lite.cc b/src/google/protobuf/compiler/java/java_enum_lite.cc
index 96815920..806008ee 100644
--- a/src/google/protobuf/compiler/java/java_enum_lite.cc
+++ b/src/google/protobuf/compiler/java/java_enum_lite.cc
@@ -36,12 +36,12 @@
#include <string>
#include <google/protobuf/compiler/java/java_context.h>
-#include <google/protobuf/compiler/java/java_enum_lite.h>
#include <google/protobuf/compiler/java/java_doc_comment.h>
+#include <google/protobuf/compiler/java/java_enum_lite.h>
#include <google/protobuf/compiler/java/java_helpers.h>
#include <google/protobuf/compiler/java/java_name_resolver.h>
-#include <google/protobuf/io/printer.h>
#include <google/protobuf/descriptor.pb.h>
+#include <google/protobuf/io/printer.h>
#include <google/protobuf/stubs/strutil.h>
namespace google {
@@ -49,17 +49,6 @@ namespace protobuf {
namespace compiler {
namespace java {
-namespace {
-bool EnumHasCustomOptions(const EnumDescriptor* descriptor) {
- if (descriptor->options().unknown_fields().field_count() > 0) return true;
- for (int i = 0; i < descriptor->value_count(); ++i) {
- const EnumValueDescriptor* value = descriptor->value(i);
- if (value->options().unknown_fields().field_count() > 0) return true;
- }
- return false;
-}
-} // namespace
-
EnumLiteGenerator::EnumLiteGenerator(const EnumDescriptor* descriptor,
bool immutable_api, Context* context)
: descriptor_(descriptor),
@@ -104,10 +93,12 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
}
printer->Print(vars,
"$name$($number$),\n");
+ printer->Annotate("name", canonical_values_[i]);
}
if (SupportUnknownEnumValue(descriptor_->file())) {
- printer->Print("UNRECOGNIZED(-1),\n");
+ printer->Print("${$UNRECOGNIZED$}$(-1),\n", "{", "", "}", "");
+ printer->Annotate("{", "}", descriptor_);
}
printer->Print(
@@ -124,37 +115,50 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
WriteEnumValueDocComment(printer, aliases_[i].value);
printer->Print(vars,
"public static final $classname$ $name$ = $canonical_name$;\n");
+ printer->Annotate("name", aliases_[i].value);
}
for (int i = 0; i < descriptor_->value_count(); i++) {
std::map<string, string> vars;
vars["name"] = descriptor_->value(i)->name();
vars["number"] = SimpleItoa(descriptor_->value(i)->number());
+ vars["{"] = "";
+ vars["}"] = "";
WriteEnumValueDocComment(printer, descriptor_->value(i));
printer->Print(vars,
- "public static final int $name$_VALUE = $number$;\n");
+ "public static final int ${$$name$_VALUE$}$ = $number$;\n");
+ printer->Annotate("{", "}", descriptor_->value(i));
}
printer->Print("\n");
// -----------------------------------------------------------------
printer->Print(
- "\n"
- "public final int getNumber() {\n"
- " return value;\n"
- "}\n"
- "\n"
- "/**\n"
- " * @deprecated Use {@link #forNumber(int)} instead.\n"
- " */\n"
- "@java.lang.Deprecated\n"
- "public static $classname$ valueOf(int value) {\n"
- " return forNumber(value);\n"
- "}\n"
- "\n"
- "public static $classname$ forNumber(int value) {\n"
- " switch (value) {\n",
- "classname", descriptor_->name());
+ "\n"
+ "@java.lang.Override\n"
+ "public final int getNumber() {\n");
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ printer->Print(
+ " if (this == UNRECOGNIZED) {\n"
+ " throw new java.lang.IllegalArgumentException(\n"
+ " \"Can't get the number of an unknown enum value.\");\n"
+ " }\n");
+ }
+ printer->Print(
+ " return value;\n"
+ "}\n"
+ "\n"
+ "/**\n"
+ " * @deprecated Use {@link #forNumber(int)} instead.\n"
+ " */\n"
+ "@java.lang.Deprecated\n"
+ "public static $classname$ valueOf(int value) {\n"
+ " return forNumber(value);\n"
+ "}\n"
+ "\n"
+ "public static $classname$ forNumber(int value) {\n"
+ " switch (value) {\n",
+ "classname", descriptor_->name());
printer->Indent();
printer->Indent();
@@ -179,6 +183,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
"private static final com.google.protobuf.Internal.EnumLiteMap<\n"
" $classname$> internalValueMap =\n"
" new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
+ " @java.lang.Override\n"
" public $classname$ findValueByNumber(int number) {\n"
" return $classname$.forNumber(number);\n"
" }\n"