I know that it's not possible to extends Java annotations.
I've created an annotation for a private field which means that the field is likely to appear unused in the class in which it is declared. For this reason, I'm getting a lot of "unused field" warnings on my annotated fields.
Is there any way to give my annotation the behaviour of @SuppressWarnings("unused")
so I don't have to doubly-annotate every field which has @MyAnnotation
?
In Java SE 6, annotations cannot subclass one another, and an annotation is not allowed to extend/implement any interfaces.
Annotation Type SuppressWarnings Note that the set of warnings suppressed in a given element is a superset of the warnings suppressed in all containing elements. For example, if you annotate a class to suppress one warning and annotate a method to suppress another, both warnings will be suppressed in the method.
Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code. 1. @SuppressWarnings("unchecked") public class Calculator { } - Here, it will ignore all unchecked warnings coming from that class.
The line @SuppressWarnings(\"serial\") tells Java not to remind you that you've omitted something called a serialVersionUID field.
The quick answer is "no". Java compiler doesn't know anything about your annotation, so it won't process it the way you want.
But the honest answer is "yes". In this article you can find detailed description of how to write compiler plugin. Chances are you can write plugin, which, in case of finding your annotation, will handle it and won't pass field to unused checker.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With