How to use the @CheckForNull annotation with SonarLint

Consider following simple example:

.findAllStudents().stream().filter(e -> e.getAdress().equals(this.adress))

In order to prevent a possible NPE when the methodcall getAdress() would return NULL, I would like to use the at_CheckForNull annotation (I cannot use the at-icon, since otherwise my post would be detected to name another user which I am not allowed to do since I am a fairly new member of this community) :

@CheckForNull
public Adress getAdress() {
    return adress;
}

Unfortunately, my program does not recognize this common annotation. What do I need to import or what do I need to do, so that this annotation can be used by SonarLint as intended?
I have imported the ‘import javax.annotation.*’ regarding the dependency ‘jsr305’

Also I always have been under the impression that SonarLint would provide me regarding the S2259 rule ‘NullPointers should not be dereferenced’ an according hint/warning whenever this is possible and not only when this is the case.

Example:

public void foo(Adress adress) {
   adress = null;
   adress.getStreet();
}

The above snippet would provide to me the wished for warning.
But let’s consider this adjusted snippet:

public void foo() {
  Adress adress =  student.getAdress();
  String street = adress.getName();
}

This would not provide any warning, although hypothetically the argument student might be null.

Best regards
Abegail

Hi Abegail,

Just FYI, you can use backticks around inline code so that it’s not interpreted as markup. E.G.: @CheckForNull.

And you seem to have overlooked the topic template. Can you provide these details, please?

  • Operating system:
  • SonarLint plugin version:
  • Programming language you’re coding in:
  • Is connected mode used:
    • Connected to SonarCloud or SonarQube (and which version):

 
Thx,
Ann

Hi!

Thanks for your input. Btw., in the meanwhile my issue is solved, and the hints do work.
Next time (when I would create another thread), I will provide initially the requested details.

Best regards
Abegail

1 Like

Hi!

I reopen the topic, but first some required information:

  • I am using Windows 10
  • Latest SonarLInt Plugin version i.e., 9.3
  • I am using Java as programming language
  • Not connected to SonarCloud or SonarQube yet, but irrelevant to my issue.

As said, the CheckForNull-annotation works now and SonarLint provides a null-referencing-warning.
I am using this annotation with the following dependency:

<dependency>
	<groupId>com.github.spotbugs</groupId>
	<artifactId>spotbugs-annotations</artifactId>
	<version>4.8.3</version>
</dependency>

Unfortunately, it came to my attention that the CheckForNull-annotation is deprecated. I am wondering what to use as best practice instead to produce the same result and from which source?

Best regards
Abegail

Hi Abegail

I assume that you’re using the javax.annotation package?

Due to the moving from Oracle to Eclipse Foundation, the javax.* packages are being renamed to jakarta.*. The former javax.annotation is now jakarta.annotation, that’s why the classes in the package with the old name are deprecated.

So you can just rename it in your imports and add the jakarta annotations package to your dependencies. Note, however, that you might run into conflicts in case your are using third party libraries that have not made the transition yet.

Best,
Marco

Hi Marco!

Thanks a lot for the hint!
For the annotation in question, I was using the following package:

du.umd.cs.findbugs.annotations.CheckForNull;

However, I will try it with the jakarta.annotation-package.

But I wonder how does one realize that an annotation is deprecated? It is not shown in the souce code by crossing out the deprecated item as it is usually the case when stuff like methods are deprecated.

Best wishes
Abegail