Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make a Java Annotation which "extends" @SuppressWarnings?

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?

like image 370
Armand Avatar asked Dec 30 '11 13:12

Armand


People also ask

Can you extend a Java annotation?

In Java SE 6, annotations cannot subclass one another, and an annotation is not allowed to extend/implement any interfaces.

What is SuppressWarnings annotation in Java?

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.

What is the use of @SuppressWarnings in Java?

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.

What is SuppressWarnings serial?

The line @SuppressWarnings(\"serial\") tells Java not to remind you that you've omitted something called a serialVersionUID field.


1 Answers

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.

like image 70
asm0dey Avatar answered Oct 05 '22 04:10

asm0dey