Auto-infer @Nullable/@NotNull Annotations

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.

This entry was posted in New Features and tagged , . Bookmark the permalink.

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

  1. KIR says:

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

  2. Jon says:

    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:

    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:

    +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:

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

  6. 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:

    +1

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

  8. John says:

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

  9. Dirk says:

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

    http://youtrack.jetbrains.net/issue/IDEABKL-4959
    http://youtrack.jetbrains.net/issue/IDEA-10924

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

  10. ggascoigne says:

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

  11. Jeff Plaisance says:

    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.

  12. Vaclav says:

    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.

  13. LNK says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>