日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通过反射执行方法

發布時間:2025/3/19 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过反射执行方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

新入職,上級分配了個任務,通過hessian實現rpc,服務端接口接收參數封裝成實體類,我實現了一個實體類RemoteParam,包括bean名稱,method名稱和參數列表,我通過class.getMethod(methodName,classes)來查找方法然后invoke執行它:


?public class RemoteParam implements Serializable{

? ? /**
*?
*/
private static final long serialVersionUID = 114263984568171511L;
/* bean名稱 ?*/
private String beanName;
/* 方法名稱 */
private String methodName;
/* 參數列表 */
private List<? extends Serializable> list;
/* 設置連接時間*/
private Long timeout;
/* 子站地址 */
private String website;



/**
* 無參構造方法
*/
public RemoteParam(){
}

/**
* 構造方法
* @param beanName?
* @param methodName
*/
public RemoteParam(String beanName,String methodName){
this.beanName=beanName;
this.methodName=methodName;
}
/**
* 構造方法
* @param beanName
* @param methodName
* @param list
*/
public RemoteParam(String beanName,String methodName,List<? extends Serializable> list){
this.beanName=beanName;
this.methodName=methodName;
if(list!=null){
if(!list.isEmpty()){
this.list=list;
}else{
this.list=null;
}
}else{
this.list=null;
}
}

/**
* 構造方法
* @param beanName
* @param methodName
* @param timeout
*/
public RemoteParam(String beanName,String methodName,Long timeout){
this.beanName=beanName;
this.methodName=methodName;
this.timeout=timeout;
}
/**
* 構造方法
* @param beanName
* @param methodName
* @param list
* @param timeout
*/
public RemoteParam(String beanName,String methodName,List<? extends Serializable> list,Long timeout){
this.beanName=beanName;
this.methodName=methodName;
if(list!=null){
if(!list.isEmpty()){
this.list=list;
}else{
this.list=null;
}
}else{
this.list=null;
}
this.timeout=timeout;
}


/**
* 構造方法
* @param beanName?
* @param methodName
*/
public RemoteParam(String beanName,String methodName,String website){
this.beanName=beanName;
this.methodName=methodName;
this.website=website;
}

/**
* 構造方法
* @param beanName
* @param methodName
* @param list
*/
public RemoteParam(String beanName,String methodName,List<? extends Serializable> list,String website){
this.beanName=beanName;
this.methodName=methodName;
if(list!=null){
if(!list.isEmpty()){
this.list=list;
}else{
this.list=null;
}
}else{
this.list=null;
}
this.website=website;
}

/**
* 構造方法
* @param beanName
* @param methodName
* @param timeout
*/
public RemoteParam(String beanName,String methodName,Long timeout,String website){
this.beanName=beanName;
this.methodName=methodName;
this.timeout=timeout;
this.website=website;
}
/**
* 構造方法
* @param beanName
* @param methodName
* @param list
* @param timeout
*/
public RemoteParam(String beanName,String methodName,List<? extends Serializable> list,Long timeout,String website){
this.beanName=beanName;
this.methodName=methodName;
if(list!=null){
if(!list.isEmpty()){
this.list=list;
}else{
this.list=null;
}
}else{
this.list=null;
}
this.timeout=timeout;
this.website=website;
}
public String getBeanName() {
return beanName;
}
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}

public List<? extends Serializable> getList() {
return list;
}
public void setList(List<? extends Serializable> list) {
this.list = list;
}

public Long getTimeout() {
return timeout;
}


public void setTimeout(Long timeout) {
this.timeout = timeout;
}

public String getWebsite() {
return website;
}


public void setWebsite(String website) {
this.website = website;
}

}

?@Override
public RemoteMsg getRemoteResult(RemoteParam paramObject,LoginBean loginBean){
Object result = null ;
? ?RemoteMsg remoteMsg = new RemoteMsg();
String beanName=paramObject.getBeanName();
String methodName=paramObject.getMethodName();
Object[] objects=null;
Class<?>[] classes=null;
if(paramObject.getList()==null){
objects=new Object[]{};
classes=new Class<?>[]{};
}else{
if(paramObject.getList().isEmpty()){
objects=new Object[]{};
classes=new Class<?>[]{};
}else{
objects=paramObject.getList().toArray();
classes=(Class<?>[])ListUtil.listToClasses(paramObject.getList()).toArray(new Class[0]);
}
}
result=this.invokeMethod(beanName, methodName, objects, classes);
Object jsonResult = JSON.toJSON(result);
remoteMsg.setResult(jsonResult);
return remoteMsg;
}

