I have a repeatable annotation with a Class
type attribute like this one.
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.PARAMETER)
public @interface MyAnnotation {
Class<?> value();
}
I'm trying to get the class name from it using AnnotationMirror
s since I can't grab the class directly without getting a MirroredTypeException
. This is the same as this question except that this annotation is repeatable, meaning it's wrapped in another annotation like this one.
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.PARAMETER)
public @interface MyAnnotationWrapper {
MyAnnotation[] value();
}
I can only figure out how to get the mirror for the wrapper annotation and not the repeatable one. I was able to accomplish what I needed using the hacky workaround in the linked question in combination with invoking the annotation method via reflection and grabbing the cause from the exception I got. It looked something like this (I don't have the code in front of me right now).
try {
annotation.getClass().getMethod("value").invoke(annotation)
} catch (<Several exception types> e) {
className = ((MirroredTypeException) e.getCause()).getTypeMirror().toString()
}
I want to know if there's a correct way to get the class name in this case.