简体   繁体   English

无法将类强制转换为ParameterizedType

[英]Class cannot be cast to ParameterizedType

public class FrontUtil<T> {

    public List<Map<String, String>> tableGen(List<Map<String, String>> theads, List<T> tbodys){


        List<Map<String, String>> frontList = null;

        for (T t : tbodys) {
            *Class<T> clazz =(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];*
            for (Map<String, String> title : theads) {
                try {
                    Field field = clazz.getDeclaredField(title.get("columnClass"));
                    PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                    Method getMethod = pd.getReadMethod();
                    String content = getMethod.invoke(t).toString();
                    Map<String, String> temp = new HashMap<String, String>(0);
                    temp.put(title.get("columnName"), content);
                    frontList.add(temp);
                }  catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        return frontList;
    }
}

the stack: 堆栈:

java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
    *****FrontUtil.tableGen(FrontUtil.java:19)
    *****action.MaterialFactoryAction.list(MaterialFactoryAction.java:78)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

I would like to get the Actual class of T though 我想获得T的实际班级

Class<T> clazz =(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

but it can't work . 但这没用。 Any suggestion would be appreciated,thanks. 任何建议,将不胜感激,谢谢。

The answer is: you can't. 答案是:您不能。 Because of type erasure that information no longer exists at run time. 由于类型删除 ,信息在运行时不再存在。

Pass in a Class argument when you create your FrontUtil instance. 创建FrontUtil实例时传递Class参数。

private Class<T> clazz;
public FrontUtil(Class<T> clazz) {
    this.clazz = clazz;
}

Then use that Class object wherever you need it. 然后在需要的地方使用该Class对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类不能转换为java.lang.reflect.ParameterizedType - Class cannot be cast to java.lang.reflect.ParameterizedType 嵌套类不能转换为java.lang.reflect.ParameterizedType - Nested class cannot be cast to java.lang.reflect.ParameterizedType 类强制转换异常泛型反射ParameterizedType - Class Cast Exception Generics Reflection ParameterizedType ClassCastException:java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType - ClassCastException:java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType java.lang.ClassCastException:无法将java.lang.Class强制转换为java.lang.reflect.ParameterizedType - java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType - java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType 设计模式:Lazy Singleton,Generics和Inheritance:java.lang.Class无法强制转换为java.lang.reflect.ParameterizedType - Design Pattern: Lazy Singleton, Generics and Inheritance: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType A 类不能转换为 A 类 - class A cannot be cast to class A 无法将申请转换为课程 - Cannot cast Application to Class 无法将类别转换为ParseObject - Class cannot be cast to ParseObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM