尝试获取泛型类型参数时出现 ClassCastException
ClassCastException while trying to get the generic type parameter
我想使用反射获取泛型实例:
this.wrapperInstance = ((Class<WRAPPER>) ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments()[1]).newInstance();
但我遇到异常:
java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
不知道问题出在哪里,也许有人可以帮助我?有完整的 class 代码:
public class XMLUtils<MODEL extends AbstractModel, WRAPPER extends WraperInterface<MODEL>> {
private WRAPPER wrapperInstance;
@SuppressWarnings("unchecked")
public XMLUtils() throws InstantiationException, IllegalAccessException {
this.wrapperInstance = ((Class<WRAPPER>) ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments()[1]).newInstance();
}
@SuppressWarnings("unchecked")
public List<MODEL> loadDataFromXML(String fileName) {
try {
File pajamuFile = new File(
UserDataLoader.INSTANCE.getCurrentUserFolder() + fileName);
JAXBContext context = JAXBContext.newInstance(wrapperInstance.getClass());
Unmarshaller um = context.createUnmarshaller();
WRAPPER wrapper = (WRAPPER) um.unmarshal(pajamuFile);
return wrapper.getDataList();
} catch (JAXBException e) {
e.printStackTrace();
return new ArrayList<MODEL>();
}
}
public void saveDataToXML(List<MODEL> dataList, String fileName) {
try {
File pajamuFile = new File(
UserDataLoader.INSTANCE.getCurrentUserFolder() + fileName);
JAXBContext context = JAXBContext.newInstance(wrapperInstance.getClass());
Marshaller m = context.createMarshaller();
WRAPPER wrapper = wrapperInstance;
wrapper.setDataList(dataList);
m.marshal(wrapper, pajamuFile);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
在 google 中,我发现大多数情况都是这样,但是在 spring 中,我在这里没有使用 spring,所以也许有任何明确的解决方案可以解决我想做的事情.
Do not know where is problem, maybe someaone can help me
错误消息是不言自明的。下面的语句 returns a Class
:
(getClass().getGenericSuperclass())
而您正试图将其转换为 ParameterizedType
((ParameterizedType) (getClass().getGenericSuperclass()))
Class
和 ParameterizedType
是兄妹。兄弟不是姐妹,所以试图把他们放在一起是残忍的。
也就是说,一个快速解决您的问题的方法是让客户端代码将 Class
类型传递给您的 XmlUtils
class。
public class XMLUtils<MODEL extends AbstractModel, WRAPPER extends WraperInterface<MODEL>> {
private WRAPPER wrapperInstance;
public XMLUtils(Class<WRAPPER> wrapperInstance) throws InstantiationException, IllegalAccessException {
this.wrapperInstance = wrapperInstance
}
//more code follows
}
你期望结果如何?
为了简化您的问题,可以说您有以下 class:
class XMLUtils extends java.lang.Object {
public XMLUtils() {
getClass().getGenericSuperclass()// == java.lang.Object
}
}
现在您正在尝试将 java.lang.Object
转换为 ParameterizedType
,但这仅在 class 是通用 class 的情况下才有效。因此它失败了...
您可能需要使用 TypeVariable
而不是 ParameterizedType
。
你检查过从getClass().getGenericSuperclass()
返回的对象的类型了吗?即
Class<?> c = getClass().getGenericSuperclass();
if (c instanceof ParameterizedType) {
System.out.println("It is an instanceof ParameterizedType");
}
else if (c instanceof TypeVariable) {
System.out.println("It is an instanceof TypeVariable");
}
else {
System.out.println("It is something else");
}
我想使用反射获取泛型实例:
this.wrapperInstance = ((Class<WRAPPER>) ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments()[1]).newInstance();
但我遇到异常:
java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
不知道问题出在哪里,也许有人可以帮助我?有完整的 class 代码:
public class XMLUtils<MODEL extends AbstractModel, WRAPPER extends WraperInterface<MODEL>> {
private WRAPPER wrapperInstance;
@SuppressWarnings("unchecked")
public XMLUtils() throws InstantiationException, IllegalAccessException {
this.wrapperInstance = ((Class<WRAPPER>) ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments()[1]).newInstance();
}
@SuppressWarnings("unchecked")
public List<MODEL> loadDataFromXML(String fileName) {
try {
File pajamuFile = new File(
UserDataLoader.INSTANCE.getCurrentUserFolder() + fileName);
JAXBContext context = JAXBContext.newInstance(wrapperInstance.getClass());
Unmarshaller um = context.createUnmarshaller();
WRAPPER wrapper = (WRAPPER) um.unmarshal(pajamuFile);
return wrapper.getDataList();
} catch (JAXBException e) {
e.printStackTrace();
return new ArrayList<MODEL>();
}
}
public void saveDataToXML(List<MODEL> dataList, String fileName) {
try {
File pajamuFile = new File(
UserDataLoader.INSTANCE.getCurrentUserFolder() + fileName);
JAXBContext context = JAXBContext.newInstance(wrapperInstance.getClass());
Marshaller m = context.createMarshaller();
WRAPPER wrapper = wrapperInstance;
wrapper.setDataList(dataList);
m.marshal(wrapper, pajamuFile);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
在 google 中,我发现大多数情况都是这样,但是在 spring 中,我在这里没有使用 spring,所以也许有任何明确的解决方案可以解决我想做的事情.
Do not know where is problem, maybe someaone can help me
错误消息是不言自明的。下面的语句 returns a Class
:
(getClass().getGenericSuperclass())
而您正试图将其转换为 ParameterizedType
((ParameterizedType) (getClass().getGenericSuperclass()))
Class
和 ParameterizedType
是兄妹。兄弟不是姐妹,所以试图把他们放在一起是残忍的。
也就是说,一个快速解决您的问题的方法是让客户端代码将 Class
类型传递给您的 XmlUtils
class。
public class XMLUtils<MODEL extends AbstractModel, WRAPPER extends WraperInterface<MODEL>> {
private WRAPPER wrapperInstance;
public XMLUtils(Class<WRAPPER> wrapperInstance) throws InstantiationException, IllegalAccessException {
this.wrapperInstance = wrapperInstance
}
//more code follows
}
你期望结果如何?
为了简化您的问题,可以说您有以下 class:
class XMLUtils extends java.lang.Object {
public XMLUtils() {
getClass().getGenericSuperclass()// == java.lang.Object
}
}
现在您正在尝试将 java.lang.Object
转换为 ParameterizedType
,但这仅在 class 是通用 class 的情况下才有效。因此它失败了...
您可能需要使用 TypeVariable
而不是 ParameterizedType
。
你检查过从getClass().getGenericSuperclass()
返回的对象的类型了吗?即
Class<?> c = getClass().getGenericSuperclass();
if (c instanceof ParameterizedType) {
System.out.println("It is an instanceof ParameterizedType");
}
else if (c instanceof TypeVariable) {
System.out.println("It is an instanceof TypeVariable");
}
else {
System.out.println("It is something else");
}