Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java reflection when a method has a variable arglist

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);
    }
}
like image 708
Ginger McMurray Avatar asked Dec 06 '11 18:12

Ginger McMurray


1 Answers

A.class.getMethod("theMethod", Object[].class);
like image 83
Ransom Briggs Avatar answered Oct 19 '22 23:10

Ransom Briggs