IntelliJ IDEA nullability annotations inspection

Build Status Build Status Version Downloads Downloads last month

Reports fields, parameters and return values of methods which do not have a nullability annotation, like @NotNull or @Nullable.1

Quick fixes are presented to add one of the annotations to the elements with a problem.

example

You can enable/disable the inspection for fields and private methods via Preferences | Editor | Inspections

example

When your project uses default annotations like @ParamsAreNonnulByDefault1 by means of JSR305' javax.annotation.meta.TypeQualifierDefault, a quick fix will be presented to apply the annotation to the current package by adding the annotation to the package-info.java. If a package-info.java does not exist yet it will be created automatically.

An example of your custom default annotation for your project could be something like this:

package com.stylismo.example;

@Documented
@Nonnull
@TypeQualifierDefault({
        ElementType.FIELD,
        ElementType.METHOD,
        ElementType.PARAMETER,
})
@Retention(RUNTIME)
public @interface NonnullByDefault {
}

Applying this @NonnullByDefault annotation to a package will result in all fields, methods and parameters to be non-null, unless an element is annotated with one of the registered @Nullable annotations or the method is overriding a method in a superclass.

By default all redundant annotations will be removed after applying the default annotation to the package. See related issue for the details here.

Only annotations registered with IntelliJ will be suggested in the quick fixes. Go to Preferences | Build | Compiler | Configure annotations ... to register your own.

example

1: Any nullability annotation can be used, like JSR-305, JetBrains' or your own as long as you configure them in IDEA.