Is there an @NonNullByDefault annotation in IDEA?

Eclipse has the @NonNullByDefault annotation, which treats all values ​​as @NonNull unless you explicitly annotate them as @Nullable .

Is there an equivalent option in IntelliJ IDEA, or should you always use @NonNull ?

+6
source share
2 answers

No, it is not currently supported by IDEA.

For evidence, see the lena link for a function request that opens to allow "NotNull" as the default behavior of an element for a given class or package .

Perhaps a similar feature will become standard with JSR-305 , which may include @ParametersAreNonnullByDefault , as well as the reverse annotation @ParametersAreNullableByDefault . Note that unlike @NonNullByDefault , return values ​​are not covered by these two annotations. Thus, you still had to explicitly annotate the return value.

Anything that does not change the current state. Neither JSR-305 became a standard, nor did IDEA implement it.

+3
source

Idea 14 includes support for JSR 305 annotation "@TypeQualifierDefault", which allows the user to create a custom annotation that will be used in the package declaration in the package-info.java file, which indicates that everything in this package (not just parameters, but return values methods, local variables, etc.) will be implicitly annotated as not allowing zero values.

Unfortunately, this does not recursively affect subpackages, so each subpackage must have a package-info.java file, declaring that the subpackage should use annotation.

See here for details and usage example:

http://youtrack.jetbrains.com/issue/IDEA-125281

Note that this is already implemented in versions of the Early Access Program (EAP).

+7
source

Source: https://habr.com/ru/post/946583/


All Articles