Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access an annotation inside another one using AbstractProcessor -not reflection

Tags:

java

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.

like image 479
Víctor Albertos Avatar asked May 21 '26 04:05

Víctor Albertos


1 Answers

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.

like image 84
Víctor Albertos Avatar answered May 22 '26 18:05

Víctor Albertos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!