@Override
public <T> T getRemoteResult(RemoteParam paramObject,LoginBean loginBean,Class<T> clazz){
Object result = null ;
String beanName=paramObject.getBeanName();
String methodName=paramObject.getMethodName();
Object[] objects=null;
Class<?>[] classes=null;
if(paramObject.getList()==null){
objects=new Object[]{};
classes=new Class<?>[]{};
}else{
if(paramObject.getList().isEmpty()){
objects=new Object[]{};
classes=new Class<?>[]{};
}else{
objects=paramObject.getList().toArray();
classes=(Class<?>[])ListUtil.listToClasses(paramObject.getList()).toArray(new Class[0]);
}
}
result=this.invokeMethod(beanName, methodName, objects, classes);

return (T)result;
}


private Object invokeMethod(String beanName, String methodName,
Object[] params, Class... type) {
Object serviceObj = null;
try {
serviceObj = this.context.getBean(beanName);
} catch (NoSuchBeanDefinitionException e) {
throw new RemoteException(RemoteMsgUtils.BEAN_ERROR_TYPE);
}
if (serviceObj == null) {
throw new RemoteException(RemoteMsgUtils.BEAN_ERROR_TYPE);
}
Method m = null;
try {
m = serviceObj.getClass().getMethod(methodName, type);
} catch (Exception e) {
throw new RemoteException(RemoteMsgUtils.METHOD_ERROR_TYPE,
e.getLocalizedMessage());
}
if (m == null) {
throw new RemoteException(RemoteMsgUtils.METHOD_ERROR_TYPE);
}
Object result;
try {
result = m.invoke(serviceObj, params);
} catch (Exception e) {
e.printStackTrace();
log.error(RemoteReceivedServiceImpl.class, e);
// e是invoke的時候拋出來的.應該找它封裝的
String errorMsg = "";
try {
errorMsg = getErrorMsg(e);
} catch (Exception e1) {
// 說明沒有這個屬性,就不用了
}
RemoteException remoteException = null;
if (StringUtils.isNotBlank(errorMsg)) {
remoteException = new RemoteException(errorMsg);
remoteException.setErrorMsg(errorMsg);
} else {
remoteException = new RemoteException(
RemoteMsgUtils.INVOKE_ERROR_TYPE);
}
if (StringUtils.isNotBlank(errorMsg)) {


}
throw remoteException;
}
return result;

}

這里我是通過reflect獲取參數類型,然后傳入執行方法,但是問題來了,如果傳入的是一個引用類型例如User,ArrayList...我獲取到的參數類型就不是method的了,因為method作為公用方法,那么入參肯定是Object,List這些了,先前的想法是客戶端傳入需要轉換的class,但是正常Object可以了,但是List這種復雜對象就不ok了,所幸,class里面有個方法:Class.isAssignableForm(Class clz),可以匹配它的超類或借口。

重寫invokeMethod方法:

/**
? ?* 重寫invokeMethod
? ?*/
@SuppressWarnings("rawtypes")
private Object newInvokeMethod(String beanName, String methodName,
Object[] params, ?Class... type) {
Object serviceObj = null;
try {
serviceObj = this.context.getBean(beanName);
} catch (NoSuchBeanDefinitionException e) {
throw new RemoteException(RemoteMsgUtils.BEAN_ERROR_TYPE);
}
if (serviceObj == null) {
throw new RemoteException(RemoteMsgUtils.BEAN_ERROR_TYPE);
}
Method m = null;
Method[] ms=serviceObj.getClass().getMethods();
for(Method method:ms){
String mName=method.getName();
if(StringUtils.equals(methodName, mName)){
boolean isThis = true;
Class<?>[] pt=method.getParameterTypes();
if(type.length==pt.length){
int plength = type.length;
? ? ? ? ? ? ? ? ? ? ?if (plength == 0) {
? ? ? ? ? ? ? ? ? ? ? ? ?isThis = true;
? ? ? ? ? ? ? ? ? ? ?} else {
? ? ? ? ? ? ? ? ? ? ? ? ?for (int i = 0; i < plength; i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?boolean ff = pt[i]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.isAssignableFrom(type[i]);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if (!ff) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?isThis = false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ?}
}else {
isThis=false;
}
if (isThis) {
m=method ;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
}
}
if (m == null) {
throw new RemoteException(RemoteMsgUtils.METHOD_ERROR_TYPE);
}
Object result;
try {
result = m.invoke(serviceObj, params);
} catch (Exception e) {
e.printStackTrace();
log.error(RemoteReceivedServiceImpl.class, e);
// e是invoke的時候拋出來的.應該找它封裝的
String errorMsg = "";
try {
errorMsg = getErrorMsg(e);
} catch (Exception e1) {
// 說明沒有這個屬性,就不用了
}
RemoteException remoteException = null;
if (StringUtils.isNotBlank(errorMsg)) {
remoteException = new RemoteException(errorMsg);
remoteException.setErrorMsg(errorMsg);
} else {
remoteException = new RemoteException(
RemoteMsgUtils.INVOKE_ERROR_TYPE);
}
if (StringUtils.isNotBlank(errorMsg)) {


}
throw remoteException;
}
return result;


}

ok!問題解決。

總結

以上是生活随笔為你收集整理的通过反射执行方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。