Lombok offers the annotation @NonNull
which executes the nullcheck and throws a NPE (if not configured differently).
I do not understand why I would use that annotation as described in the example of that documentation:
private String name;
public NonNullExample(@NonNull Person person) {
super("Hello");
if (person == null) {
throw new NullPointerException("person is marked @NonNull but is null");
}
this.name = person.getName();
}
The NPE would be thrown anyway. The only reason here to use the annotation imo is if you would want the exception to be different from a NPE.
EDIT: I do know that the Exception would be thrown explicitly and thus 'controlled', but at least the text of the error message should be editable, shouldn't it?
Lombok is used to reduce boilerplate code for model/data objects, e.g., it can generate getters and setters for those object automatically by using Lombok annotations. The easiest way is to use the @Data annotation.
What is Lombok. Project Lombok (from now on, Lombok) is an annotation-based Java library that allows you to reduce boilerplate code. Lombok offers various annotations aimed at replacing Java code that is well known for being boilerplate, repetitive, or tedious to write.
@NotNull The @NotNull annotation is, actually, an explicit contract declaring that: A method should not return null. Variables (fields, local variables, and parameters) cannot hold a null value.
Lombok is just a tool to make your life easy. It does not provide any assistance or support for clean code. If clean code and design principles are not of importance (trust me it's not that bad as it sounds in most of the cases), then using Lombok to simplify development process is the best option.
The reason the 'post lombok' example still has the nonnull annotation is that, unlike most other lombok annotations, if you delombok this code we leave the annotation in. That's because, as @mernst said, The @NonNull annotation ALSO serves a documentation and 'input for linters' purpose, which most other lombok annotations don't do.
NonNull annotation on fields will generate null check for starting of setter method body and throws NullPointerException with appropriate exception message. Making primitive fields @NonNull is meaningless, will get compiler warning.
But with lombok, all we need is to add @NonNull right infront of a parameter. After this, lombok will automatically generate a null-check statement right inside your own method or constructor. To give you a better understanding of this concept, let's demonstrate this with an example.
We should be mindful that in addition to fields that start with the $ sign, Lombok will not generate a withX () method if it already exists in our class:
Writing a type annotation such as @NonNull
serves several purposes.
@NonNull
annotation or with an explicit programmer-written check. The "Vanilla Java" version either has a typo (a stray @NonNull
) or shows the code after Lombok processes it.IMHO, you've understood that documentation page wrongly.
That documentation page doesn't imply that you are recommended to use both Lombok @NonNull
annotations and explicit if (smth == null) throw …
-like checks as the same time (in the same method).
It just says that a code like this one (let's call it code A):
import lombok.NonNull;
public class NonNullExample extends Something {
private String name;
public NonNullExample(@NonNull Person person) {
super("Hello");
this.name = person.getName();
}
}
will be automatically (internally) translated by Lombok into a code like the one quoted the question (let's call it code B).
But that documentation page doesn't say that it would make sense for you to explicitly write the code B (though you are allowed; and Lombok will even try to prevent double check in this case). It just says that with Lombok you are now able to write the code A (and how it will work — it will be implicitly converted into the code B).
Note, that the code B is a “vanilla Java” code. It isn't expected to be processed by the Lombok for the second time. So @NonNull
in the code B is just a plain annotation, which has no influence on the behavior (at least, not by Lombok means).
It's a separate question why Lombok works in that way — why it doesn't remove @NonNull
from the generated code. Initially I even thought that it might be a bug in that documentation page. But, as Lombok author explains in his comment, @NonNull
s are intentionally kept for the purposes of documentation and possible processing by other tools.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With