Imagine I have two methods:
private <T> T testGenericsT(Class<T> clazz) {
Class[] classArray = {clazz};
return (T) newProxyInstance(
clazz.getClassLoader(),
classArray,
(proxy, method, args) -> null
);
}
private AccessibleStreamable testGenerics(Class<AccessibleStreamable> clazz) {
Class[] classArray = { clazz };
return (AccessibleStreamable) newProxyInstance(
clazz.getClassLoader(),
classArray,
(proxy, method, args) -> null
);
}
The upper one is not type safe, I get the warning unchecked cast java.lang.Object to T But the lower one is accepted, from my point of view I'm not sure where these differs.
Is there a way I can make the upper one typesafe?
What am I missing here, why is it perfectly fine to cast it to a AccessibleStreamable but not T?
I can even change my code like this
private AccessibleStreamable testGenerics(Class<AccessibleStreamable> clazz) {
Class[] classArray = { List.class };
return (AccessibleStreamable) newProxyInstance(
Map.class.getClassLoader(),
classArray,
(proxy, method, args) -> null
);
}
And it will still not say any warning. Of course I wouldn't expect it to give me a warning saying that I'm doing an unchecked cast to T. But I guess my main question is, is there a way I can make the upper one safe? And I don't mean by suppressing the warning.
Aucun commentaire:
Enregistrer un commentaire