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.
gillesB
  • 1,061
  • 1
  • 14
  • 30
  • What about `javax.annotation.Nullable` ? – Stewart Nov 25 '22 at 10:01
  • I did not know about that specific annotation and added it to the list. Unfortunately Eclipse and SonarLint ignore it. – gillesB Nov 25 '22 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 – gillesB Nov 25 '22 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 https://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, – Petr Janeček Nov 25 '22 at 10:47
  • Oh, also, see https://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use, it's a mess. IDEs can be set-up to use whichever annotation, though. Or, use a checker like NullAway. – Petr Janeček Nov 25 '22 at 10:48
  • @PetrJaneček Thank you: I updated my answer accordingly. – gillesB Nov 25 '22 at 11:12

1 Answers1

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.

gillesB
  • 1,061
  • 1
  • 14
  • 30
  • The answer does not need the ugly import of `spring-core` any more. – gillesB Nov 25 '22 at 10:38
  • 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. – howlger Nov 25 '22 at 12:59
  • 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. – gillesB Nov 25 '22 at 13:34