带参数的Java反射私有方法最佳方法?
我尝试使用java反射调用一个私有方法我开发了一个小方法来调用其他方法迭代所有方法并通过名称和参数类型进行比较我成功调用了4个方法 我有一个问题 1) 。这是最好的方法吗?。因为我知道class.getMethod只匹配公共方法。Java有内置的东西吗 这是我的密码带参数的Java反射私有方法最佳方法?,java,reflection,methods,parameters,private,Java,Reflection,Methods,Parameters,Private,我尝试使用java反射调用一个私有方法我开发了一个小方法来调用其他方法迭代所有方法并通过名称和参数类型进行比较我成功调用了4个方法 我有一个问题 1) 。这是最好的方法吗?。因为我知道class.getMethod只匹配公共方法。Java有内置的东西吗 这是我的密码 public class Compute { private Method getMethod(Class clazz,String methodName,Class...parametersClass) {
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
}
公共类计算
{
私有方法getMethod(类clazz、字符串方法名、类…参数Class)
{
外部:
方法[]methods=clazz.getDeclaredMethods();
用于(方法:方法)
{
//比较请求方法和实际方法的方法类型。。。。。
if(method.getName().equals(methodName)&¶metersClass.length==method.getParameterTypes().length)//可能匹配
{
类[]arrayForRealParameters=method.getParameterTypes();
//比较请求方法和实际方法的方法类型。。。。。
对于(int i=0;i您将使用getDeclaredMethods
获得一个类上所有方法的列表。请尝试使用(单数)方法,该方法将方法名和参数类型类的varargs参数作为参数。这将使您直接访问该方法,因此您不必自己迭代所有方法
例如:
clazz.getDeclaredMethod(methodName, parametersClass);
请参见如何使用java反射API调用带有参数的私有方法-
在这里,我编写了一个名为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);
}
public Object executePrivateMethod(Class clazz,String methodName,Class[]参数类型,Object…args)抛出IllegalArgumentException,IllegalAccessException,InvocationTargetException,实例化Exception,SecurityException,NoSuchMethodException{
//获取要执行的声明方法
方法pvtMethod=clazz.getDeclaredMethod(方法名,参数类型);
pvtMethod.setAccessible(true);
//调用loadConfiguration()方法并返回结果对象
返回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);
//参数
private-Map-requestParams=new-HashMap();
请求参数put(“a”、“a”);
请求参数put(“b”、“b”);
请求参数put(“c”、“c”);
请求参数put(“d”、“d”);
//召唤
executePrivateMethod(JSGeneratorFacade.class,“testMethod”,新类[]{Map.class},requestParams);
查看方法#setAccessible(true);
但是get declaredMethod只返回公共方法。我猜.getDeclaredMethod返回一个直接在类上声明的方法
;getMethod返回一个公共方法
方法
。此外,诸如私有方法
之类的访问修饰符仍然适用;它们可能导致非法访问异常n除非在方法
对象上调用setAccessible(true)
,否则将抛出。