本文介绍了ClassCastException在投射List< String>到Class<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,它有一个输入列表和每个输入值,我必须将其转换为所需的类型。 (实际上这个列表的参数值是some形式的,我应该将它转换为所需的类型,所需的类型是方法需要的类型,这是通过反射来调用api的)
$

$ p $ 类型[] argTypes = method.getGenericParameterTypes();
列表< Object> acutalValues = //方法输入的实际值列表。
//结果列表将包含铸造值。
列表< 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>失败..
//将实际值转换为所需的类型。必需的类型是requiredClassType
arguments.add(castToRequiredType(requiredClassTYpe,actualValues.get(i)));
}

异常消息如下所示:

  ClassCastException:无法将sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl(id = 77)转换为java.lang.Class 



现在,如果我将第一行更改为

 类型[] argTypes = method.getParameterTypes()

然后它传递任何想法,这是因为正如在 method.getGenericParameterTypes() JavaDoc中所述的 它:

因此,如果你的方法声明如下所示:

  public void myMethod(String string,List< String> list){...} 

argTypes [0] 将包含 Class (即 Class )的实例,但 argTypes [1] 将包含 java.lang.reflect.ParameterizedType 的实例。您可以使用 getActualTypeArguments()方法获取有关 ParameterizedType 的实际类型参数的信息。上例所描述的例子:

$ p $ Type [] listTypeArgs =((ParameterizedType)argTypes [1])。getActualTypeArguments()

// listTypeArgs.length == 1 as List< String>有一个类型参数
// listTypeArgs [0]现在包含类的实例< ;? extends String>


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?

解决方案

Because as stated in method.getGenericParameterTypes() JavaDoc it:

Thus if your method declaration looks like:

public void myMethod(String string, List<String> list) { ... }

argTypes[0] will contain instance of Class (i.e. Class<? extends String>), but argTypes[1] will contain instance of java.lang.reflect.ParameterizedType. You can get information about actual type arguments of ParameterizedType using getActualTypeArguments() method. Example for case I described above:

Type[] listTypeArgs = ((ParameterizedType) argTypes[1]).getActualTypeArguments()

// listTypeArgs.length == 1 as List<String> have one type argument
// listTypeArgs[0] now contains instance of Class<? extends String>

这篇关于ClassCastException在投射List&lt; String&gt;到Class&lt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 20:46