Auto-infer @Nullable/@NotNull Annotations
December 14th, 2010 by Anna KozlovaFor 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.
Tags: Code Analysis, ideaX

December 14th, 2010 at 1:34 pm
Looks like the second screenshot should include “@Nullable” for the bar() instead of @NotNull?
December 14th, 2010 at 7:02 pm
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.
December 15th, 2010 at 3:00 am
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…
December 15th, 2010 at 4:24 am
+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.
December 15th, 2010 at 2:44 pm
@KIR
nope, @Nullable would produce NPE (null.substring(0))
December 15th, 2010 at 3:38 pm
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.
December 16th, 2010 at 7:29 am
+1
Forcing users to depend on a jetbrains jar (and include it in their maven repository, etc) does not work in practice.
December 16th, 2010 at 9:09 am
Please, vote for http://youtrack.jetbrains.net/issue/IDEA-19564
Thank you
December 17th, 2010 at 11:27 pm
yes. Strongly agreed that Intellij should refrain adding such features that require proprietary library.
December 19th, 2010 at 10:06 am
@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.
January 5th, 2011 at 6:23 pm
+1 to allowing support for the javax.annotation.Nullable version.
March 25th, 2011 at 2:42 pm
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.
June 14th, 2011 at 4:28 am
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.
October 23rd, 2012 at 10:54 am
Could you please patch code and throw IllegalArgument / AssertionErrors for javax.validation.constraints.NotNull as it done for jetbrains NotNull?