Generics - <A extends Annotation> getAnnotation

Hello. I'm using reflection to obtain some properties of entity classes in EJB 3.0.

There two methods that cougth my attention given the way in which the complirer treats them:

Class.<A extends Annotation> A getAnnotation(Class<A> annotationClass)

and

Method.<T extends Annotation> T getAnnotation(Class<T> annotationClass)

Looking at their signatures, I figure they should work in the same way, however, this is what actually happens:

EmbeddedId embId = method.getAnnotation(EmbeddedId.class);

-> no complaining here. works great.

Table tableAnnotation = (Table) entityClass.getAnnotation(Table.class);

->Type safety: The method getAnnotation(Class) belongs to the raw type Class. References to generic type Class<T> should be parameterized

Note that I has to use a cast in the Class' getAnnotation, or otherwise it would not compile.

Please explain me this behaviour and tell me how to get rid of that warning.

Thanks in advance,

Hugo Oliveira

hugom.oliveira.ext@siemens.com

[1107 byte] By [Hugo_Oliveiraa] at [2007-11-26 23:21:52]
# 1
What is the class behind entityClass? I'd assume it's a generic class being used in a raw fashion.
stefan.schulza at 2007-7-10 14:26:30 > top of Java-index,Core,Core APIs...
# 2
Hello.It's just a Serializable class annotated with @Table.Hugo Oliveirahugom.oliveira.ext@siemens.com
Hugo_Oliveiraa at 2007-7-10 14:26:30 > top of Java-index,Core,Core APIs...
# 3

Well, if you cannot show the class declaration, I cannot tell.

If it looks somewhat like one of these:

public class EntityClass<T> implements Serializable ...

public class EntityClass implements SomeGenericClass, Serializable ...

where either your instance is created not defining the generic type or SomeGenericClass (or any superclass) is parameterized, this could be a reason.

stefan.schulza at 2007-7-10 14:26:30 > top of Java-index,Core,Core APIs...
# 4
Class<?> entityClass = classMetadata.getMappedClass(EntityMode.POJO);Table table = entityClass.getAnnotation(Table.class);
R.Banfilla at 2007-7-10 14:26:30 > top of Java-index,Core,Core APIs...