Today i was writing some heavy reflection-using code, and i came across this behavior i can't explain: why does
Type[] types = ((ParameterizedType)m.getGenericReturnType()).getActualTypeArguments();
Class[] c = (Class[])types;
Throw a ClassCastException, when iterating over that same array and casting every single element, i.e.
for(Type t : types) {
Class c = (Class)t;
}
succeeds?
I mean, if the casting of a single element to another class is possible, why isn't the casting between arrays of the same types possible as well?
There probably is a reason, but i can't seem to find one...
You can't cast a Type[] to a Class[] because a Type[] is not a Class[]. It may sound trivial, but that's all there is to it.
If you could cast a Type[] to a Class[], what would you expect to happen when one of its elements was a Type that isn't a Class (a ParameterizedType for example)?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With