Java Reflection私有方法与参数最佳方法?

我试图使用javareflection调用私有方法我开发了一个小方法来调用其他方法迭代所有方法并按名称和参数类型进行比较我已成功调用4个方法。

我有一个问题。

1)。 这是最好的方法吗? 因为我理解class.getMethod只匹配公共方法。 Java内置了什么?

这是我的代码。

public class Compute { private Method getMethod(Class clazz,String methodName,Class...parametersClass) { outer: Method[] methods = clazz.getDeclaredMethods(); for(Method method:methods) { //comparing the method types... of the requested method and the real method..... if(method.getName().equals(methodName) && parametersClass.length== method.getParameterTypes().length)//it a possible match { Class[]arrayForRealParameters = method.getParameterTypes(); //comparing the method types... of the requested method and the real method..... for(int i=0;i<arrayForRealParameters.length;i++)if(!arrayForRealParameters[i].getSimpleName().equals(parametersClass[i].getSimpleName()))continue outer; return method; } } return null; } private Calendar getCalendar(){return java.util.Calendar.getInstance();} private void methodByReflex(){System.out.println("Empty Parameters.");} private void methodByReflex(Integer a,Integer b){System.out.println(a*b);} private void methodByReflex(String a,String b){System.out.println(a.concat(b));} public static void main(String[] args) throws Exception { Compute clazz = new Compute(); Class[]arrayOfEmptyClasses=new Class[]{}; Class[]arrayOfIntegerClasses=new Class[]{Integer.class,Integer.class}; Class[]arrayOfStringClasses=new Class[]{String.class,String.class}; Method calendarMethod=clazz.getMethod(clazz.getClass(),"getCalendar",arrayOfEmptyClasses); Method emptyMethod=clazz.getMethod(clazz.getClass(),"methodByReflex",arrayOfEmptyClasses); Method intMethod=clazz.getMethod(clazz.getClass(),"methodByReflex",arrayOfIntegerClasses); Method stringMethod=clazz.getMethod(clazz.getClass(),"methodByReflex",arrayOfStringClasses); System.out.println((calendarMethod==null)+" "+(emptyMethod==null)+" "+(intMethod==null)+" "+(stringMethod==null));//prints false false false false Calendar cal = (Calendar)calendarMethod.invoke(clazz); System.out.println(cal.getTime());//prints current time stringMethod.invoke(clazz,"John ","Lennon");//Prints John Lennon emptyMethod.invoke(clazz);//prints Empty Parameters. intMethod.invoke(clazz,13,13);// prints 169 Integer a=10,b=10; intMethod.invoke(clazz,a,b);//prints 100 } 

}

任何帮助都是欣赏它。 非常感谢。

您将获得具有getDeclaredMethods的类的所有方法的列表。 尝试使用getDeclaredMethod (singular)方法,该方法将方法名称和参数类型的类的varargs参数作为参数。 这应该让你直接进入方法,这样你就不必自己迭代所有方法。

例:

 clazz.getDeclaredMethod(methodName, parametersClass); 

看看我如何使用javareflectionAPI调用带参数的私有方法 –

在这里,我编写了一个名为executePrivateMethod的方法,如:

 public Object executePrivateMethod(Class clazz,String methodName,Class[] parameterTypes,Object ... args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, SecurityException, NoSuchMethodException{ //get declared Method for execution Method pvtMethod = clazz.getDeclaredMethod(methodName,parameterTypes); pvtMethod.setAccessible(true); //invoke loadConfiguration() method and return result Object return pvtMethod.invoke(clazz.newInstance(),args); } 

怎么称呼:

 //params private Map requestParams = new HashMap(); requestParams.put("a","a"); requestParams.put("b","b"); requestParams.put("c","c"); requestParams.put("d","d"); //call executePrivateMethod(JSGeneratorFacade.class,"testMethod",new Class[]{Map.class},requestParams);