Java Reflection私有方法与参数的最佳方法? [英] Java Reflection private method with parameters best approach?

查看:85
本文介绍了Java Reflection私有方法与参数的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用java反射调用私有方法,我开发了一个小方法来调用其他方法,这些方法遍历所有方法并按名称和参数类型进行比较,我成功地调用了4种方法.

i am trying to call a private method using java reflection i developed a small method to call the others methods iterating over all methods and comparing by name and by parameters type i have invoke 4 methods with success.

我有一个问题.

1).这是最好的方法吗?因为我了解class.getMethod只匹配公共方法. Java有内置的东西吗?

1). is this the best way to do it?. because i understand class.getMethod only match public methods. Java have something built-in?

这是我的代码.

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
}

}

任何帮助都是值得的. 非常感谢.

any help is appreciate it. thanks a lot.

推荐答案

您将获得使用getDeclaredMethods的类中所有方法的列表.尝试使用

You are getting a list of all the methods on a class with getDeclaredMethods. Try using the getDeclaredMethod (singular) method, which takes as a parameter the method name and a varargs argument for the classes of the parameter types. That should get you directly to the method so you don't have to iterate through all methods yourself.

示例:

clazz.getDeclaredMethod(methodName, parametersClass);

这篇关于Java Reflection私有方法与参数的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