Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] @Builder inside @UtilityClass fails to comple (Java 8 only) #3496

Open
etanol opened this issue Sep 7, 2023 · 0 comments
Open

[BUG] @Builder inside @UtilityClass fails to comple (Java 8 only) #3496

etanol opened this issue Sep 7, 2023 · 0 comments

Comments

@etanol
Copy link

etanol commented Sep 7, 2023

Describe the bug

When defining a nested class inside a class annotated with @UtilityClass fails when the nested class is annotated with @Builder. The compiler (javac) reports the following error, with no file or line information:

error: modifier static not allowed here

This only seems to be failing using JDK8. I tried JDK17 an couldn't reproduce. It also happens only with direct Lombok code generation. Executing delombok and then compiling the result works.

To Reproduce

Try to build the following code with a Java 8 compiler:

import lombok.experimental.UtilityClass;
import lombok.Builder;

@UtilityClass
public class Sample1 {
        @Builder
        public class Nested {
                private int x;
        }
}

Executing delombok (with the same JDK version) also prints the error message mentioned above, but still manages to print the following code:

// Generated by delombok at Thu Sep 07 11:59:34 CEST 2023

public final class Sample1 {

        public static class Nested {
                private int x;

                @java.lang.SuppressWarnings("all")
                Nested(final int x) {
                        this.x = x;
                }


                @java.lang.SuppressWarnings("all")
                public static class NestedBuilder {
                        @java.lang.SuppressWarnings("all")
                        private int x;

                        @java.lang.SuppressWarnings("all")
                        NestedBuilder() {
                        }

                        /**
                         * @return {@code this}.
                         */
                        @java.lang.SuppressWarnings("all")
                        public Sample1.Nested.NestedBuilder x(final int x) {
                                this.x = x;
                                return this;
                        }

                        @java.lang.SuppressWarnings("all")
                        public Sample1.Nested build() {
                                return new Sample1.Nested(this.x);
                        }

                        @java.lang.Override
                        @java.lang.SuppressWarnings("all")
                        public java.lang.String toString() {
                                return "Sample1.Nested.NestedBuilder(x=" + this.x + ")";
                        }
                }

                @java.lang.SuppressWarnings("all")
                public static Sample1.Nested.NestedBuilder builder() {
                        return new Sample1.Nested.NestedBuilder();
                }
        }

        @java.lang.SuppressWarnings("all")
        private Sample1() {
                throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated");
        }
}

And this code does compile correctly.

Expected behavior

Since JDK 17 and delomboking works. The expected behavior is the compilation to work in Java 8.

Version info (please complete the following information):

  • Lombok version: 1.18.26
  • Platform: Amazon Corretto
    • javac 1.8.0_372 (fails)
    • javac 17.0.7 (works)

Additional context

I believe since Java 8 is nearing it's end of life, this will be considered low priority. Still, I thought it could at least document one failing corner case for other potential developers facing the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant