[FEATURE] Support JSpecify @NullMarked
Author: xav73400Created Apr 1, 2025Updated Sep 1, 2026
Labelsaccepted
Describe the feature
Support of the JSpecify's @NullMarked annotation when generating constructors. Goal: creating class instances with all non-nullable fields set and not null after instantiation.
Lomboked version:
import org.jspecify.annotations.NullMarked;
@NullMarked
@RequiredArgsConstructor
public class Bean {
private String s;
}Delomboked version:
import org.jspecify.annotations.NullMarked;
@NullMarked
public class Bean {
private String s;
@Generated
public Bean(String s) {
if (s == null) {
throw new NullPointerException("s is marked non-null but is null");
} else {
this.s = s;
}
}
}Describe the target audience Users of JSpecify annotation.
Additional context
In JSpecify specification, annotating a class with @NullMarked indicates that all its fields are not nullable (except if they are annotated with @Nullable).
However, the @NullMarked can also been applied directly on a package through its package-info.java. In that case, all classes under this package are implicitly marked @NullMarked as well and so the same constructor generation rules as above should apply.
Source: projectlombok/lombok