# Use custom @NonNull/@Nullable annotations

**URL:** <https://community.sonarsource.com/t/use-custom-nonnull-nullable-annotations/38800>\
**Category:** Report False-positive / False-negative...\
**Tags:** sonarqube-ide, java\
**Created:** [February 17, 2021, 6:00am UTC](https://community.sonarsource.com/t/use-custom-nonnull-nullable-annotations/38800 "2021-02-17T06:00:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoseLion](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/joselion/32/11013_2.png) [@JoseLion](https://community.sonarsource.com/u/JoseLion)\
**Post date:** [February 17, 2021, 6:00am UTC](https://community.sonarsource.com/t/use-custom-nonnull-nullable-annotations/38800/1 "2021-02-17T06:00:19Z")

</div>

Hi,

I’m trying to use my own @NonNull/@Nullable annotations, but rules like [RSPEC-2637](https://rules.sonarsource.com/java/RSPEC-2637) are not triggered. It’s a bit confusing to me since the annotations I wrote look just like Spring’s [NonNull](https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/lang/NonNull.java) and [Nullable](https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/lang/Nullable.java) 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](https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/lang/NonNullApi.java) and [NonNullFields](https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/lang/NonNullFields.java) 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 `package-info.java` 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](https://github.com/JoseLion/strict-null-check) 🙂

I really appreciate any help you can provide.

Cheers!

---

<div class="post-metadata">

**Author:** ![Quentin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/quentin/32/74716_2.png) [@Quentin](https://community.sonarsource.com/u/Quentin)\
**Post date:** [March 11, 2021, 11:01am UTC](https://community.sonarsource.com/t/use-custom-nonnull-nullable-annotations/38800/3 "2021-03-11T11:01:16Z")

</div>

Hello @JoseLion

From [this more recent post](https://community.sonarsource.com/t/eclipses-nonnullbydefault-annotation-should-also-check-fields/39288), I can see that you now understand way better how we deal with Nullable annotations.

Did you manage to find answers to your questions in between?

---

<div class="post-metadata">

**Author:** ![JoseLion](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/joselion/32/11013_2.png) [@JoseLion](https://community.sonarsource.com/u/JoseLion)\
**Post date:** [April 4, 2021, 11:45pm UTC](https://community.sonarsource.com/t/use-custom-nonnull-nullable-annotations/38800/5 "2021-04-04T23:45:38Z")

</div>

Hi @Quentin!

Sorry for the late response, and thanks a lot for your answer (here and the mentioned post 🙂)

Yep, now I have a better idea of how it works, and I think I found the answers for most of my questions too, thanks a lot!

Just one last question, I noticed that when the annotations are checked, there’s access to the annotation metadata, right? Wouldn’t this allow to actually check the annotation implementation and metadata instead of just checking for specific annotations burn into the code?

I understand this would need a big refactor, and I might be missing something too 😅 but I’d like to know if it’s possible and/or why this approach was not used (or if there are any plans to going this way). IMO, I think it would be great a great feature to be able to use custom nullability annotations 🙂

Thanks again for the support!

Cheers!!

---

<div class="post-metadata">

**Author:** ![Quentin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/quentin/32/74716_2.png) [@Quentin](https://community.sonarsource.com/u/Quentin)\
**Post date:** [April 8, 2021, 3:48pm UTC](https://community.sonarsource.com/t/use-custom-nonnull-nullable-annotations/38800/7 "2021-04-08T15:48:38Z")

</div>

If I understand correctly, your question is about supporting “meta-annotations” themselves annotated with a known Nullable annotation?

In fact, we are already supporting this… inconsistently, only in a few rules. ☹  
When investigating this question, I realized that our helper class about nullable annotations became quite messy with time. I created a ticket ([SONARJAVA-3795](https://jira.sonarsource.com/browse/SONARJAVA-3795)) to eventually clean everything and consistently support meta-annotations.

Thank you for raising this interesting point.  
Best,  
Quentin

---

<div class="post-metadata">

**Author:** ![JoseLion](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/joselion/32/11013_2.png) [@JoseLion](https://community.sonarsource.com/u/JoseLion)\
**Post date:** [April 9, 2021, 5:53am UTC](https://community.sonarsource.com/t/use-custom-nonnull-nullable-annotations/38800/9 "2021-04-09T05:53:05Z")

</div>

Yep, that’s what I meant. Looking at the ticket you created makes it even clearer. 🙂  
So maybe supporting “meta-annotations” for nullness rules won’t be as much work/refactor as I initially imagined. That’s great news!

I’m glad I could help around this, and I’ll be looking forward to this awesome improvement.

Thanks again for the great support.  
Cheers,  
Jose

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [April 16, 2021, 5:53am UTC](https://community.sonarsource.com/t/use-custom-nonnull-nullable-annotations/38800/10 "2021-04-16T05:53:52Z")

</div>

Closed on April 16, 2021, 5:53am UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
