考慮下面的代碼:爲什麼Eclipse會抱怨不安全的類型轉換?
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是彰顯投(Class<T>)
以下消息:
Type safety: Unchecked cast from Class<capture#2-of ? extends SomeObject> to Class<T>
這是爲什麼,考慮到T
應該保證從SomeObject
延長在方法簽名?什麼情況下可能存在這將是不安全的?