5

I've got something along the lines of the following:

public class A { 
    public void theMethod(Object arg1) {
        // do some stuff with a single argument
    }
}

public class B {
    public void reflectingMethod(Object arg) {
        Method method = A.class.getMethod("theMethod", Object.class);
        method.invoke(new A(), arg);
    }
}

How do I modify that so that I can do the following instead?

public class A { 
    public void theMethod(Object... args) {
        // do some stuff with a list of arguments
    }
}

public class B {
    public void reflectingMethod(Object... args) {
        Method method = A.class.getMethod("theMethod", /* what goes here ? */);
        method.invoke(new A(), args);
    }
}
3
  • Have you tried List<Object>? Commented Dec 6, 2011 at 18:48
  • That worked. I'd tried it before but gotten stumped at how to get the class for List<Object>, since ".class" isn't allowed on generics. Creating a new empty arraylist works though (as long as the method signatures use ArrayList<Object> for the arguments. Commented Dec 6, 2011 at 19:18
  • @Darthenius, well a List is not really a varargs :-) btw, James, you should be able to look up the method (with List) by doing List.class I guess... Commented Dec 6, 2011 at 20:06

2 Answers 2

5
A.class.getMethod("theMethod", Object[].class);
Sign up to request clarification or add additional context in comments.

6 Comments

It's the callee you're looking for ... not a particular caller.
With reflection, the method that is returned is the one with a matching method name and parameters. In this case, the method name is theMethod and the parameters is an object array. If you pass invoke different subclasses classes of Object it will not care.
Object[].class caused an exception when trying to invoke the reflected method if I passed in more than one item in the array. For instance a String[] of {"a", "b", "c"} complained that there were too many arguments (it expected 1 but got three).
I'll move this to an answer once the system will let m: Darthenius's suggestion in the comments for the original question worked, once I wrapped my head around how to do it. public class A { public void theMethod(ArrayList<Object> args) { // do stuff } } public class B { public void reflectingMethod(ArrayList<Object> args) { Method method; try { method = A.class.getMethod("theMethod", args.getClass()); method.invoke(new A(), args); } catch (Exception e) { } } }
@JamesMcMurray, you can actually use varargs. Just invoke the method wrapping the args inside a Object[] e.g. method.invoke(new A(), new Object[] {new Object[] { "a", "b", "c" }} ). Basically, invoke method also takes varargs so can confuse where the array is directed.
|
0

Darthenius's suggestion in the comments for the original question worked, once I wrapped my head around how to do it.

public class A {
    public void theMethod(ArrayList<Object> args) { // do stuff 
    }
}

public class B {
    public void reflectingMethod(ArrayList<Object> args) {
        Method method;
        try {
            method = A.class.getMethod("theMethod", args.getClass());
            method.invoke(new A(), args);
        } catch (Exception e) {}
    }
}

Comments

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.