| Index: third_party/protobuf/src/google/protobuf/compiler/java/java_enum_lite.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_lite.cc
|
| similarity index 59%
|
| copy from third_party/protobuf/src/google/protobuf/compiler/java/java_enum.cc
|
| copy to third_party/protobuf/src/google/protobuf/compiler/java/java_enum_lite.cc
|
| index cfed815f8c181cd4e59beb91c066be43749d142c..ed415eeed9327ee0272f67073038f887941079a4 100644
|
| --- a/third_party/protobuf/src/google/protobuf/compiler/java/java_enum.cc
|
| +++ b/third_party/protobuf/src/google/protobuf/compiler/java/java_enum_lite.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_enum.h>
|
| +#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_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
|
| +
|
| +EnumLiteGenerator::EnumLiteGenerator(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 =
|
| @@ -65,31 +81,30 @@ EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor)
|
| }
|
| }
|
|
|
| -EnumGenerator::~EnumGenerator() {}
|
| +EnumLiteGenerator::~EnumLiteGenerator() {}
|
|
|
| -void EnumGenerator::Generate(io::Printer* printer) {
|
| +void EnumLiteGenerator::Generate(io::Printer* printer) {
|
| WriteEnumDocComment(printer, descriptor_);
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - printer->Print(
|
| - "public enum $classname$\n"
|
| - " implements com.google.protobuf.ProtocolMessageEnum {\n",
|
| - "classname", descriptor_->name());
|
| - } else {
|
| - printer->Print(
|
| - "public enum $classname$\n"
|
| - " implements com.google.protobuf.Internal.EnumLite {\n",
|
| - "classname", descriptor_->name());
|
| - }
|
| + printer->Print(
|
| + "public enum $classname$\n"
|
| + " implements com.google.protobuf.Internal.EnumLite {\n",
|
| + "classname", descriptor_->name());
|
| printer->Indent();
|
|
|
| for (int i = 0; i < canonical_values_.size(); i++) {
|
| map<string, string> vars;
|
| vars["name"] = canonical_values_[i]->name();
|
| - 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");
|
| + "$name$($number$),\n");
|
| + }
|
| +
|
| + if (SupportUnknownEnumValue(descriptor_->file())) {
|
| + printer->Print("UNRECOGNIZED(-1),\n");
|
| }
|
|
|
| printer->Print(
|
| @@ -122,7 +137,9 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
|
|
| printer->Print(
|
| "\n"
|
| - "public final int getNumber() { return value; }\n"
|
| + "public final int getNumber() {\n"
|
| + " return value;\n"
|
| + "}\n"
|
| "\n"
|
| "public static $classname$ valueOf(int value) {\n"
|
| " switch (value) {\n",
|
| @@ -148,8 +165,8 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
| " internalGetValueMap() {\n"
|
| " return internalValueMap;\n"
|
| "}\n"
|
| - "private static com.google.protobuf.Internal.EnumLiteMap<$classname$>\n"
|
| - " internalValueMap =\n"
|
| + "private static final com.google.protobuf.Internal.EnumLiteMap<\n"
|
| + " $classname$> internalValueMap =\n"
|
| " new com.google.protobuf.Internal.EnumLiteMap<$classname$>() {\n"
|
| " public $classname$ findValueByNumber(int number) {\n"
|
| " return $classname$.valueOf(number);\n"
|
| @@ -158,88 +175,10 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
| "\n",
|
| "classname", descriptor_->name());
|
|
|
| - // -----------------------------------------------------------------
|
| - // Reflection
|
| -
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - printer->Print(
|
| - "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n"
|
| - " getValueDescriptor() {\n"
|
| - " return getDescriptor().getValues().get(index);\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");
|
| -
|
| - // TODO(kenton): Cache statically? Note that we can't access descriptors
|
| - // 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()));
|
| - } else {
|
| - printer->Print(
|
| - " return $parent$.getDescriptor().getEnumTypes().get($index$);\n",
|
| - "parent", ClassName(descriptor_->containing_type()),
|
| - "index", SimpleItoa(descriptor_->index()));
|
| - }
|
| -
|
| - printer->Print(
|
| - "}\n"
|
| - "\n"
|
| - "private static final $classname$[] VALUES = ",
|
| - "classname", descriptor_->name());
|
| -
|
| - if (CanUseEnumValues()) {
|
| - // If the constants we are going to output are exactly the ones we
|
| - // have declared in the Java enum in the same order, then we can use
|
| - // the values() method that the Java compiler automatically generates
|
| - // for every enum.
|
| - printer->Print("values();\n");
|
| - } else {
|
| - printer->Print(
|
| - "{\n"
|
| - " ");
|
| - for (int i = 0; i < descriptor_->value_count(); i++) {
|
| - printer->Print("$name$, ",
|
| - "name", descriptor_->value(i)->name());
|
| - }
|
| - printer->Print(
|
| - "\n"
|
| - "};\n");
|
| - }
|
| -
|
| - printer->Print(
|
| - "\n"
|
| - "public static $classname$ valueOf(\n"
|
| - " com.google.protobuf.Descriptors.EnumValueDescriptor desc) {\n"
|
| - " if (desc.getType() != getDescriptor()) {\n"
|
| - " throw new java.lang.IllegalArgumentException(\n"
|
| - " \"EnumValueDescriptor is not for this type.\");\n"
|
| - " }\n"
|
| - " return VALUES[desc.getIndex()];\n"
|
| - "}\n"
|
| - "\n",
|
| - "classname", descriptor_->name());
|
| -
|
| - // index is only used for reflection; lite implementation does not need it
|
| - printer->Print("private final int index;\n");
|
| - }
|
| -
|
| - // -----------------------------------------------------------------
|
| -
|
| printer->Print(
|
| "private final int value;\n\n"
|
| - "private $classname$(int index, int value) {\n",
|
| + "private $classname$(int value) {\n",
|
| "classname", descriptor_->name());
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - printer->Print(" this.index = index;\n");
|
| - }
|
| printer->Print(
|
| " this.value = value;\n"
|
| "}\n");
|
| @@ -253,7 +192,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
|
| printer->Print("}\n\n");
|
| }
|
|
|
| -bool EnumGenerator::CanUseEnumValues() {
|
| +bool EnumLiteGenerator::CanUseEnumValues() {
|
| if (canonical_values_.size() != descriptor_->value_count()) {
|
| return false;
|
| }
|
|
|