30888

AbstractProcessor, how to claim for annotations which target is another annotation?

Question:

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 { }

<a href="https://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/AbstractProcessor.html" rel="nofollow">AbstractProcessor</a> provides a method called <a href="https://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/AbstractProcessor.html#getSupportedSourceVersion()" rel="nofollow">getSupportedAnnotationTypes</a> 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 <a href="https://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/AbstractProcessor.html#process(java.util.Set,%20javax.annotation.processing.RoundEnvironment)" rel="nofollow">processor#process</a> 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?

Answer1:

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.

Answer2:

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 { ... <blockquote>

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

</blockquote>

Javadoc:

<a href="https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/SupportedAnnotationTypes.html" rel="nofollow">https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/SupportedAnnotationTypes.html</a>

Recommend

  • Spring @ExceptionHandler does not return content unless body is empty
  • open source, multi-platform, browser-based screen capture extension? [closed]
  • R: merging copies of the same variable
  • com.jcraft.jsch.JSchException: Algorithm negotiation fail
  • 'SOCK_RAW' option in 'socket' system call
  • How to access the element at variable index of a tuple?
  • How to select the last one test without NA in r
  • Replace Inf in R data.table / Show number of Inf in colums
  • Group by and select min date with data.table
  • Concise R data.table syntax for modal value (most frequent) by group
  • Exclude resource paths from Jersey application.wadl?
  • plotting spatial points over a raster layer in r
  • iphone - Hide the annotation view in map view
  • PHP OOXML Libraries? [closed]
  • How can I make a right-click behave as a left-click for the purpose of selecting or focusing an obje
  • What's a fast (non-loop) way to apply a dict to a ndarray (meaning use elements as keys and rep
  • What does a hyphen at end of a term mean
  • 3.0.0.M1: SSL - Invalid keystore format
  • pandas computation in each group
  • How to estimate the Kalman Filter with 'KFAS' R package, with an AR(1) transition equation
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Plotting line graph with factors in R
  • Implicit joins and Where in Doctrine - how?
  • Control modification in presentation layer
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Fetching methods from BroadcastReceiver to update UI
  • Bug in WPF DataGrid
  • Redux, normalised entities and lodash merge
  • Hazelcast - OperationTimeoutException
  • RestKit - RKRequestDelegate does not exist
  • Revoking OAuth Access Token Results in 404 Not Found
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • how does django model after text[] in postgresql [duplicate]
  • How do I configure my settings file to work with unit tests?
  • Java static initializers and reflection
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • unknown Exception android
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app