Can I make a Java annotation that extends @SuppressWarnings?

I know that it is not possible to extend Java annotations .

I created an annotation for a private field, which means that the field will most likely not be used in the class in which it is declared. For this reason, I receive many warnings about an unused field in my annotated fields.

Is there a way to give my annotation the behavior of @SuppressWarnings("unused") , so I don’t need to double the annotation of each field that has @MyAnnotation ?

+8
java annotations suppress-warnings
source share
1 answer

The quick answer is no. The Java compiler does not know anything about your annotation, so it will not process it the way you want.


But the honest answer is yes. In this article you can find a detailed description of how to write a compiler plugin. Most likely, you can write a plugin that, if your annotation is detected, will process it and will not pass the field to an unused check.

0
source share

All Articles