9

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?

Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
  • 1
    Isn't that `@Nonnull` instead (JSR 305)? IDEA can use `@NotNull`, but this is a different annotation. – fge Jun 05 '13 at 11:10
  • By the way, what use would you make of that annotation? Is that for static source code analysis? IDEA can use FindBugs, it has a plugin for that. As to using these annotations, I follow Guice's advice: method parameters cannot be null unless annotated with `@Nullable`. But that is a question of policy... – fge Jun 05 '13 at 11:28
  • @fge Our coding policy is to use the Eclipse annotations including `@NonNullByDefault`. So, my question is whether IDEA be configured to emulate Eclipse' null analysis. `@NonNullByDefault` seems to be the tricky part. – Philipp Claßen Jun 05 '13 at 11:45
  • 1
    please, vote for http://youtrack.jetbrains.com/issue/IDEA-65566 – lena Jun 05 '13 at 14:34

2 Answers2

11

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.

Some Guy
  • 111
  • 1
  • 4
  • I can verify that this approach works with IntelliJ IDEA 2023.1.1 (Community Edition). – kol Jun 09 '23 at 14:26
3

No, it is currently not supported by IDEA.

As a proof, see lena's link about the open feature request to allow 'NotNull' as the default element behavior for a given class or package.

Maybe a similar feature will be become standard with JSR-305, which may include the @ParametersAreNonnullByDefault annotation and also the opposite annotation @ParametersAreNullableByDefault. Note that in contrast to @NonNullByDefault, return values are not covered by those two annotations. So, you still had to annotate the return value explicitely.

All that doesn't change the current state, though. Neither has JSR-305 become a standard, nor does IDEA implement it.

Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
  • 5
    IDEA now support @ParametersAreNonnullByDefault: http://www.jetbrains.com/idea/webhelp/@parametersarenonnullbydefault-annotation.html – Lodewijk Bogaards Jul 16 '14 at 13:10
  • ^ link in comment is broken... this one works (for now...): https://www.jetbrains.com/help/idea/2017.1/parametersarenonnullbydefault-annotation.html – Lambart Mar 25 '17 at 00:34