Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I use Lombok-Annotation @NonNull?

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?

like image 455
metters Avatar asked Jun 24 '19 10:06

metters


People also ask

Why we are using Lombok?

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 are Lombok annotations?

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.

What is the function of the NOT null constructor?

@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.

Should we use Lombok or not?

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.

Why does post Lombok still have the @nonnull annotation?

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.

What is the use of @nonnull annotation on fields?

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.

How to create a null-check statement with Lombok?

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.

Can Lombok generate withx () method if it already exists in class?

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:


2 Answers

Writing a type annotation such as @NonNull serves several purposes.

  • It is documentation: it communicates the method's contract to clients, in a more concise and precise way than Javadoc text.
  • It enables run-time checking -- that is, it guarantees that your program crashes with a useful error message (rather than doing something worse) if a buggy client mis-uses your method. Lombok does this for you, without forcing the programmer to write the run-time check. The referenced example shows the two ways to do this: with a single @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.
  • It enables compile-time checking. A tool such as the Checker Framework gives a guarantee that the code will not crash at run time. Tools such as NullAway, Error Prone, and FindBugs are heuristic bug-finders that will warn you about some mis-uses of null but do not give you a guarantee.
like image 194
mernst Avatar answered Oct 09 '22 07:10

mernst


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, @NonNulls are intentionally kept for the purposes of documentation and possible processing by other tools.

like image 2
Sasha Avatar answered Oct 09 '22 08:10

Sasha