Atitit.通过null 参数 反射  动态反推方法调用

Atitit.通过null 参数 反射  动态反推方法调用 

 

此时,直接使用java  apacheref工具都失效了。必须要自己实现了。

如果调用接口方法的话,就不能使用apacheref工具,可以使用javaref。。

 

思路流程

首先,通过参数 反推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(stackparamNum);

 

Object o = objs_tmp[paramNum - 1];

 

Object[] params = AArrays.left(objs_tmpparamNum - 1);

 

ttx t = (ttx) o;

// try {

// Object ret = MethodUtils.invokeMethod(t, methparams);

// 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(class1methparams);

Object ret = m1.invoke(oparams);

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,methparamTypes);

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(methparamTypes);

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(suitParamNtypesparams,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.lengthi++) {

if (params[i] == null)

continue;

if (params[i].getClass() != paramTypes[i])

return false;

 

}

return true;

}

 

---end

 

 

posted @ 2016-02-11 23:16  attilaxAti  阅读(31)  评论(0编辑  收藏  举报