Given this annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Interceptor {
Class<? extends Behaviour> value();
}
The users of my library can extend its API creating custom annotations annotated with @Interceptor
, as follows:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Interceptor(BypassInterceptor.class)
public @interface Bypass {
}
AbstractProcessor provides a method called getSupportedAnnotationTypes which returns the names of the annotation types supported by the processor. But if I specify the name of @Interceptor
, as follows:
@Override public Set<String> getSupportedAnnotationTypes() {
Set<String> annotations = new LinkedHashSet();
annotations.add(Interceptor.class.getCanonicalName());
return annotations;
}
The processor#process method will not be notified when a class is annotated with @Bypass
annotation.
So, when using an AbstractProcessor
, how to claim for annotations which target is another annotation?
You should use the
@SupportedAnnotationTypes
annotation on your processor, and not override thegetSupportedAnnotationTypes()
method, for example:Javadoc:
https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/SupportedAnnotationTypes.html
If your annotation processor is scanning for all annotations that are meta-annotated with your annotation, you'll need to specify
"*"
for your supported annotation types, and then inspect each annotation's declaration (usingProcessingEnvironment.getElements()
to determine whether it has the meta-annotation of interest.