IntelliJ IDEA 2021.1 Help

@ParametersAreNonnullByDefault

The @ParametersAreNonnullByDefault annotation helps you define that all method parameters in a class or a package have @NotNull semantic, unless they are explicitly annotated with the @Nullable annotation.

The @ParametersAreNonnullByDefault annotation can be used with a package, a class, or a method.

To use the annotation, add the jsr305 library to module dependencies:

  1. Open the Project Structure dialog Ctrl+Alt+Shift+S, and go to Modules | Dependencies.

  2. Click Add Icons general add and select Library | Java.

  3. In the IntelliJ IDEA home directory, select lib\jsr305.jar.

  4. (Optionally) In the next dialog, you can modify library name and level.

  5. Apply the changes and close the dialog.

Once you add the JAR to your project, you can start using the @ParametersAreNonnullByDefault annotation. For 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; } }

If you annotate the sort() method with @ParametersAreNonnullByDefault, IntelliJ IDEA immediately recognizes that if statement is extraneous, and reports about the condition that is always true.

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

Paramnotnull

Last modified: 08 March 2021