My code runs in Java 5 and 6, but when I upgraded to Java 7 I get a "java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.GenericArrayType".
For the following code:
public class A<T> {
public Vector<Integer[]> arr;
}
System.out.println(((ParameterizedType)A.class.getField("arr").getGenericType()).getActualTypeArguments()[0]);
System.out.println(((ParameterizedType) A.class.getField("arr").getGenericType()).getActualTypeArguments()[0].getClass());
Java6 prints:
java.lang.Integer[]
class sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl
Java7 prints:
class [Ljava.lang.Integer;
class java.lang.Class
This different behavior is breaking my code which relies on generics. Can someone please help me understand why this happens and how to get GenericArrayType from Vector<Integer[]> arr
?
The documentation for GenericArrayType
states it represents an array type whose component type is either a parameterized type or a type variable.
In the given example the array type is Integer[]
. The component type, Integer
, is neither a parameterized type nor a type variable. So your array type really should never have been represented by a GenericArrayType
.
So apparently JRE6 behavior was buggy and it got fixed. Sorry. :(
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