projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.73k stars 2.36k forks source link

[BUG] Builder default for a field with an annotated generic type parameter throws Compiler exception in Java 14 #2728

Open mjustin opened 3 years ago

mjustin commented 3 years ago

Describe the bug

The build fails with a "compiler bug" when using @Builder.Default with an annotated generic type parameter on Java 14. This bug does not occur in Java 11 (the latest LTS version), 13, or 15.

If the code is delomboked and then compiled, the code compiles successfully.

@Builder
public class Test {
    @Builder.Default
    private final List<@NotNull String> problematicProperty = List.of("A", "B", "C");
}

To Reproduce

Create a directory with the given Test.java file. Ensure you are using Java 14 (This was tested using the Hotspot AdoptOpenJDK version 14.0.2 as installed by SDKMAN! per sdk install java 14.0.2.hs-adpt).

Test.java

import lombok.Builder;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.List;

@Builder
public class Test {
    @Builder.Default
    private final List<@NotNull String> problematicProperty = List.of("A", "B", "C"); // This causes the exception

    private final List<@NotNull String> workingPropertyNoDefault = List.of("A", "B", "C"); // No problems with this one

    @Builder.Default
    private final List<String> workingPropertyNoAnnotation = List.of("A", "B", "C"); // No problems with this one

    @Target({ElementType.TYPE_USE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface NotNull { }
}

Running Lombok against the test file throws an error. Delomboking the test file and running that does not throw any error.

Test commands

$ javac -version
javac 14.0.2

$ java --version
openjdk 14.0.2 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.2+12)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.2+12, mixed mode, sharing)

$ javac -cp lombok-1.18.16.jar Test.java
An exception has occurred in the compiler (14.0.2). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
    at jdk.compiler/com.sun.tools.javac.jvm.ClassWriter.writePosition(ClassWriter.java:671)
    at jdk.compiler/com.sun.tools.javac.jvm.ClassWriter.writeTypeAnnotation(ClassWriter.java:648)
    at jdk.compiler/com.sun.tools.javac.jvm.ClassWriter.writeTypeAnnotations(ClassWriter.java:552)
    at jdk.compiler/com.sun.tools.javac.jvm.ClassWriter.writeCode(ClassWriter.java:1104)
    at jdk.compiler/com.sun.tools.javac.jvm.ClassWriter.writeMethod(ClassWriter.java:972)
    at jdk.compiler/com.sun.tools.javac.jvm.ClassWriter.writeMethods(ClassWriter.java:1463)
    at jdk.compiler/com.sun.tools.javac.jvm.ClassWriter.writeClassFile(ClassWriter.java:1568)
    at jdk.compiler/com.sun.tools.javac.jvm.ClassWriter.writeClass(ClassWriter.java:1489)
    at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:757)
    at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1646)
    at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1614)
    at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:972)
    at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:316)
    at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:176)
    at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:57)
    at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:43)
printing javac parameters to: /path/to/javac.20210127_121326.args

$ java -jar lombok-1.18.16.jar delombok Test.java -d delombok

$ javac -cp lombok-1.18.16.jar delombok/Test.java

Expected behavior

The build should succeed, producing the same end result as when compiled on other versions of Java.

Version info (please complete the following information):

Additional context

It's very possible this is a javac bug as reported in the output, and not a Lombok one. However, as the delomboked code compiles I can't easily track down what that bug might be, as I'm not sure if it's reproducible with pure Java code, or what code would trigger the bug.

Furthermore, since Java 14 is not a LTS and it works in Java 15, even if this is an issue with Lombok and not javac, it may not be worth fixing.

Rawi01 commented 3 years ago

Duplicate of #2539

I already spend some time debugging that but it is not that easy as it is something that happens at the very end of the compilation which is not covered by unit tests. Great news that this works in Java 15, if I can figure out what they changed lombok might be able to fix that in Java 14 too.

mjustin commented 3 years ago

Duplicate of #2539

I already spend some time debugging that but it is not that easy as it is something that happens at the very end of the compilation which is not covered by unit tests. Great news that this works in Java 15, if I can figure out what they changed lombok might be able to fix that in Java 14 too.

Thanks. I missed it when searching for existing issues, I think mainly because I was looking for generics specifically in my search.