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] @UtilityClass fails to make inner class static in Java 9+ #3097

Closed
liblit opened this issue Jan 26, 2022 · 4 comments · Fixed by #3101
Closed

[BUG] @UtilityClass fails to make inner class static in Java 9+ #3097

liblit opened this issue Jan 26, 2022 · 4 comments · Fixed by #3101
Assignees
Milestone

Comments

@liblit
Copy link

liblit commented Jan 26, 2022

Describe the bug

When a class is annotated with @UtilityClass, and this class has an inner class, the inner class is also treated as static in Java 1.8, but is not treated as static in Java 9 and later. This causes compilation failure under Java 9+ for code that compiled correctly in Java 1.8.

To Reproduce

Save the following as Outer.java:

import lombok.experimental.UtilityClass;

@UtilityClass
class Outer {

  Object buildInner() {
    return new Inner();
  }

  class Inner {}
}

Compile this file using javac from Java 1.8, and observe that compilation succeeds with no diagnostic output:

% /path/to/java-1.8/bin/javac -cp lombok.jar Outer.java

Now compile this file using javac from Java 9 or later, and observe that compilation fails:

% /path/to/java-9/bin/javac -cp lombok.jar Outer.java
Outer.java:7: error: non-static variable this cannot be referenced from a static context
    return new Inner();
           ^
1 error

Expected behavior

Java 9 and later compilers should have compiled this source file, producing no diagnostic output.

Version info (please complete the following information):

  • Lombok version: 1.18.22
  • Platform: javac 9

Additional context

The problem first arises with Java 9, but also occurs in Java 11, 14, and 17. (I haven't tested Java 10, 12, 13, 15, or 16.)

#2519 seems closely related, but was closed because it could not be reproduced. However, that earlier report mentioned nothing about different Java versions. In this new report, I find that the Java compiler version is critical: the problem only arises in Java 9 and later, not in Java 1.8.

@rzwitserloot
Copy link
Collaborator

Heh, yeah that's no doubt what happened with #2519. Nice find. No need to test on the non-LTS versions except the last for such things.

@Rawi01
Copy link
Collaborator

Rawi01 commented Jan 29, 2022

The generated code is fine but we also have to adjust the related compiler symbols, in this case we have to remove the enclosing type of the inner class.

@TysonMN
Copy link

TysonMN commented Apr 20, 2022

I think the first release containing this fix is version 1.18.24.

@xinbimingjingtai
Copy link

I need this commit to make lombok.accessors.capitalization work in IDE

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

Successfully merging a pull request may close this issue.

5 participants