IntelliJ IDEA 14.0.0 Web 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:

notnull_default_annotation1

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

notnull_default_annotation2

See Also

Last modified: 12 November 2014
comments powered by Disqus