Lombok @NonNull Annotation
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 this() or super() calls.
If a null-check is already present at the top, no additional null-check will be generated.
Note that, if a null check is already present at the top of a method or constructor, it will not be generated by lombok. Let’s consider a simple example –
class User { @Getter @Setter @NonNull private String name;}
The above annotated code is equivalent to the following java code –
class User { @Getter @Setter @NonNull private String name; @NonNull public String getName() { return name; } public void setName(@NonNull final String name) { if(name == null) throw new NullPointerException("name"); this.name = name; }}