3

Given these types:

@Retention(RUNTIME)
@Target(ANNOTATION_TYPE)
public @interface Annotation1 {

}

@Retention(RUNTIME)
@Target(TYPE)
@Annotation1
public @interface Annotation2 {

}

@Annotation2
public class Mock {

}

I'm able to access the Annotation2 from Mock class using an AbstractProcessor, as follows:

Element element = //obtained from RoundEnvironment instance.
AnnotationMirror annotationMirror = element.getAnnotationMirrors().get(0);

But when I query for the annotations annotated in the pevious annotationMirror- which is a mirror of Annotation2, I got an empty list.

annotationMirror
    .getAnnotationType()
    .asElement()
    .getAnnotationMirrors();

I think somehow this question is related to this one.

Community
  • 1
  • 1
Víctor Albertos
  • 8,093
  • 5
  • 43
  • 71
  • I think this happens because you've applied `@Annotation2` to a class, which is not a valid target for it - you've specified that the annotation applies only to methods and parameters. – yole Jun 26 '16 at 15:49
  • Sorry, it was a typo. I've updated the question to fix it. – Víctor Albertos Jun 26 '16 at 15:56
  • Why are you trying to get annotations from the `annotationMirror`? What are you trying to accomplish? – ck1 Jun 26 '16 at 16:45
  • Because I need to create a new java class for every class processed and annotated with an annotation annotated with Annotation1. – Víctor Albertos Jun 26 '16 at 17:15

1 Answers1

2

The code posted in the question works fine.

annotationMirror
    .getAnnotationType()
    .asElement()
    .getAnnotationMirrors();

The problem was related with a missing import in the source code used for testing purpose.

Víctor Albertos
  • 8,093
  • 5
  • 43
  • 71