[BUG] lombok imports do not work with module imports.
Describe the bug
The annotation @NonNull does not works when imported via a module import (import module lombok;)
To Reproduce Consider these classes:
package somewhere;
import module lombok; // Note the import style!
public class Dummy1 {
public static void foo(@NonNull String x) {
if (1 == 1 && x == null) throw new AssertionError("Ops!, @NonNull failed to do its work in 1");
}
}package somewhere;
import lombok.NonNull; // Note the import style!
public class Dummy2 {
public static void foo(@NonNull String x) {
if (1 == 1 && x == null) throw new AssertionError("Ops!, @NonNull failed to do its work in 2");
}
}package somewhere;
public class Dummy3 {
public static void main(String[] args) {
try {
Dummy1.foo(null);
IO.println("1 is nuts");
} catch (IllegalArgumentException e) {
IO.println("1 is ok");
} catch (AssertionError e) {
IO.println(e.getMessage());
}
try {
Dummy2.foo(null);
IO.println("2 is nuts");
} catch (IllegalArgumentException e) {
IO.println("2 is ok");
} catch (AssertionError e) {
IO.println(e.getMessage());
}
}
}This is the output with lombok 1.18.42 and Java 25:
Ops!, @NonNull failed to do its work in 1
2 is okExpected behavior
@NonNull should work with module imports. This is a very unexpected bug.
Version info:
- Lombok 1.18.42
- Java 25.
- [I'm also using gradle 9.1.0 and netbeans 27, but I don't think that this matters.]
Aditional context
This bug is very evil, as it silently disables @NonNull, but only sometimes (when imported in a module import) and its cause is very surprising, unexpected, subtle and unintuitive. However, since Java 25 is still fresh new today, few people would be caught by that for now.
I needed to resort to a lot of debugging and head-banging in order to understand what was wrong in my project. For now, the workaround is to always add import lombok.NonNull;. This work together with import module lombok; if you are also using other lombok features.
Also note that testing this bug with JUnit 6 isn't easy because NonNull is ambiguous, since it also appears in JSpecify and that forces the developer to put import lombok.NonNull; in the code, silencing up the bug. To test this with JUnit, a multi-module project setup is necessary.
Source: projectlombok/lombok