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.

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

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

  1. Joe says:

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

  2. Stephen Friedrich says:

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

  3. yole says:

    Stephen,
    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. Fredrik says:

    Great!

  5. Ted M. Young says:

    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. Carlos says:

    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. Anna Kozlova says:

    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. Joakim Gottzén says:

    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. Anna Kozlova says:

    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. Rajiv says:

    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. James says:

    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!

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>