Hi, and thanks for all of your help in advance.
I'm new to Generics, and I'm working with a piece of code that I did not write. The following method uses Generics in a way I'm not sure I understand, and there is a warning that I'm not sure how to fix:
public <A extends Annotation> A getAnnotation(Class targetClass, Class<A> annotationClass) {
A annotation = (A) targetClass.getAnnotation(annotationClass);
if (annotation == null) {
Class[] interfaces = targetClass.getInterfaces();
for (Class intf : interfaces) {
annotation = getAnnotation(intf, annotationClass);
if (annotation != null) {
break;
}
}
}
return annotation;
}
Here is my current understanding:
1. The declaration specifies that the method getAnnotation takes a Class (targetClass) that can represent any type of object, and a Class (annotationClass) that must represent some class that implements the interface Annotation (henceforth referred to as A). It also returns an object of the type A.
2. The first line of the method gets the annotations of type A if it is present in the class targetClass. The warning is also being generated here. It reads: "Type Safety: The method getAnnotation(class) belongs to the raw type Class. References to generic type Class<T> should be parameterized." Any clue on how to solve this?
3. This utility method seems to take a class, and find an annotation if it exists for this class. If it doesn't exist for this class, it will run through the class's interfaces as well to find the annotation.
Again, thanks for all your help on this issue in solving why this warning is occurring.
Regards,
Anthony Frasso