From 330a94b46ae0fd3c4697889b587c66f328c1fc53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20L=C3=A9aut=C3=A9?= Date: Tue, 13 Sep 2022 16:09:14 -0700 Subject: [PATCH] fix serialization warnings in generated code when compiling with Java version 18 and above Java 18 added additional linter checks as part of https://bugs.openjdk.java.net/browse/JDK-8274336 and https://bugs.openjdk.java.net/browse/JDK-8274335 These additional checks cause generated protobuf code to raise the following compiler warning: "non-transient instance field of a serializable class declared with a non-serializable type" This change fixes the code generation to annotate the generated fields with the necessary suppressions to avoid false positives. All the code generated from src/google/protobuf/*.proto now compiles successfully without serialization warnings in Java 18. fixes #9673 --- src/google/protobuf/compiler/java/enum_field.cc | 1 + src/google/protobuf/compiler/java/map_field.cc | 1 + src/google/protobuf/compiler/java/message.cc | 2 ++ src/google/protobuf/compiler/java/message_field.cc | 3 ++- src/google/protobuf/compiler/java/primitive_field.cc | 3 ++- src/google/protobuf/compiler/java/string_field.cc | 4 +++- 6 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/google/protobuf/compiler/java/enum_field.cc b/src/google/protobuf/compiler/java/enum_field.cc index ea74d8bc6f..14f6878595 100644 --- a/src/google/protobuf/compiler/java/enum_field.cc +++ b/src/google/protobuf/compiler/java/enum_field.cc @@ -680,6 +680,7 @@ void RepeatedImmutableEnumFieldGenerator::GenerateMembers( io::Printer* printer) const { printer->Print( variables_, + "@SuppressWarnings(\"serial\")\n" "private java.util.List $name$_;\n" "private static final " "com.google.protobuf.Internal.ListAdapter.Converter<\n" diff --git a/src/google/protobuf/compiler/java/map_field.cc b/src/google/protobuf/compiler/java/map_field.cc index 66d7fbb9c5..d4d90919eb 100644 --- a/src/google/protobuf/compiler/java/map_field.cc +++ b/src/google/protobuf/compiler/java/map_field.cc @@ -335,6 +335,7 @@ void ImmutableMapFieldGenerator::GenerateMembers(io::Printer* printer) const { " $value_default_value$);\n" "}\n"); printer->Print(variables_, + "@SuppressWarnings(\"serial\")\n" "private com.google.protobuf.MapField<\n" " $type_parameters$> $name$_;\n" "private com.google.protobuf.MapField<$type_parameters$>\n" diff --git a/src/google/protobuf/compiler/java/message.cc b/src/google/protobuf/compiler/java/message.cc index 52768bb477..5a32a60498 100644 --- a/src/google/protobuf/compiler/java/message.cc +++ b/src/google/protobuf/compiler/java/message.cc @@ -440,6 +440,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { // oneofCase_ and oneof_ printer->Print(vars, "private int $oneof_name$Case_ = 0;\n" + "@SuppressWarnings(\"serial\")\n" "private java.lang.Object $oneof_name$_;\n"); // OneofCase enum printer->Print( @@ -1740,6 +1741,7 @@ void ImmutableMessageGenerator::GenerateAnyMethods(io::Printer* printer) { " defaultInstance.getDescriptorForType().getFullName());\n" "}\n" "\n" + "@SuppressWarnings(\"serial\")\n" "private volatile com.google.protobuf.Message cachedUnpackValue;\n" "\n" "@java.lang.SuppressWarnings(\"unchecked\")\n" diff --git a/src/google/protobuf/compiler/java/message_field.cc b/src/google/protobuf/compiler/java/message_field.cc index 32da1b74eb..78cdcca9f5 100644 --- a/src/google/protobuf/compiler/java/message_field.cc +++ b/src/google/protobuf/compiler/java/message_field.cc @@ -910,7 +910,8 @@ void RepeatedImmutableMessageFieldGenerator::GenerateInterfaceMembers( void RepeatedImmutableMessageFieldGenerator::GenerateMembers( io::Printer* printer) const { - printer->Print(variables_, "private java.util.List<$type$> $name$_;\n"); + printer->Print(variables_, "@SuppressWarnings(\"serial\")\n" + "private java.util.List<$type$> $name$_;\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, diff --git a/src/google/protobuf/compiler/java/primitive_field.cc b/src/google/protobuf/compiler/java/primitive_field.cc index 3706ef164f..deb69ad57a 100644 --- a/src/google/protobuf/compiler/java/primitive_field.cc +++ b/src/google/protobuf/compiler/java/primitive_field.cc @@ -715,7 +715,8 @@ void RepeatedImmutablePrimitiveFieldGenerator::GenerateInterfaceMembers( void RepeatedImmutablePrimitiveFieldGenerator::GenerateMembers( io::Printer* printer) const { - printer->Print(variables_, "private $field_list_type$ $name$_;\n"); + printer->Print(variables_, "@SuppressWarnings(\"serial\")\n" + "private $field_list_type$ $name$_;\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER); printer->Print(variables_, diff --git a/src/google/protobuf/compiler/java/string_field.cc b/src/google/protobuf/compiler/java/string_field.cc index d8b5d4ef6f..f9ad91e47c 100644 --- a/src/google/protobuf/compiler/java/string_field.cc +++ b/src/google/protobuf/compiler/java/string_field.cc @@ -218,7 +218,8 @@ void ImmutableStringFieldGenerator::GenerateInterfaceMembers( void ImmutableStringFieldGenerator::GenerateMembers( io::Printer* printer) const { - printer->Print(variables_, "private volatile java.lang.Object $name$_;\n"); + printer->Print(variables_, "@SuppressWarnings(\"serial\")\n" + "private volatile java.lang.Object $name$_;\n"); PrintExtraFieldInfo(variables_, printer); if (HasHazzer(descriptor_)) { @@ -801,6 +802,7 @@ void RepeatedImmutableStringFieldGenerator::GenerateInterfaceMembers( void RepeatedImmutableStringFieldGenerator::GenerateMembers( io::Printer* printer) const { printer->Print(variables_, + "@SuppressWarnings(\"serial\")\n" "private com.google.protobuf.LazyStringList $name$_;\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER);