lundi 26 janvier 2015

Why does Eclipse complain about unsafe type casting?

Consider the following code:



public static <T extends SomeObject> List<T> getByClassName(String className) throws GetRecordsException {
try {
Class<T> clazz = (Class<T>) Class.forName(className).asSubclass(SomeObject.class);
return getByClassName(clazz);
} catch (ClassNotFoundException e) {
throw new GetRecordsException("Failed to get class forName: " + className, e);
}
}


Eclipse is highlighting the cast (Class<T>) with the following message:


Type safety: Unchecked cast from Class<capture#2-of ? extends SomeObject> to Class<T>


Why is this, considering that T is supposed to be guaranteed to extend from SomeObject in the method signature? What scenario might exist where this would be unsafe?






Aucun commentaire:

Enregistrer un commentaire