Use custom @NonNull/@Nullable annotations


I’m trying to use my own @NonNull/@Nullable annotations, but rules like RSPEC-2637 are not triggered. It’s a bit confusing to me since the annotations I wrote look just like Spring’s NonNull and Nullable annotations, which work perfectly fine with SonarLint. Am I missing something? Any insight on how this works would be deeply appreciated!

To add a bit more context, I actually wanted to write my own package-level annotations for null-safety, just like Spring’s NonNullApi and NonNullFields annotations. But when that didn’t work, I tracked the issue to the point where I could not even use my own @NotNull annotation.

The main reasons for using custom annotations are:

  • Spring annotations work great, but if I’m not using Spring (when writing a library, for example), it won’t make sense to add the whole Spring dependency to be able to use a few annotations
  • I would like my package-level annotation to apply to all targets (not just fields, parameters, methods). Would that work with SonarLint?
  • Also, I wrote a small Gradle plugin to automatically add all the files to the project, and it lets you choose which annotation(s) to add to the packages. It would be great if SonarLint can check for any null-safety annotation that anyone would wish to use, as long as it has the javax.annotation.Nonnull annotation (or its implementation, maybe?).

If anyone would like to check the Gradle plugin, here’s the link: strict-null-check :slightly_smiling_face:

I really appreciate any help you can provide.