Software As She’s Developed

Mahemoff’s Podcast/Blog – Web, Programming, Usability from the Author of ‘Ajax Design Patterns’ (AjaxPatterns.org)

Software As She’s Developed header image 2

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

April 19th, 2005 · No Comments · SoftwareDev

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) {}

}

}

Tags:

No Comments so far ↓

Like gas stations in rural Texas after 10 pm, comments are closed.