Atitit.通过null 参数 反射 动态反推方法调用
Atitit.通过null 参数 反射 动态反推方法调用
此时,直接使用java apache的ref工具都失效了。必须要自己实现了。
如果调用接口方法的话,就不能使用apache的ref工具,可以使用java的ref。。
思路流程
首先,通过参数 反推Class<?>[] paramTypes, 然后调用api
如果为null ,使用methodname where by name集合,如果只有一个最好了。
{
然后判断参数个数,如果不符合,抛出异常。
}
如果不为一个,则跑出异常。。模糊不能精确确定内个method
作者:: 绰号:老哇的爪子 ( 全名::Attilax Akbar Al Rapanui 阿提拉克斯 阿克巴 阿尔 拉帕努伊 ) 汉字名:艾龙, EMAIL:1466519819@qq.com
转载请注明来源: http://blog.csdn.net/attilax
@SuppressWarnings("all")
private <ttx> void invokeinterface(Class<ttx> class1, String meth,
int paramNum) {
Object[] objs_tmp = VmUtil.pop2objArr(stack, paramNum);
Object o = objs_tmp[paramNum - 1];
Object[] params = AArrays.left(objs_tmp, paramNum - 1);
ttx t = (ttx) o;
// try {
// Object ret = MethodUtils.invokeMethod(t, meth, params);
// stack.push(ret);
// } catch (NoSuchMethodException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// } catch (IllegalAccessException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// } catch (InvocationTargetException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
try {
Method m1 = refx.getMeth(class1, meth, params);
Object ret = m1.invoke(o, params);
stack.push(ret);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static Method getMeth(Class<? extends Object> class1, String meth, Object[] params) {
Class<?>[] paramTypes =refx. getParamTypes(params);
Method m=getMethByParamTypes(class1,meth, paramTypes);
if(m==null)
m=getMethByParams(class1,meth,params);
return m;
}
private static Method getMethByParamTypes(
Class<? extends Object> class1, String meth, Class<?>[] paramTypes) {
try {
return class1.getMethod(meth, paramTypes);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
System.out.println("---------warning...");
e.printStackTrace();
return null;
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
*/
private static Method getMethByParams(Class<? extends Object> class1,
String meth, Object[] params) {
List<Method> li=Lists.newArrayList();
Method[] ma=class1.getDeclaredMethods();
for (Method method : ma) {
if(method.getName().equals(meth))
{
Class<?>[] paramTypes=method.getParameterTypes();
if(suitParamNtypes( params,paramTypes))
li.add(method);
}
}
if(li.size()>1) {
String string = "The method @m@(..) is ambiguous for the type @cls@".replace("@m@", meth).replace("@cls@", class1.getName());
throw new RuntimeException(string);
}
return li.get(0);
}
private static boolean suitParamNtypes(Object[] params,
Class<?>[] paramTypes) {
if (params.length != paramTypes.length)
return false;
for (int i = 0; i < paramTypes.length; i++) {
if (params[i] == null)
continue;
if (params[i].getClass() != paramTypes[i])
return false;
}
return true;
}
---end
相关文章
- Win10 设备管理器一个USB设备描述符请求失败解决方法
- 调用反射类的指定方法
- 调用反射类的方法
- java中反射机制通过字节码文件对象获取字段和函数的方法
- java反射--方法反射的基本操作
- Python生成随机数组的方法小结
- GitHub访问速度慢的解决方法
- java反射机制,通过类名获取对象,通过方法名和参数调
- 去掉织梦后台验证码的方法
- 反射方法获取事件的委托链上的函数
- Javascript定义类(class)的三种方法
- Atitit.通过null 参数 反射 动态反推方法调用
- SAP UI5 应用开发教程之五十七 - 基于 OData 注解的 Smart Field 使用方法学习试读版
- 解决hide方法反射限制的问题
- Android kotlin 扩展方法和扩展属性的用法
- 利用JAVA反射机制实现调用私有方法
- 复分析可视化方法:笔记:log(z)的可视化微分法
- 软件工程——理论、方法与实践 之 软件测试
- java Collections.sort()实现List排序的默认方法和自定义方法
- 解决的方法:warning: Clock skew detected. Your build may be incomplete.
- uitextField单词的方法和抖动的限制
- 时序违例的解决方法
- c#反射入门篇(Reflection)——MethodInfo 发现方法的属性
- ML之ME:机器学习之风控业务中常用模型评估指标PSI(人群偏移度指标)的的简介、使用方法、案例应用之详细攻略