简体   繁体   English

使用反射的调用方法

[英]Invoking method using reflection

I am trying to figure out how to invoke a method of a custom class. 我试图弄清楚如何调用自定义类的方法。 Here is the process of what I am trying to do: 这是我正在尝试执行的过程:

1) I initialize an array of methods from the list of methods of my custom class, and an empty List of Method which will be used to hold a filtered list of these methods. 1)我从我的自定义类的方法列表中初始化了一个方法数组,并初始化了一个空的方法列表,它将用于保存这些方法的过滤列表。

Method method[] = MyClass.getDeclaredMethods();
List<Method> x = new ArrayList<Method>();

2) I then run my array of methods through a for loop and filter out whichever methods do not fill my required criteria. 2)然后,我通过for循环运行方法数组,并过滤​​掉不符合我的要求标准的任何方法。

 for (Method m : methods){
       if(...){
          if(...){
               x.add(m);
          }
       }
    }

3) Finally, I need to invoke each of the methods in the finalized list. 3)最后,我需要调用最终列表中的每个方法。 This is where I am stuck, I am not exactly sure how the invoke function works. 这就是我遇到的问题,我不确定调用函数的工作方式。 Here is what I am trying: 这是我正在尝试的:

for(int i=0; i < x.size(); i++){
    boolean g = x.get(i).invoke();
        if(...)
        else(...)
}

The thing is, I know Exactly what it is I don't know, I am just having trouble finding the answers. 问题是,我确切地知道我所不知道的是什么,我只是找不到答案。 These are the questions I need answered: 这些是我需要回答的问题:

1) Which object will actually use the invoke function? 1)哪个对象将实际使用invoke函数? Is it going to be, in my case, the particular method I want to invoke, or an instance of the class I am trying to invoke? 就我而言,是要调用的特定方法还是要尝试调用的类的实例?

2) I know that the invoke function is going to require arguments, one of which is the parameter data for the method. 2)我知道invoke函数将需要参数,其中之一是方法的参数数据。 What I am unclear about is what exactly the first argument needs to be. 我不清楚的是第一个论点到底是什么。 I am thinking that the first argument is the actual method itself, but then I run into a logical loop, because the way I have it coded has the method using the invoke function, so I am stumped. 我以为第一个参数是实际的方法本身,但是随后我遇到了一个逻辑循环,因为我的编码方式具有使用invoke函数的方法,因此我很困惑。

3) In my case, the methods I wish to invoke don't actually take any parameters, so when I do happen to figure out how the invoke function works, will I need to set one of the arguments to null, or will I just omit that part of the argument list? 3)就我而言,我希望调用的方法实际上没有任何参数,因此当我确实弄清楚invoke函数的工作方式时,我需要将其中一个参数设置为null,还是忽略参数列表的那一部分?

You're using .invoke incorrectly. 您使用的.invoke错误。 See this short example: 请看以下简短示例:

public class Test {
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        X obj = new X();
        Method method = obj.getClass().getMethod("test", null);
        method.invoke(obj, null);
    }   
}

class X {
    public void test(){
        System.out.println("method call");
    }
}

Output: 输出:

method call 方法调用

More information in the docs . 文档中有更多信息。

Invokes the underlying method represented by this Method object, on the specified object with the specified parameters. 在具有指定参数的指定对象上调用此Method对象表示的基础方法。

You have never specified an object nor parameters. 您从未指定对象或参数。 My sample uses no parameters so I can put null instead. 我的示例不使用任何参数,因此我可以改用null But either way you have to provide an instance as the first parameter (unless it is static ). 但是无论哪种方式,您都必须提供一个实例作为第一个参数(除非它是static )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM