I have a method which has a list of inputs and each input value I have to cast it to required type. (Actually this list has values of parameters in "some" form which I am supposed to converted into required type, the required type is the type which method requires, this is for api invocation through reflection)
I have written code like this:
Type[] argTypes = method.getGenericParameterTypes();
List<Object> acutalValues = // List of actual values from method input.
// Resultant list of arguments which will holds casted values.
List<Object> arguments = new ArrayList<Object>(argTypes.length);
for (int i = 0; i < argTypes.length; i++) {
Class<?> requiredClassTYpe = (Class<?>) argTypes[i]; // argTypes[1] =java.util.List<java.lang.String> fails..
// cast the actual value to the required type. Required type is requiredClassType
arguments.add(castToRequiredType(requiredClassTYpe, actualValues.get(i)));
}
The exception message is like this :
ClassCastException: Cannot cast sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl (id=77) to java.lang.Class
Now if I change the first line to
Type[] argTypes = method.getParameterTypes()
Then it passes any idea why it might be happening like this?