| 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 cfed815f8c181cd4e59beb91c066be43749d142c..0353b6072e0f7850d9464254f51618a943fbce48 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
|
| @@ -1,6 +1,6 @@
|
| // Protocol Buffers - Google's data interchange format
|
| // Copyright 2008 Google Inc. All rights reserved.
|
| -// http://code.google.com/p/protobuf/
|
| +// https://developers.google.com/protocol-buffers/
|
| //
|
| // Redistribution and use in source and binary forms, with or without
|
| // modification, are permitted provided that the following conditions are
|
| @@ -35,9 +35,11 @@
|
| #include <map>
|
| #include <string>
|
|
|
| +#include <google/protobuf/compiler/java/java_context.h>
|
| #include <google/protobuf/compiler/java/java_enum.h>
|
| #include <google/protobuf/compiler/java/java_doc_comment.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/stubs/strutil.h>
|
| @@ -47,8 +49,22 @@ namespace protobuf {
|
| namespace compiler {
|
| namespace java {
|
|
|
| -EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor)
|
| - : descriptor_(descriptor) {
|
| +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
|
| +
|
| +EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor,
|
| + bool immutable_api,
|
| + Context* context)
|
| + : descriptor_(descriptor), immutable_api_(immutable_api),
|
| + name_resolver_(context->GetNameResolver()) {
|
| for (int i = 0; i < descriptor_->value_count(); i++) {
|
| const EnumValueDescriptor* value = descriptor_->value(i);
|
| const EnumValueDescriptor* canonical_value =
|
| @@ -88,10 +104,17 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
| vars["index"] = SimpleItoa(canonical_values_[i]->index());
|
| vars["number"] = SimpleItoa(canonical_values_[i]->number());
|
| WriteEnumValueDocComment(printer, canonical_values_[i]);
|
| + if (canonical_values_[i]->options().deprecated()) {
|
| + printer->Print("@java.lang.Deprecated\n");
|
| + }
|
| printer->Print(vars,
|
| "$name$($index$, $number$),\n");
|
| }
|
|
|
| + if (SupportUnknownEnumValue(descriptor_->file())) {
|
| + printer->Print("UNRECOGNIZED(-1, -1),\n");
|
| + }
|
| +
|
| printer->Print(
|
| ";\n"
|
| "\n");
|
| @@ -122,7 +145,17 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
|
|
| printer->Print(
|
| "\n"
|
| - "public final int getNumber() { return value; }\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");
|
| + }
|
| + printer->Print(
|
| + " return value;\n"
|
| + "}\n"
|
| "\n"
|
| "public static $classname$ valueOf(int value) {\n"
|
| " switch (value) {\n",
|
| @@ -178,15 +211,59 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
| // at module init time because it wouldn't work with descriptor.proto, but
|
| // we can cache the value the first time getDescriptor() is called.
|
| if (descriptor_->containing_type() == NULL) {
|
| - printer->Print(
|
| - " return $file$.getDescriptor().getEnumTypes().get($index$);\n",
|
| - "file", ClassName(descriptor_->file()),
|
| - "index", SimpleItoa(descriptor_->index()));
|
| + if (!MultipleJavaFiles(descriptor_->file(), immutable_api_)) {
|
| + printer->Print(
|
| + " return $file$.getDescriptor().getEnumTypes().get($index$);\n",
|
| + "file", name_resolver_->GetClassName(descriptor_->file(),
|
| + immutable_api_),
|
| + "index", SimpleItoa(descriptor_->index()));
|
| + } else {
|
| + printer->Indent();
|
| + if (EnumHasCustomOptions(descriptor_)) {
|
| + // We need to load the immutable classes in order to parse custom
|
| + // options. However, since file level enums (no outer class) are
|
| + // shared by immutable code and mutable code, the immutable classes
|
| + // may not exist. So we try to use Java reflection to retrieve the
|
| + // descriptor from immutable classes.
|
| + printer->Print(
|
| + "try {\n"
|
| + " java.lang.Class immutableFileClass =\n"
|
| + " java.lang.Class.forName(\"$immutable_file_class_name$\");\n"
|
| + " @java.lang.SuppressWarnings(\"unchecked\")\n"
|
| + " java.lang.reflect.Method m =\n"
|
| + " immutableFileClass.getMethod(\"getDescriptor\");\n"
|
| + " com.google.protobuf.Descriptors.FileDescriptor file =\n"
|
| + " (com.google.protobuf.Descriptors.FileDescriptor)\n"
|
| + " m.invoke(immutableFileClass);\n"
|
| + " return file.getEnumTypes().get($index$);\n"
|
| + "} catch (Exception e) {\n"
|
| + // Immutable classes cannot be found. Proceed as if custom options
|
| + // don't exist.
|
| + "}\n",
|
| + "immutable_file_class_name",
|
| + name_resolver_->GetImmutableClassName(descriptor_->file()),
|
| + "index", SimpleItoa(descriptor_->index()));
|
| + }
|
| + printer->Print(
|
| + "return $immutable_package$.$descriptor_class$.$descriptor$\n"
|
| + " .getEnumTypes().get($index$);\n",
|
| + "immutable_package", FileJavaPackage(descriptor_->file(), true),
|
| + "descriptor_class",
|
| + name_resolver_->GetDescriptorClassName(descriptor_->file()),
|
| + "descriptor", "getDescriptor()",
|
| + "index", SimpleItoa(descriptor_->index()));
|
| + printer->Outdent();
|
| + }
|
| } else {
|
| printer->Print(
|
| - " return $parent$.getDescriptor().getEnumTypes().get($index$);\n",
|
| - "parent", ClassName(descriptor_->containing_type()),
|
| - "index", SimpleItoa(descriptor_->index()));
|
| + " return $parent$.$descriptor$.getEnumTypes().get($index$);\n",
|
| + "parent", name_resolver_->GetClassName(descriptor_->containing_type(),
|
| + immutable_api_),
|
| + "descriptor", descriptor_->containing_type()->options()
|
| + .no_standard_descriptor_accessor()
|
| + ? "getDefaultInstance().getDescriptorForType()"
|
| + : "getDescriptor()",
|
| + "index", SimpleItoa(descriptor_->index()));
|
| }
|
|
|
| printer->Print(
|
| @@ -221,11 +298,18 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
| " if (desc.getType() != getDescriptor()) {\n"
|
| " throw new java.lang.IllegalArgumentException(\n"
|
| " \"EnumValueDescriptor is not for this type.\");\n"
|
| - " }\n"
|
| + " }\n",
|
| + "classname", descriptor_->name());
|
| + if (SupportUnknownEnumValue(descriptor_->file())) {
|
| + printer->Print(
|
| + " if (desc.getIndex() == -1) {\n"
|
| + " return UNRECOGNIZED;\n"
|
| + " }\n");
|
| + }
|
| + printer->Print(
|
| " return VALUES[desc.getIndex()];\n"
|
| "}\n"
|
| - "\n",
|
| - "classname", descriptor_->name());
|
| + "\n");
|
|
|
| // index is only used for reflection; lite implementation does not need it
|
| printer->Print("private final int index;\n");
|
|
|