这是个是工具类:
public class ConverUtils<T,E> { public T converObject(E e){ ObjectMapper objectMapper=new ObjectMapper(); ParameterizedType param = (ParameterizedType)this.getClass().getGenericSuperclass(); Class type = (Class)param.getActualTypeArguments()[0]; System.out.println(type.getSimpleName()); return T; } }
这个是测试用例:
public class ConverUtilTest{ public static void main(String[] args) { ConverUtils<User,OtherUser> conver=new ConverUtils<User,OtherUser>(); OtherUser other=new OtherUser(); User user = conver.converObject(OtherUser); } }
以下是报错信息:
Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType at com.lvmama.web.ConverUtils.converObject(ConverUtils.java:4) at com.lvmama.web.ConverTest.main(ConverUtilTest.java:5)
时间: 2024-10-23 21:27:59