Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a @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 do you have to always use @Nonnull?

like image 312
Philipp Claßen Avatar asked Jun 05 '13 11:06

Philipp Claßen


People also ask

How do I annotate in IntelliJ?

You can also annotate a particular file from the file history view. In the History tab, select the file version you want to review, right-click the corresponding line and select Annotate from the context menu.

What is the use of @nullable annotation?

The @Nullable annotation helps you detect: Method calls that can return null. Variables (fields, local variables, and parameters), that can be null.

What does @NonNull annotation do?

@NonNull – The compiler can determine cases where a code path might receive a null value, without ever having to debug a NullPointerException. @ReadOnly – The compiler will flag any attempt to change the object. This is similar to Collections.

What are inferred annotations?

Inferred annotations These annotations can help you better understand code contacts and make code analysis more efficient. IntelliJ IDEA infers the @Nullable and @NotNull annotations that you can use to analyze your source code to spot places where you overlooked null.


1 Answers

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

Unfortunately, this doesn't (currently) recursively affect subpackages, so each subpackage has to have a package-info.java file too, declaring that subpackage to use the annotation.

See here for details and an example of use:

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

Note that this is already implemented in Early Access Program (EAP) builds.

like image 173
Some Guy Avatar answered Nov 06 '22 00:11

Some Guy