0

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?

comrade
  • 4,590
  • 5
  • 33
  • 48
Víctor Albertos
  • 8,093
  • 5
  • 43
  • 71

2 Answers2

1

You should use the @SupportedAnnotationTypes annotation on your processor, and not override the getSupportedAnnotationTypes() method, for example:

@SupportedAnnotationTypes({"com.test.Interceptor"})
public class AnnotationProcessor extends AbstractProcessor {
    ...

The Processor.getSupportedAnnotationTypes() method can construct its result from the value of this annotation, as done by AbstractProcessor.getSupportedAnnotationTypes().

Javadoc:

https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/SupportedAnnotationTypes.html

ck1
  • 5,243
  • 1
  • 21
  • 25
1

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 (using ProcessingEnvironment.getElements() to determine whether it has the meta-annotation of interest.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • Thanks for the reply, that's exactly what I'm doing now, regardless that the javadoc discourage using it. But I'm stuck trying to retrieve these annotations. Could you please take a look at this another question? http://stackoverflow.com/questions/38040104/how-to-access-an-annotation-inside-another-one-using-abstractprocessor-not-refl – Víctor Albertos Jun 26 '16 at 17:14