0

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.

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.

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

3) Finally, I need to invoke each of the methods in the finalized list. 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? 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. 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.

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?

4
  • 3
    First step: read the Method#invoke() javadoc. It answers both your questions. Commented Dec 3, 2013 at 21:50
  • What is that? I am sorry, really new to java and SO, so I am not familiar with it. Commented Dec 3, 2013 at 21:53
  • Java provides really good documentation for its JDK in the form of Javadoc. You can read about the Method class and its methods here. Commented Dec 3, 2013 at 21:56
  • 2
    When you are new to java you should probably not start with reflection. Commented Dec 3, 2013 at 22:01

1 Answer 1

3

You're using .invoke incorrectly. 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.

You have never specified an object nor parameters. My sample uses no parameters so I can put null instead. But either way you have to provide an instance as the first parameter (unless it is static).

Sign up to request clarification or add additional context in comments.

3 Comments

If it is static, you can pass either an instance or null as the argument to the first parameter.
Is it really necessary to throw all those exceptions? It turns out that was one of my big problems. I got it all figured out now though.
@user2993636: either you throw them or you catch them. I didn't catch them because it would take attention away from the actual purpose of my answer, but anything beyond a code sample should handle the exceptions properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.