WHICH @NOTNULL JAVA ANNOTATION SHOULD I USE?

Mikki Halpin

Total Post:23

Points:161
Posted by  Mikki Halpin
Java 
 1167  View(s)
Ratings:
Rate this:
I'm looking to make my code more readable as well as use tooling like IDE code inspection and/or static code analysis (FindBugs and Sonar) to avoid NullPointerExceptions. Many of the tools seem incompatible with each others' @NotNull/@NonNull/@Nonnull annotation and listing all of them in my code would be terrible to read. Any suggestions of which one is the 'best'? Here is the list of equivalent annotations I've found:

javax.validation.constraints.NotNull
Created for runtime validation, not static analysis.
documentation

edu.umd.cs.findbugs.annotations.NonNull
Used by Findbugs static analysis and therefore Sonar
documentation

javax.annotation.Nonnull
This might work with Findbugs too, but JSR-305 is inactive.
source

com.intellij.annotations.NotNull
Used by IntelliJ IDEA IDE for static analysis.
documentation

lombok.NonNull
Used to control code generation in Project Lombok.
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: Which @NotNull Java annotation should I use?

    For Android projects you should use android.support.annotation.NonNull and android.support.annotation.Nullable. These and other helpful Android-specific annotations are available in the Support Library.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!