Skip to content

compiler serial warnings when compiling generated source with Java 18 #9673

@xvrl

Description

@xvrl

What version of protobuf and what language are you using?
Version: 3.20.0-rc-1
Language: Java

What operating system (Linux, Windows, ...) and version?
macOS 12.3

What runtime / compiler are you using (e.g., python version or gcc version)
using com.google.protobuf:protoc:3.20.0-rc-1:exe:osx-x86_64 compiler artifact
invoked via org.xolstice.maven.plugins:protobuf-maven-plugin:0.6.1

% java -version
openjdk version "18" 2022-03-22
OpenJDK Runtime Environment (build 18+36-2087)
OpenJDK 64-Bit Server VM (build 18+36-2087, mixed mode, sharing)

What did you do?
Compile the code generated from the following proto definition:

example.proto

syntax = "proto3";

package test;

option java_package = "org.example.test";
option java_outer_classname = "TestProto";
option java_multiple_files = true;

message KeyValuePair {
  string key = 1;
  string value = 2;
}

What did you expect to see

No compiler warnings when compiling the generated Java source code.

What did you see instead?

As part of https://bugs.openjdk.java.net/browse/JDK-8274336 https://bugs.openjdk.java.net/browse/JDK-8274335 Java 18 added additional linter checks.

Java 18 reports the following warning for generated source files, and fails compilation if -Werror is specified:

KeyValuePair.java:[19,4] [serial] non-transient instance field of a serializable class declared with a non-serializable type

See KeyValuePair.java.txt for the generated source

Anything else we should know about your project / environment

Today possible workarounds are to either:
a) not pass -Werror to the Java compiler
b) mangle the source code generated by protoc using something like the maven-replacer-plugin, to add the necessary @SuppressWarnings annotations, which is rather ugly and brittle
e.g.

    <plugin>
        <groupId>com.google.code.maven-replacer-plugin</groupId>
        <artifactId>maven-replacer-plugin</artifactId>
        <executions>
          <execution>
            <phase>generate-sources</phase>
            <goals>
              <goal>replace</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <includes>
            <include>target/generated-sources/**/*.java</include>
          </includes>
          <regex>true</regex>
          <regexFlags>
            <regexFlag>MULTILINE</regexFlag>
          </regexFlags>
          <replacements>
             <replacement> 
               <token>public final class</token> 
               <value>@SuppressWarnings("serial") public final class</value> 
             </replacement> 
          </replacements>
        </configuration>
      </plugin>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions