More flexible and configurable @Nullable/@NotNull annotations

For quite a while IntelliJ IDEA can detect probable NPE’s in your code. But you have to annotate it with JetBrains’ @Nullable/@NotNull. In order to leave your code free from JetBrains’ annotations you could use external annotations, but they were not so convenient to use as they were not visible in the code. With libraries and SDK code, however, there is no other way except using the external annotations.

So, to make things better, 10.5 IntelliJ IDEA will be able to recognize the most common annotations, such as javax @Nullable/@Nonnull, FindBugs @Nullable/@NonNull, as well as your own annotations with the same semantics.

You just need to configure which annotations are allowed in you code and which should be used by quick fixes to annotate the code.

Try it in IntelliJ IDEA 10.5 EAP and let us know what you think.

Comments below can no longer be edited.

11 Responses to More flexible and configurable @Nullable/@NotNull annotations

  1. Avatar

    Joe says:

    March 29, 2011

    Thanks a lot! This is a very welcomed and helpful enhancement!

  2. Avatar

    Stephen Friedrich says:

    March 29, 2011

    How does that work with the ant task com.intellij.ant.InstrumentIdeaExtensions?

  3. Avatar

    yole says:

    March 30, 2011

    Currently it doesn’t. The Ant task only recognizes the JetBrains annotations. We’re not sure if it would be a good idea to support other annotations as well.

  4. Avatar

    Fredrik says:

    March 30, 2011


  5. Avatar

    Ted M. Young says:

    April 13, 2011

    I like this feature, but the dialog for setting the default is confusing. It took me several tries of setting it to use the javax.annotation before I realized that I had to click on the little D/ (set as default) option. This is quite different from how defaults are set elsewhere in IDEA, so is very confusing.

  6. Avatar

    Carlos says:

    June 7, 2011

    When I annotate fields with javax @Nullable/@Nonnull and generate setters/getters for them (Ctrl+Enter on Mac OS X), IntelliJ IDEA 10.5 still uses JetBrains annotations on the generated accessors, even tought I configured javax.annotation.* in the Nullable/NotNull configuration. It’s a bug?

  7. Avatar

    Anna Kozlova says:

    June 14, 2011

    Carlos, I’ve just rechecked and it works as expected for me. Could you please check that you have configured default annotations:
    1. open File|Settings|Inspections
    2. choose constant conditions & exceptions inspection
    3. press configure annotations
    4. select javax.annotation.Nullable
    5. press set as default from toolbar
    I suspect that it is the same problem as Tim already mentioned above. We would try to rework the UI to make it more clear. Thank you

  8. Avatar

    Joakim Gottzén says:

    September 8, 2011

    Following the steps above results in an xml file with the annotation and not that the actual method parameter being annotated, is this the expected behaviour?

  9. Avatar

    Anna Kozlova says:

    September 8, 2011

    This feature was designed not to touch source code as it may be not possible for some reason (e.g. jdk). So, yes, it is its expected behaviour

  10. Avatar

    Rajiv says:

    June 11, 2012

    I just discovered this feature it looks awesome, I am just wondering why not JDK has this to include so that its available to all and by default. Though most programmer knows what causes NPE, Its best to avoid NPE and rely on tools rather than littering code with !=null checks every now and then.

  11. Avatar

    James says:

    December 6, 2013

    Can @Contract be added to an @Override method if the super-type does not have an @Contract annotation? If the super-type does have @Contract, how does this affect the @Override method? Thanks!

Discover more