In my code, I may have annotation defined on methods or fields so what I’m doing is checking both the methods and fields in the class and I store all annotations in a separate list.
Then later I have a method called getAnnotation.
Annotation getAnnotation(Class annotationClass) {
for (Annotation annotation : annotations) {
if (annotation.getClass().equals(annotationClass)) {
return annotation;
}
}
return null;
}
And I call it like this:
Annotation annotation = getAnnotation(MyAnnotation.class);
The problem is that the getAnnotation method does not match up the class names. When I debug I see that the annotations are showing as proxy objects. How can I find the specific annotation that I want in this case?
TIA
I define the map like this:
Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>(4);
I populated the Map like this:
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
annotations.put(annotation.getClass(), annotation);
}
Yay! I figured it out. The Annotation object has an “annotationType()” method to return the real class name.
So the code for anyone interested would look like this:
Map, Annotation> annotations = new HashMap, Annotation>(4);
Then the class matching works fine.