1

After detecting a NPE while testing, I finally wanted to take a look at @Nullable annotations.

After a small internet search there seems to be a @Nullable annotation for every IDE or environment:

I have seen that they seem all to follow JSR-305, which describes this annotation. But the JSR is dormant, see also: What is the status of JSR 305?.

So I have two questions:

  • Is there another general annotation which can be used?
  • If no, do the IDEs only recognize their own annotations.
    • E.g. would it be possible to use the Spring annotations in a non-Spring-project, and Eclipse and IntelliJ can handle the annotation correctly.
6
  • What about javax.annotation.Nullable ? Commented Nov 25, 2022 at 10:01
  • I did not know about that specific annotation and added it to the list. Unfortunately Eclipse and SonarLint ignore it. Commented Nov 25, 2022 at 10:17
  • 1
    Oh! But SolarLint works with javax.annotation.CheckForNull. As actually pointed out in S2259 and the Javadoc of javax.annotation.Nullable. But Eclipse still ignores it. @Stewart: Thank you for the hint Commented Nov 25, 2022 at 10:26
  • Eclipse can be set up to work with any Nullable annotation, check under Java / Compiler / Errors-Warnings / Null Analysis. Long-term, there's going to be jspecify.dev which is a joint project to finally come up with a single spec and a single set of annotations, and it is worked on by multiple projects, companies, IDEs. Not there yet, it's in a very early stage, so not prod-ready, but take a look in a year or two. Until then, javax.annotation.Nullable is where it's at unless you prefer a different one for the small semantic differences they have, Commented Nov 25, 2022 at 10:47
  • Oh, also, see stackoverflow.com/questions/4963300/…, it's a mess. IDEs can be set-up to use whichever annotation, though. Or, use a checker like NullAway. Commented Nov 25, 2022 at 10:48

1 Answer 1

0

As I happen to use SonarLint/SonarQube the following works independently of the IDE.

For a Maven project one can add the JSR-305 annotations provided by Findbugs.

<dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>jsr305</artifactId> <version>3.0.2</version> <scope>compile</scope> </dependency 

Then the needed methods can be annotated with @CheckForNull. According to the @Nullable-Javadoc static analysis tools should ignore the @Nullable annotation.

If the method return value is then used without a preceding null-check, SonarLint raises a Null pointers should not be dereferenced (java:S2259) issue.

Eclipse ignores this by default, but as Petr Janeček pointed out it can be configured to use the annotation. If javax.annotation.CheckForNull or javax.annotation.Nullable should be used is probably debatable.

Eclipse settings

I have not tested this for other IDEs.

Sign up to request clarification or add additional context in comments.

3 Comments

The answer does not need the ugly import of spring-core any more.
According to your screenshot you configured the wrong annotation: first has to be javax.annotation.Nullable. Please note, as far as I known, only the org.eclipse.jdt null annotations do not pollute the bytecode.
This is what I meant with "If javax.annotation.CheckForNull or javax.annotation.Nullable should be used is probably debatable." I think that javax.annotation.CheckForNull equals org.eclipse.jdt.annotation.Nullable according to their respective Javadoc. javax.annotation.Nullable is (according to it's Javadoc) only a hint for the developer. Till now I have not yet considered comparing the bytecode, as I added the annotations only in the compile-scope. But it is an interesting point.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.