我正在尝试使用Java反射调用私有方法,我开发了一个小方法来调用其他方法,迭代所有方法并按名称和参数类型进行比较,我已经成功调用了4个方法。
我有一个问题。
1)。这是最好的方法吗?因为我懂课。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
(单数)方法,该方法将方法名和参数类型类的可变参数作为参数。这应该能让你直接找到方法,这样你就不必自己遍历所有的方法了。
的例子:
clazz.getDeclaredMethod(methodName, parametersClass);
查看如何使用java反射API调用带参数的私有方法-
这里我写了一个名为executeprivatemmethod的方法,如:
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<String, String> requestParams = new HashMap<String, String>();
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);