IntelliJ IDEA 2016.3 Help

@ParametersAreNonnullByDefault Annotation

Overview

Annotation @ParametersAreNonnullByDefault gives the developer an option to define that for a given class or package all the elements (methods, parameters, fields and variables) have @NotNull semantic, unless they are explicitly annotated with the @Nullable annotation.

This is done by adding annotation @javax.annotation.ParametersAreNonnullByDefault to the entire package, class, or method.

To use @javax.annotation.ParametersAreNonnullByDefault annotation, make sure that jsr305 jars are added to the module libraries.

Example

Consider the following code:

public static <T extends Comparable<T>> List<T> sort(List<T> list) { if(list != null){ List<T> copy = new ArrayList<T>(list); sort(copy); return copy; } else { return null; } }

IntelliJ IDEA doesn't complain.

Let's decorate the sort() method with @ParametersAreNonnullByDefault annotation, which means that all the symbols of this method behave as if they were @NotNull.

IntelliJ IDEA immediately recognizes that if statement is extraneous, and reports about the condition that is always true:

/help/img/idea/2016.3/notnull_default_annotation1.png

However, if you decorate the parameter of the method sort() as nullable, you will see no inspection messages:

/help/img/idea/2016.3/notnull_default_annotation2.png

See Also

Last modified: 21 March 2017