I am working on an annotation processor. This code compiles:
package sand;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;
@SupportedAnnotationTypes("sand.Foo")
public class FooProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
return false; // TODO
}
}
However, I am displeased by the string constant "sand.Foo" (not too much in this case, but more in general for the future).
If Foo
is renamed or moved to another package, this code will still compile, but it won't work.
I would like to do something like:
@SupportedAnnotationTypes(Foo.class)
That way, if Foo's name changes, the compilation will fail and someone will have to correct the file.
But this does not work because a Class
is not a String
. So I tried:
@SupportedAnnotationTypes(Foo.class.getName())
But the compiler does not consider this a constant expression, which is required in this context, so that won't work either.
Is there any way to coerce a class literal into its name at compile time?
Instead of using the annotation, your processor can implement
getSupportedAnnotationTypes()
to provide supported annotation type names at runtime:In case you'd like to keep using (non-standard) annotations for this, you could create your own annotation that takes a compile time type as argument, like @k_g suggested. @SupportedAnnotationTypes isn't really anything special, it is only used automatically when you are extending
AbstractProcessor
anyway. Take a look at the source code ofAbstractProcessor.getSupportedAnnotationTypes()
.The signature of your custom annotation should use
Class<?>[]
instead ofString[]
:Override
getSupportedAnnotationTypes
and look up your custom annotation in the same way asAbstractProcessor
. For example like this:You can define your own.
and then use
@SupportedAnnotationTypes_Class(supported = sand.Foo.class)
to use it.