求助错误解决 java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

J2EE 码拜 9年前 (2015-05-10) 1662次浏览 0个评论
 

用的CGLIB代理的,不知道哪个出错了,不用CGLIB代理的话,就又会出现代理对象找不到该方法的异常
求解决。。。

没有人啊??
2分
类型不能转换,代码呢
下面是BaseAction.java

package cn.han.action;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings(“serial”)
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

public T model;

public BaseAction(){
/*Type te=this.getClass().getGenericSuperclass();
ParameterizedType type= (ParameterizedType)te;
Class clazz=(Class) type.getActualTypeArguments()[0];*/

Class clazz= GenericSuperClass.getGenericSuperclass(getClass());
try {
model=(T)clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return null;
}

@Override
public T getModel() {
// TODO Auto-generated method stub
return model;
}

}

下面是GenericSuperClass.java

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class GenericSuperClass {

/**范类转换*/
public static Class getGenericSuperclass(Class entity) {
ParameterizedType type = (ParameterizedType) entity.getGenericSuperclass();
Class entityClass = (Class) type.getActualTypeArguments()[0];
return entityClass;
}

}

下面是实现类的Action   ElecTextAction.java

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.han.domain.ElecText;
import cn.han.service.IElecTextService;
@SuppressWarnings(“serial”)
@Controller(“elecTextAction”)
@Scope(value=”prototype”)
public class ElecTextAction extends BaseAction<ElecText> {
    
public ElecText et=super.model;
@Resource(name=IElecTextService.SERVICE_NAME)
public IElecTextService service;
public String save(){
service.save(et);
return “asave”;
}

}

4分
http://www.blogjava.net/leisure/archive/2011/12/26/367185.html

这个能解决你的问题。

5分
LZ啊 你看看你的ElecTextAction  BaseAction这些对象的实例是不是给Spring管理,让它创建了代理。
比如你用的ElecTextAction  对象的实例其实是代理。这时候你getClass() 这个Class对象可就不是你想要的Class了,这种情况下就会出现你说的异常。
ElecTextAction确实是Spring代理了。。那怎么弄呢
9分
引用 8 楼 hanyingjunshishen 的回复:

ElecTextAction确实是Spring代理了。。那怎么弄呢

对没用过CGLIB。
要解决的问题就是拿到真实对象的实例。比如A的实例a,创建代理后的实例proxiedA,你拿到proxiedA后怎么才能拿到a。起码到现在,JDK的代理机制了我是没找到拿甚是对象的办法。
CGLIB也许方便些。

刚刚解决了,通过CGLIB代理生成的类应该是继承了被代理的类,所以getGenericSuperclass只是取到被代理类的Type。正常应该是要取到被代理类所继承的父类的Type,所以要先getSuperclass取到被代理类再getGenericSuperclass取到父类的Type才成

Type type = getClass().getGenericSuperclass();
if(!(type instanceof ParameterizedType)){
	type = getClass().getSuperclass().getGenericSuperclass();
}
Class<T> cls = (Class<T>)((ParameterizedType)type).getActualTypeArguments()[0];

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求助错误解决 java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!