Auto-infer @Nullable/@NotNull Annotations

Anna Kozlova

For a long time already IntelliJ IDEA can detect probable NPE’s in your code. You need to annotate your methods/fields/variables with @Nullable/@NotNull annotations and IntelliJ IDEA will highlight possible contract violations. Good. But the annotations only begin to work when there is enough of them in the code. You do need to annotate your code to get the benefits.

IntelliJ IDEA 10 can do it for you.

Just run Analyze | Infer Nullity… and choose a scope where you want annotations to be inferred. IntelliJ IDEA uses many rules to analyze nullity and make a decision. Here are just 2 examples:

1) The IDE detects parameters that are used without checking for null:

and presumes that they are @NotNull:

2) Variables that are checked for null are assumed as @Nullable, etc.

As a result you can benefit from static code analysis without paying an entrance fee.

Comments below can no longer be edited.

14 Responses to Auto-infer @Nullable/@NotNull Annotations

  1. KIR says:

    December 14, 2010

    Looks like the second screenshot should include “@Nullable” for the bar() instead of @NotNull?

  2. Jon says:

    December 14, 2010

    This is a great feature!

    Is there any chance you could support the equivalent annotations defined in JSR-305 (in the javax.annotations namespace), rather than requiring the annotations in the org.jetbrains.annotations namespace?

    This would allow better integration with third-party tools like FindBugs, and I think organisations are happier to have ‘import javax.annotations’ in their code than ‘import org.jetbrains.annotations’, especially for dev-teams which use a mixture of IDEs.

  3. Gilles says:

    December 15, 2010

    I do agree with Jon. It really is a brillaient feature, but being on an eclipse project, i won’t import a vendor-specific annotation all over the place even though i really lové intellij…

  4. Wim Deblauwe says:

    December 15, 2010

    +1 for this. We are not using this IDEA feature because of the jetbrains imports. If the javax.annotations would be supported, we would use it.

  5. m.milicevic says:

    December 15, 2010

    nope, @Nullable would produce NPE (null.substring(0))

  6. Kohsuke Kawaguchi says:

    December 15, 2010

    Another +1. It should be very easy for IDEA to support javax.annotations instead of JetBrains specific annotations.

    I understand that JSR-305 is not final and all, but this is just a compile time dependency, and a possible impact from future changes in those annotations is pretty small.

  7. Taras Tielkes says:

    December 16, 2010


    Forcing users to depend on a jetbrains jar (and include it in their maven repository, etc) does not work in practice.

  8. Anna Kozlova says:

    December 16, 2010

    Please, vote for 🙂 Thank you

  9. John says:

    December 17, 2010

    yes. Strongly agreed that Intellij should refrain adding such features that require proprietary library.

  10. Dirk says:

    December 19, 2010

    @Anna: please take into account that there are several similar issues when you evaluate the popularity of this feature:

    Just counting votes on IDEA-19564 might not be accurate.

  11. ggascoigne says:

    January 5, 2011

    +1 to allowing support for the javax.annotation.Nullable version.

  12. Jeff Plaisance says:

    March 25, 2011

    Is there a way to use class level defaults for this? I’d like for everything to be @NotNull by default unless specified to be @Nullable.

  13. Vaclav says:

    June 14, 2011

    Would it be possible to enable to infer nullity for libraires used in a project? Would help a lot when using some libraries with sources.

  14. LNK says:

    October 23, 2012

    Could you please patch code and throw IllegalArgument / AssertionErrors for javax.validation.constraints.NotNull as it done for jetbrains NotNull?


Subscribe for updates