Java 尝试获取泛型类型参数时发生ClassCastException
我想使用反射获取泛型的实例:Java 尝试获取泛型类型参数时发生ClassCastException,java,generics,reflection,Java,Generics,Reflection,我想使用反射获取泛型的实例: this.wrapperInstance = ((Class<WRAPPER>) ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments()[1]).newInstance(); 不知道哪里有问题,也许有人能帮我?有完整的类别代码: public class XMLUtils<MODEL extends AbstractMod
this.wrapperInstance = ((Class<WRAPPER>) ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments()[1]).newInstance();
不知道哪里有问题,也许有人能帮我?有完整的类别代码:
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();
}
}
公共类XMLUtils{
私有包装器实例;
@抑制警告(“未选中”)
public XMLUtils()抛出实例化异常,IllegalAccessException{
this.wrapperInstance=((类)((参数化类型)(getClass().getGenericSuperclass())).getActualTypeArguments()[1]).newInstance();
}
@抑制警告(“未选中”)
公共列表loadDataFromXML(字符串文件名){
试一试{
File pajamuFile=新文件(
UserDataLoader.INSTANCE.getCurrentUserFolder()+文件名);
JAXBContext context=JAXBContext.newInstance(wrapperInstance.getClass());
Unmarshaller um=context.createUnmarshaller();
WRAPPER=(WRAPPER)um.unmarshal(pajamuFile);
返回wrapper.getDataList();
}捕获(JAXBEException e){
e、 printStackTrace();
返回新的ArrayList();
}
}
public void saveDataToXML(列表数据列表,字符串文件名){
试一试{
File pajamuFile=新文件(
UserDataLoader.INSTANCE.getCurrentUserFolder()+文件名);
JAXBContext context=JAXBContext.newInstance(wrapperInstance.getClass());
Marshaller m=context.createMarshaller();
包装器=包装器实例;
wrapper.setDataList(dataList);
m、 元帅(包装工,pajamuFile);
}捕获(JAXBEException e){
e、 printStackTrace();
}
}
}
在谷歌我发现最类似的情况,但在spring中,我没有使用spring,所以可能有任何明确的解决方案来解决我想做的事情
不知道哪里有问题,也许有人能帮我
错误消息是不言自明的。下面的语句返回一个类
:
(getClass().getGenericSuperclass())
您正试图将其强制转换为参数化类型
((ParameterizedType) (getClass().getGenericSuperclass()))
类
和参数化类型
是同级。兄弟不是姐妹,所以试图把他们彼此抛弃是残忍的
也就是说,问题的一个快速解决方案是要求客户机代码将类
类型传递给XmlUtils
类
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
}
公共类XMLUtils{
私有包装器实例;
公共XMLUtils(类包装器实例)抛出InstanceionException、IllegalAccessException{
this.wrapperInstance=wrapperInstance
}
//下面是更多的代码
}
您希望结果如何
为了简化您的问题,可以说您有以下课程:
class XMLUtils extends java.lang.Object {
public XMLUtils() {
getClass().getGenericSuperclass()// == java.lang.Object
}
}
现在,您正试图将
java.lang.Object
转换为ParameterizedType
,但这仅在类是泛型类的情况下有效。因此它失败了…您可能需要使用TypeVariable
而不是parameteredType
您是否检查了从getClass().getGenericSuperclass()返回的对象的类型?i、 e
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");
}
Class c=getClass().getGenericSuperclass();
if(参数化类型的c实例){
System.out.println(“它是参数化类型的实例”);
}
else if(类型变量的c实例){
System.out.println(“它是TypeVariable的实例”);
}
否则{
System.out.println(“这是另一回事”);
}
请阅读:@Edgar查看,它允许您解析泛型类型,而无需传递类引用。好的,谢谢您的建议。我本想在这里这样做,但很可能我误解了:
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");
}