I’m using Java 1.6.0_25.
I have an annotation defined:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Resource {
String value();
}
And later when I use getAnnotation:
Resource resource = (Resource)cls.getAnnotation(Resource.class);
the compiler and IDE agree that I must cast the result, but getAnnotation is declared in the Java 1.5 documentation as:
public <A extends Annotation> A getAnnotation(Class<A> annotationClass);
Since Resource.class is of type Class, it seems to me that this means that cls.getAnnotation(Resource.class) should return type Resource, and I should need to cast.
All examples I’ve found using getAnnotation don’t have a cast, so I must be doing something wrong.
What is the type of
cls? Is it rawClassor is itClass<Something>?If it’s the raw type
Class, then the cast is necessary. If you make it at leastClass<?>instead ofClass, you won’t need the cast anymore.