Migrate away from examining the syntax of a proto file via reflection.

PiperOrigin-RevId: 503500019
pull/11615/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 13c0d0756c
commit 67ac9eb4e9
  1. 8
      src/google/protobuf/compiler/java/enum.cc
  2. 34
      src/google/protobuf/compiler/java/enum_field.cc
  3. 28
      src/google/protobuf/compiler/java/enum_field_lite.cc
  4. 6
      src/google/protobuf/compiler/java/enum_lite.cc
  5. 9
      src/google/protobuf/compiler/java/helpers.cc
  6. 16
      src/google/protobuf/compiler/java/helpers.h
  7. 12
      src/google/protobuf/compiler/java/map_field.cc
  8. 14
      src/google/protobuf/compiler/java/map_field_lite.cc

@ -119,7 +119,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
printer->Annotate("name", canonical_values_[i]); printer->Annotate("name", canonical_values_[i]);
} }
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
if (ordinal_is_index) { if (ordinal_is_index) {
printer->Print("${$UNRECOGNIZED$}$(-1),\n", "{", "", "}", ""); printer->Print("${$UNRECOGNIZED$}$(-1),\n", "{", "", "}", "");
} else { } else {
@ -167,7 +167,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
printer->Print( printer->Print(
"\n" "\n"
"public final int getNumber() {\n"); "public final int getNumber() {\n");
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
if (ordinal_is_index) { if (ordinal_is_index) {
printer->Print( printer->Print(
" if (this == UNRECOGNIZED) {\n" " if (this == UNRECOGNIZED) {\n"
@ -251,7 +251,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
printer->Print( printer->Print(
"public final com.google.protobuf.Descriptors.EnumValueDescriptor\n" "public final com.google.protobuf.Descriptors.EnumValueDescriptor\n"
" getValueDescriptor() {\n"); " getValueDescriptor() {\n");
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
if (ordinal_is_index) { if (ordinal_is_index) {
printer->Print( printer->Print(
" if (this == UNRECOGNIZED) {\n" " if (this == UNRECOGNIZED) {\n"
@ -346,7 +346,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
" \"EnumValueDescriptor is not for this type.\");\n" " \"EnumValueDescriptor is not for this type.\");\n"
" }\n", " }\n",
"classname", descriptor_->name()); "classname", descriptor_->name());
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
" if (desc.getIndex() == -1) {\n" " if (desc.getIndex() == -1) {\n"
" return UNRECOGNIZED;\n" " return UNRECOGNIZED;\n"

@ -120,7 +120,7 @@ void SetEnumVariables(
(*variables)["get_has_field_bit_from_local"] = (*variables)["get_has_field_bit_from_local"] =
GenerateGetBitFromLocal(builderBitIndex); GenerateGetBitFromLocal(builderBitIndex);
if (SupportUnknownEnumValue(descriptor->file())) { if (SupportUnknownEnumValue(descriptor)) {
variables->insert( variables->insert(
{"unknown", absl::StrCat((*variables)["type"], ".UNRECOGNIZED")}); {"unknown", absl::StrCat((*variables)["type"], ".UNRECOGNIZED")});
} else { } else {
@ -167,7 +167,7 @@ void ImmutableEnumFieldGenerator::GenerateInterfaceMembers(
printer->Print(variables_, printer->Print(variables_,
"$deprecation$boolean has$capitalized_name$();\n"); "$deprecation$boolean has$capitalized_name$();\n");
} }
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER);
printer->Print(variables_, printer->Print(variables_,
"$deprecation$int get$capitalized_name$Value();\n"); "$deprecation$int get$capitalized_name$Value();\n");
@ -188,7 +188,7 @@ void ImmutableEnumFieldGenerator::GenerateMembers(io::Printer* printer) const {
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
} }
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER);
printer->Print(variables_, printer->Print(variables_,
"@java.lang.Override $deprecation$public int " "@java.lang.Override $deprecation$public int "
@ -219,7 +219,7 @@ void ImmutableEnumFieldGenerator::GenerateBuilderMembers(
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
} }
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER);
printer->Print(variables_, printer->Print(variables_,
"@java.lang.Override $deprecation$public int " "@java.lang.Override $deprecation$public int "
@ -286,7 +286,7 @@ void ImmutableEnumFieldGenerator::GenerateKotlinDslMembers(
" $kt_dsl_builder$.${$set$capitalized_name$$}$(value)\n" " $kt_dsl_builder$.${$set$capitalized_name$$}$(value)\n"
" }\n"); " }\n");
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
variables_, variables_,
"$kt_deprecation$public var $kt_name$Value: kotlin.Int\n" "$kt_deprecation$public var $kt_name$Value: kotlin.Int\n"
@ -338,7 +338,7 @@ void ImmutableEnumFieldGenerator::GenerateMergingCode(
"if (other.has$capitalized_name$()) {\n" "if (other.has$capitalized_name$()) {\n"
" set$capitalized_name$(other.get$capitalized_name$());\n" " set$capitalized_name$(other.get$capitalized_name$());\n"
"}\n"); "}\n");
} else if (SupportUnknownEnumValue(descriptor_->file())) { } else if (SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
variables_, variables_,
"if (other.$name$_ != $default_number$) {\n" "if (other.$name$_ != $default_number$) {\n"
@ -362,7 +362,7 @@ void ImmutableEnumFieldGenerator::GenerateBuildingCode(
void ImmutableEnumFieldGenerator::GenerateBuilderParsingCode( void ImmutableEnumFieldGenerator::GenerateBuilderParsingCode(
io::Printer* printer) const { io::Printer* printer) const {
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print(variables_, printer->Print(variables_,
"$name$_ = input.readEnum();\n" "$name$_ = input.readEnum();\n"
"$set_has_field_bit_builder$\n"); "$set_has_field_bit_builder$\n");
@ -437,7 +437,7 @@ void ImmutableEnumOneofFieldGenerator::GenerateMembers(
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER);
printer->Print( printer->Print(
variables_, variables_,
@ -473,7 +473,7 @@ void ImmutableEnumOneofFieldGenerator::GenerateBuilderMembers(
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER);
printer->Print( printer->Print(
variables_, variables_,
@ -552,7 +552,7 @@ void ImmutableEnumOneofFieldGenerator::GenerateBuildingCode(
void ImmutableEnumOneofFieldGenerator::GenerateMergingCode( void ImmutableEnumOneofFieldGenerator::GenerateMergingCode(
io::Printer* printer) const { io::Printer* printer) const {
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
variables_, variables_,
"set$capitalized_name$Value(other.get$capitalized_name$Value());\n"); "set$capitalized_name$Value(other.get$capitalized_name$Value());\n");
@ -564,7 +564,7 @@ void ImmutableEnumOneofFieldGenerator::GenerateMergingCode(
void ImmutableEnumOneofFieldGenerator::GenerateBuilderParsingCode( void ImmutableEnumOneofFieldGenerator::GenerateBuilderParsingCode(
io::Printer* printer) const { io::Printer* printer) const {
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print(variables_, printer->Print(variables_,
"int rawValue = input.readEnum();\n" "int rawValue = input.readEnum();\n"
"$set_oneof_case_message$;\n" "$set_oneof_case_message$;\n"
@ -604,7 +604,7 @@ void ImmutableEnumOneofFieldGenerator::GenerateSerializedSizeCode(
void ImmutableEnumOneofFieldGenerator::GenerateEqualsCode( void ImmutableEnumOneofFieldGenerator::GenerateEqualsCode(
io::Printer* printer) const { io::Printer* printer) const {
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
variables_, variables_,
"if (get$capitalized_name$Value()\n" "if (get$capitalized_name$Value()\n"
@ -619,7 +619,7 @@ void ImmutableEnumOneofFieldGenerator::GenerateEqualsCode(
void ImmutableEnumOneofFieldGenerator::GenerateHashCode( void ImmutableEnumOneofFieldGenerator::GenerateHashCode(
io::Printer* printer) const { io::Printer* printer) const {
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print(variables_, printer->Print(variables_,
"hash = (37 * hash) + $constant_name$;\n" "hash = (37 * hash) + $constant_name$;\n"
"hash = (53 * hash) + get$capitalized_name$Value();\n"); "hash = (53 * hash) + get$capitalized_name$Value();\n");
@ -661,7 +661,7 @@ void RepeatedImmutableEnumFieldGenerator::GenerateInterfaceMembers(
WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER); WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER);
printer->Print(variables_, printer->Print(variables_,
"$deprecation$$type$ get$capitalized_name$(int index);\n"); "$deprecation$$type$ get$capitalized_name$(int index);\n");
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER);
printer->Print(variables_, printer->Print(variables_,
"$deprecation$java.util.List<java.lang.Integer>\n" "$deprecation$java.util.List<java.lang.Integer>\n"
@ -716,7 +716,7 @@ void RepeatedImmutableEnumFieldGenerator::GenerateMembers(
" return $name$_converter_.convert($name$_.get(index));\n" " return $name$_converter_.convert($name$_.get(index));\n"
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER);
printer->Print(variables_, printer->Print(variables_,
"@java.lang.Override\n" "@java.lang.Override\n"
@ -844,7 +844,7 @@ void RepeatedImmutableEnumFieldGenerator::GenerateBuilderMembers(
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER);
printer->Print(variables_, printer->Print(variables_,
"$deprecation$public java.util.List<java.lang.Integer>\n" "$deprecation$public java.util.List<java.lang.Integer>\n"
@ -954,7 +954,7 @@ void RepeatedImmutableEnumFieldGenerator::GenerateBuildingCode(
void RepeatedImmutableEnumFieldGenerator::GenerateBuilderParsingCode( void RepeatedImmutableEnumFieldGenerator::GenerateBuilderParsingCode(
io::Printer* printer) const { io::Printer* printer) const {
// Read and store the enum // Read and store the enum
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print(variables_, printer->Print(variables_,
"int tmpRaw = input.readEnum();\n" "int tmpRaw = input.readEnum();\n"
"ensure$capitalized_name$IsMutable();\n" "ensure$capitalized_name$IsMutable();\n"

@ -128,7 +128,7 @@ void SetEnumVariables(
(*variables)["set_has_field_bit_to_local"] = (*variables)["set_has_field_bit_to_local"] =
GenerateSetBitToLocal(messageBitIndex); GenerateSetBitToLocal(messageBitIndex);
if (SupportUnknownEnumValue(descriptor->file())) { if (SupportUnknownEnumValue(descriptor)) {
variables->insert( variables->insert(
{"unknown", absl::StrCat((*variables)["type"], ".UNRECOGNIZED")}); {"unknown", absl::StrCat((*variables)["type"], ".UNRECOGNIZED")});
} else { } else {
@ -172,7 +172,7 @@ void ImmutableEnumFieldLiteGenerator::GenerateInterfaceMembers(
"$deprecation$boolean ${$has$capitalized_name$$}$();\n"); "$deprecation$boolean ${$has$capitalized_name$$}$();\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
} }
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER);
printer->Print(variables_, printer->Print(variables_,
"$deprecation$int ${$get$capitalized_name$Value$}$();\n"); "$deprecation$int ${$get$capitalized_name$Value$}$();\n");
@ -212,7 +212,7 @@ void ImmutableEnumFieldLiteGenerator::GenerateMembers(
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
} }
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER);
printer->Print( printer->Print(
variables_, variables_,
@ -232,7 +232,7 @@ void ImmutableEnumFieldLiteGenerator::GenerateMembers(
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
// Generate private setters for the builder to proxy into. // Generate private setters for the builder to proxy into.
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, SETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, SETTER);
printer->Print(variables_, printer->Print(variables_,
"private void set$capitalized_name$Value(int value) {\n" "private void set$capitalized_name$Value(int value) {\n"
@ -266,7 +266,7 @@ void ImmutableEnumFieldLiteGenerator::GenerateBuilderMembers(
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
} }
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER);
printer->Print( printer->Print(
variables_, variables_,
@ -327,7 +327,7 @@ void ImmutableEnumFieldLiteGenerator::GenerateKotlinDslMembers(
" $kt_dsl_builder$.${$set$capitalized_name$$}$(value)\n" " $kt_dsl_builder$.${$set$capitalized_name$$}$(value)\n"
" }\n"); " }\n");
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
variables_, variables_,
"$kt_deprecation$public var $kt_name$Value: kotlin.Int\n" "$kt_deprecation$public var $kt_name$Value: kotlin.Int\n"
@ -409,7 +409,7 @@ void ImmutableEnumOneofFieldLiteGenerator::GenerateMembers(
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER);
printer->Print( printer->Print(
variables_, variables_,
@ -436,7 +436,7 @@ void ImmutableEnumOneofFieldLiteGenerator::GenerateMembers(
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
// Generate private setters for the builder to proxy into. // Generate private setters for the builder to proxy into.
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, SETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, SETTER);
printer->Print(variables_, printer->Print(variables_,
"private void set$capitalized_name$Value(int value) {\n" "private void set$capitalized_name$Value(int value) {\n"
@ -485,7 +485,7 @@ void ImmutableEnumOneofFieldLiteGenerator::GenerateBuilderMembers(
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, GETTER);
printer->Print( printer->Print(
variables_, variables_,
@ -571,7 +571,7 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateInterfaceMembers(
variables_, variables_,
"$deprecation$$type$ ${$get$capitalized_name$$}$(int index);\n"); "$deprecation$$type$ ${$get$capitalized_name$$}$(int index);\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER);
printer->Print(variables_, printer->Print(variables_,
"$deprecation$java.util.List<java.lang.Integer>\n" "$deprecation$java.util.List<java.lang.Integer>\n"
@ -641,7 +641,7 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateMembers(
" return result == null ? $unknown$ : result;\n" " return result == null ? $unknown$ : result;\n"
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER);
printer->Print(variables_, printer->Print(variables_,
"@java.lang.Override\n" "@java.lang.Override\n"
@ -707,7 +707,7 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateMembers(
" $name$_ = emptyIntList();\n" " $name$_ = emptyIntList();\n"
"}\n"); "}\n");
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, SETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, SETTER);
printer->Print(variables_, printer->Print(variables_,
"private void set$capitalized_name$Value(\n" "private void set$capitalized_name$Value(\n"
@ -740,7 +740,7 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateFieldInfo(
WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_), WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
output); output);
printer->Print(variables_, "\"$name$_\",\n"); printer->Print(variables_, "\"$name$_\",\n");
if (!SupportUnknownEnumValue(descriptor_->file())) { if (!SupportUnknownEnumValue(descriptor_)) {
PrintEnumVerifierLogic(printer, descriptor_, variables_, PrintEnumVerifierLogic(printer, descriptor_, variables_,
/*var_name=*/"$type$", /*var_name=*/"$type$",
/*terminating_string=*/",\n", /*terminating_string=*/",\n",
@ -815,7 +815,7 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateBuilderMembers(
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER); WriteFieldEnumValueAccessorDocComment(printer, descriptor_, LIST_GETTER);
printer->Print(variables_, printer->Print(variables_,
"@java.lang.Override\n" "@java.lang.Override\n"

@ -97,7 +97,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
printer->Annotate("name", canonical_values_[i]); printer->Annotate("name", canonical_values_[i]);
} }
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print("${$UNRECOGNIZED$}$(-1),\n", "{", "", "}", ""); printer->Print("${$UNRECOGNIZED$}$(-1),\n", "{", "", "}", "");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
} }
@ -142,7 +142,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
"\n" "\n"
"@java.lang.Override\n" "@java.lang.Override\n"
"public final int getNumber() {\n"); "public final int getNumber() {\n");
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
" if (this == UNRECOGNIZED) {\n" " if (this == UNRECOGNIZED) {\n"
" throw new java.lang.IllegalArgumentException(\n" " throw new java.lang.IllegalArgumentException(\n"
@ -235,7 +235,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
" result.append(getClass().getName()).append('@')\n" " result.append(getClass().getName()).append('@')\n"
" .append(java.lang.Integer.toHexString(\n" " .append(java.lang.Integer.toHexString(\n"
" java.lang.System.identityHashCode(this)));\n"); " java.lang.System.identityHashCode(this)));\n");
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
" if (this != UNRECOGNIZED) {\n" " if (this != UNRECOGNIZED) {\n"
" result.append(\" number=\").append(getNumber());\n" " result.append(\" number=\").append(getNumber());\n"

@ -932,11 +932,10 @@ int GetExperimentalJavaFieldType(const FieldDescriptor* field) {
} }
if (field->is_map()) { if (field->is_map()) {
if (!SupportUnknownEnumValue(field)) { const FieldDescriptor* value = field->message_type()->map_value();
const FieldDescriptor* value = field->message_type()->map_value(); if (GetJavaType(value) == JAVATYPE_ENUM &&
if (GetJavaType(value) == JAVATYPE_ENUM) { !SupportUnknownEnumValue(field)) {
extra_bits |= kMapWithProto2EnumValue; extra_bits |= kMapWithProto2EnumValue;
}
} }
return kMapFieldType | extra_bits; return kMapFieldType | extra_bits;
} else if (field->is_packed()) { } else if (field->is_packed()) {

@ -384,15 +384,19 @@ inline bool ExposePublicParser(const FileDescriptor* descriptor) {
return descriptor->syntax() == FileDescriptor::SYNTAX_PROTO2; return descriptor->syntax() == FileDescriptor::SYNTAX_PROTO2;
} }
// Whether unknown enum values are kept (i.e., not stored in UnknownFieldSet inline bool SupportUnknownEnumValue(const EnumDescriptor* e) {
// but in the message and can be queried using additional getters that return return !e->is_closed();
// ints.
inline bool SupportUnknownEnumValue(const FileDescriptor* descriptor) {
return descriptor->syntax() == FileDescriptor::SYNTAX_PROTO3;
} }
inline bool SupportUnknownEnumValue(const FieldDescriptor* field) { inline bool SupportUnknownEnumValue(const FieldDescriptor* field) {
return field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3; // If we get in a map field, we are asking about its value type, not the map
// field.
if (field->is_map()) {
field = field->message_type()->map_value();
}
GOOGLE_ABSL_CHECK(field->enum_type() != nullptr) << field->type_name();
return SupportUnknownEnumValue(field->enum_type());
} }
// Check whether a message has repeated fields. // Check whether a message has repeated fields.

@ -147,7 +147,7 @@ void SetMessageVariables(
{"value_enum_type_pass_through_nullness", {"value_enum_type_pass_through_nullness",
absl::StrCat(pass_through_nullness, (*variables)["value_enum_type"])}); absl::StrCat(pass_through_nullness, (*variables)["value_enum_type"])});
if (SupportUnknownEnumValue(descriptor->file())) { if (SupportUnknownEnumValue(descriptor)) {
// Map unknown values to a special UNRECOGNIZED value if supported. // Map unknown values to a special UNRECOGNIZED value if supported.
variables->insert( variables->insert(
{"unrecognized_value", {"unrecognized_value",
@ -275,7 +275,7 @@ void ImmutableMapFieldGenerator::GenerateInterfaceMembers(
"$deprecation$$value_enum_type$ ${$get$capitalized_name$OrThrow$}$(\n" "$deprecation$$value_enum_type$ ${$get$capitalized_name$OrThrow$}$(\n"
" $key_type$ key);\n"); " $key_type$ key);\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
variables_, variables_,
"/**\n" "/**\n"
@ -478,7 +478,7 @@ void ImmutableMapFieldGenerator::GenerateBuilderMembers(
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
if (context_->options().opensource_runtime) { if (context_->options().opensource_runtime) {
printer->Print( printer->Print(
variables_, variables_,
@ -645,7 +645,7 @@ void ImmutableMapFieldGenerator::GenerateMapGetters(
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
variables_, variables_,
"/**\n" "/**\n"
@ -870,8 +870,8 @@ void ImmutableMapFieldGenerator::GenerateBuildingCode(
void ImmutableMapFieldGenerator::GenerateBuilderParsingCode( void ImmutableMapFieldGenerator::GenerateBuilderParsingCode(
io::Printer* printer) const { io::Printer* printer) const {
if (!SupportUnknownEnumValue(descriptor_->file()) && if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM &&
GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { !SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
variables_, variables_,
"com.google.protobuf.ByteString bytes = input.readBytes();\n" "com.google.protobuf.ByteString bytes = input.readBytes();\n"

@ -144,7 +144,7 @@ void SetMessageVariables(
{"value_enum_type_pass_through_nullness", {"value_enum_type_pass_through_nullness",
absl::StrCat(pass_through_nullness, (*variables)["value_enum_type"])}); absl::StrCat(pass_through_nullness, (*variables)["value_enum_type"])});
if (SupportUnknownEnumValue(descriptor->file())) { if (SupportUnknownEnumValue(descriptor)) {
// Map unknown values to a special UNRECOGNIZED value if supported. // Map unknown values to a special UNRECOGNIZED value if supported.
variables->insert( variables->insert(
{"unrecognized_value", {"unrecognized_value",
@ -248,7 +248,7 @@ void ImmutableMapFieldLiteGenerator::GenerateInterfaceMembers(
"$deprecation$$value_enum_type$ ${$get$capitalized_name$OrThrow$}$(\n" "$deprecation$$value_enum_type$ ${$get$capitalized_name$OrThrow$}$(\n"
" $key_type$ key);\n"); " $key_type$ key);\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
variables_, variables_,
"/**\n" "/**\n"
@ -432,7 +432,7 @@ void ImmutableMapFieldLiteGenerator::GenerateMembers(
" return $name$ValueConverter.doForward(map.get(key));\n" " return $name$ValueConverter.doForward(map.get(key));\n"
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
variables_, variables_,
"/**\n" "/**\n"
@ -556,7 +556,7 @@ void ImmutableMapFieldLiteGenerator::GenerateMembers(
" internalGetMutable$capitalized_name$(),\n" " internalGetMutable$capitalized_name$(),\n"
" $name$ValueConverter);\n" " $name$ValueConverter);\n"
"}\n"); "}\n");
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
WriteFieldDocComment(printer, descriptor_); WriteFieldDocComment(printer, descriptor_);
printer->Print( printer->Print(
variables_, variables_,
@ -583,8 +583,8 @@ void ImmutableMapFieldLiteGenerator::GenerateFieldInfo(
printer->Print(variables_, printer->Print(variables_,
"\"$name$_\",\n" "\"$name$_\",\n"
"$default_entry$,\n"); "$default_entry$,\n");
if (!SupportUnknownEnumValue(descriptor_) && if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM &&
GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { !SupportUnknownEnumValue(descriptor_)) {
PrintEnumVerifierLogic(printer, ValueField(descriptor_), variables_, PrintEnumVerifierLogic(printer, ValueField(descriptor_), variables_,
/*var_name=*/"$value_enum_type$", /*var_name=*/"$value_enum_type$",
/*terminating_string=*/",\n", /*terminating_string=*/",\n",
@ -711,7 +711,7 @@ void ImmutableMapFieldLiteGenerator::GenerateBuilderMembers(
" return this;\n" " return this;\n"
"}\n"); "}\n");
printer->Annotate("{", "}", descriptor_); printer->Annotate("{", "}", descriptor_);
if (SupportUnknownEnumValue(descriptor_->file())) { if (SupportUnknownEnumValue(descriptor_)) {
printer->Print( printer->Print(
variables_, variables_,
"/**\n" "/**\n"

Loading…
Cancel
Save