or: How I learned to stop worrying and love the NullPointerException.
@NonNullwas introduced in lombok v0.11.10.
You can use
@NonNull on the parameter of a method or constructor to have lombok generate a null-check statement for you.
Lombok has always treated any annotation named
@NonNull on a field as a signal to generate a null-check if lombok generates an entire method or constructor for you, via for example
@Data. Now, however, using lombok's own
@lombok.NonNull on a parameter results in the insertion of just the null-check statement inside your own method or constructor.
The null-check looks like
if (param == null) throw new NullPointerException("param"); and will be inserted at the very top of your method. For constructors, the null-check will be inserted immediately following any explicit
If a null-check is already present at the top, no additional null-check will be generated.
Supported configuration keys:
When lombok generates a null-check
ifstatement, by default, a
java.lang.NullPointerExceptionwill be thrown with the field name as the exception message. However, you can use
IllegalArgumentExceptionin this configuration key to have lombok throw that exception, with 'fieldName is null' as exception message.
error] (default: not set)
Lombok will flag any usage of
@NonNullas a warning or error if configured.
Lombok's detection scheme for already existing null-checks consists of scanning for if statements that look just like lombok's own. Any 'throws' statement as the 'then' part of the if statement, whether in braces or not, counts. The conditional of the if statement must look exactly like
PARAMNAME == null. The first statement in your method that is not such a null-check stops the process of inspecting for null-checks.
@Data and other method-generating lombok annotations will trigger on any annotation named
@NonNull regardless of casing or package name, this feature only triggers on lombok's own
@NonNull annotation from the
@NonNull on a primitive parameter results in a warning. No null-check will be generated.
@NonNull on a parameter of an abstract method used to generate a warning; starting with version 1.16.8, this is no longer the case, to acknowledge the notion that
@NonNull also has a documentary role. For the same reason, you can annotate a method as
@NonNull; this is allowed, generates no warning, and does not generate any code.