Thursday, August 25, 2011

@Nullable / @Null / @NotNull / @Notnull / @Nonnull / @CheckForNull annotations


Jetbrains never ceases to delight. As of 10.5, their IDE supports not only their own pioneering org.jetbrains.annotations Nullable annotations, but also any other arbitrary @Nullable annotations you decide to use.

Currently the most popular ones are the JSR-305 (javax.annotation) annotations, the FindBugs nullable annotations, and IntelliJ's own nullable annotations.

If you decide to use the Findbugs annotations (which are found in package edu.umd.cs.findbugs.annotations), keep in mind that you probably want to use @CheckForNull instead of @Nullable. By default IntelliJ includes @Nullable for Findbugs, but you should change that to @CheckForNull.

The reason is that @Nullable is all but ignored by Findbugs if you decide to use that as well. Findbugs' annotations seem badly named, but hey that's just the way it is.

On a related subject, be careful not to get the JSR-303 nullable annotations confused with the JSR-305 nullable annotations.

Here is a quick summary of the differences:

JSR-303

  • Used for runtime bean validation, NOT static code analysis
  • javax.validation.constraints.NotNull
  • javax.validation.constraints.Null


JSR-305

  • Used for Static code analysis - seems to be all but dead
  • javax.annotation.Nonnull
  • javax.annotation.Nullable


Findbugs

  • Used by the Findbugs static code analysis tool
  • edu.umd.cs.findbugs.annotations.NonNull
  • edu.umd.cs.findbugs.annotations.Nullable (Probably not what you want)
  • edu.umd.cs.findbugs.annotations.CheckForNull


IntelliJ

  • Used by IntelliJ IDEA, but also publicly available as a jar
  • org.jetbrains.annotations.Nullable
  • org.jetbrains.annotations.NonNull



As usual, IntelliJ's built-in IDE support for nullable is awesome, and Eclipse's is decidedly un-awesome.


0 comments: