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?