Java 1.5 Reflection: getMethod() when the method has varargs

Updated May 18, 2005 Shrunk width due to narrow WP style (it’s like coding in 30 columns per line)

package assertion;

import junit.framework.TestCase;

import java.lang.reflect.Method; import java.lang.reflect.Array;

public class ReflectionTest extends TestCase {

private class Shop {
    public void order(String ... productIds) {}
}

public void testReflectingOnVarargsIs-
                        EquivalentToReflectingOn-
                        AnyArray()
               throws NoSuchMethodException {

    Method goodMethod = Shop.class.
              getMethod("order",
              Array.newInstance(String.class,0).getClass());
    assertNotNull(goodMethod);

    try {
        Method badMethod = Shop.class.getMethod("order");
        fail();
    } catch (NoSuchMethodException e) {}

    try {
        Method badMethod = Shop.class.getMethod("order", String.class);
        fail();
    } catch (NoSuchMethodException e) {}

}

}