| Index: third_party/protobuf/src/google/protobuf/compiler/java/java_enum.cc
|
| diff --git a/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.cc b/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.cc
|
| index 5fc9b002fd6fe91b4f2509952be11aca96fecf9d..947b80e4147d36521f019e3304f0f147ea072ed9 100644
|
| --- a/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.cc
|
| +++ b/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.cc
|
| @@ -64,6 +64,7 @@ EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor,
|
| bool immutable_api,
|
| Context* context)
|
| : descriptor_(descriptor), immutable_api_(immutable_api),
|
| + context_(context),
|
| name_resolver_(context->GetNameResolver()) {
|
| for (int i = 0; i < descriptor_->value_count(); i++) {
|
| const EnumValueDescriptor* value = descriptor_->value(i);
|
| @@ -91,6 +92,16 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
| "classname", descriptor_->name());
|
| printer->Indent();
|
|
|
| + bool ordinal_is_index = true;
|
| + string index_text = "ordinal()";
|
| + for (int i = 0; i < canonical_values_.size(); i++) {
|
| + if (canonical_values_[i]->index() != i) {
|
| + ordinal_is_index = false;
|
| + index_text = "index";
|
| + break;
|
| + }
|
| + }
|
| +
|
| for (int i = 0; i < canonical_values_.size(); i++) {
|
| map<string, string> vars;
|
| vars["name"] = canonical_values_[i]->name();
|
| @@ -100,12 +111,21 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
| if (canonical_values_[i]->options().deprecated()) {
|
| printer->Print("@java.lang.Deprecated\n");
|
| }
|
| - printer->Print(vars,
|
| - "$name$($index$, $number$),\n");
|
| + if (ordinal_is_index) {
|
| + printer->Print(vars,
|
| + "$name$($number$),\n");
|
| + } else {
|
| + printer->Print(vars,
|
| + "$name$($index$, $number$),\n");
|
| + }
|
| }
|
|
|
| if (SupportUnknownEnumValue(descriptor_->file())) {
|
| - printer->Print("UNRECOGNIZED(-1, -1),\n");
|
| + if (ordinal_is_index) {
|
| + printer->Print("UNRECOGNIZED(-1),\n");
|
| + } else {
|
| + printer->Print("UNRECOGNIZED(-1, -1),\n");
|
| + }
|
| }
|
|
|
| printer->Print(
|
| @@ -140,17 +160,33 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
| "\n"
|
| "public final int getNumber() {\n");
|
| if (SupportUnknownEnumValue(descriptor_->file())) {
|
| - printer->Print(
|
| - " if (index == -1) {\n"
|
| - " throw new java.lang.IllegalArgumentException(\n"
|
| - " \"Can't get the number of an unknown enum value.\");\n"
|
| - " }\n");
|
| + if (ordinal_is_index) {
|
| + printer->Print(
|
| + " if (this == UNRECOGNIZED) {\n"
|
| + " throw new java.lang.IllegalArgumentException(\n"
|
| + " \"Can't get the number of an unknown enum value.\");\n"
|
| + " }\n");
|
| + } else {
|
| + printer->Print(
|
| + " if (index == -1) {\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();
|
| @@ -178,7 +214,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
| " $classname$> internalValueMap =\n"
|
| " new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
|
| " public $classname$ findValueByNumber(int number) {\n"
|
| - " return $classname$.valueOf(number);\n"
|
| + " return $classname$.forNumber(number);\n"
|
| " }\n"
|
| " };\n"
|
| "\n",
|
| @@ -187,18 +223,19 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
| // -----------------------------------------------------------------
|
| // Reflection
|
|
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| + if (HasDescriptorMethods(descriptor_, context_->EnforceLite())) {
|
| printer->Print(
|
| "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n"
|
| " getValueDescriptor() {\n"
|
| - " return getDescriptor().getValues().get(index);\n"
|
| + " return getDescriptor().getValues().get($index_text$);\n"
|
| "}\n"
|
| "public final com.google.protobuf.Descriptors.EnumDescriptor\n"
|
| " getDescriptorForType() {\n"
|
| " return getDescriptor();\n"
|
| "}\n"
|
| "public static final com.google.protobuf.Descriptors.EnumDescriptor\n"
|
| - " getDescriptor() {\n");
|
| + " getDescriptor() {\n",
|
| + "index_text", index_text);
|
|
|
| // TODO(kenton): Cache statically? Note that we can't access descriptors
|
| // at module init time because it wouldn't work with descriptor.proto, but
|
| @@ -229,7 +266,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
| " (com.google.protobuf.Descriptors.FileDescriptor)\n"
|
| " m.invoke(immutableFileClass);\n"
|
| " return file.getEnumTypes().get($index$);\n"
|
| - "} catch (Exception e) {\n"
|
| + "} catch (java.lang.Exception e) {\n"
|
| // Immutable classes cannot be found. Proceed as if custom options
|
| // don't exist.
|
| "}\n",
|
| @@ -304,16 +341,27 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
| "}\n"
|
| "\n");
|
|
|
| - printer->Print("private final int index;\n");
|
| + if (!ordinal_is_index) {
|
| + printer->Print("private final int index;\n");
|
| + }
|
| }
|
|
|
| // -----------------------------------------------------------------
|
|
|
| printer->Print(
|
| - "private final int value;\n\n"
|
| - "private $classname$(int index, int value) {\n",
|
| - "classname", descriptor_->name());
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| + "private final int value;\n\n");
|
| +
|
| + if (ordinal_is_index) {
|
| + printer->Print(
|
| + "private $classname$(int value) {\n",
|
| + "classname", descriptor_->name());
|
| + } else {
|
| + printer->Print(
|
| + "private $classname$(int index, int value) {\n",
|
| + "classname", descriptor_->name());
|
| + }
|
| + if (HasDescriptorMethods(descriptor_, context_->EnforceLite()) &&
|
| + !ordinal_is_index) {
|
| printer->Print(" this.index = index;\n");
|
| }
|
| printer->Print(
|
|
|