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
pull/10561/head
Xavier Léauté 2 years ago
parent db38a8c2da
commit 330a94b46a
  1. 1
      src/google/protobuf/compiler/java/enum_field.cc
  2. 1
      src/google/protobuf/compiler/java/map_field.cc
  3. 2
      src/google/protobuf/compiler/java/message.cc
  4. 3
      src/google/protobuf/compiler/java/message_field.cc
  5. 3
      src/google/protobuf/compiler/java/primitive_field.cc
  6. 4
      src/google/protobuf/compiler/java/string_field.cc

@ -680,6 +680,7 @@ void RepeatedImmutableEnumFieldGenerator::GenerateMembers(
io::Printer* printer) const {
printer->Print(
variables_,
"@SuppressWarnings(\"serial\")\n"
"private java.util.List<java.lang.Integer> $name$_;\n"
"private static final "
"com.google.protobuf.Internal.ListAdapter.Converter<\n"

@ -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"

@ -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"

@ -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_,

@ -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_,

@ -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);

Loading…
Cancel
Save