Can't invoke method with varargs parameters wi

2020-03-25 08:22发布

I'm trying to use reflection to invoke method with varargs parameters.

And caught NoSuchMethodException. I couldn't figure out what is wrong here.

Code:

public class ReflectionTest {

    public ReflectionTest() {   }

    private void varargMethod(String string, Integer ... var) {

        System.out.println("vargarMethod() called");
        System.out.println(string + " Number of args: " + var.length
                + "\nContents");

        for (int i = 0; i < var.length; i++) {
            System.out.printf(" args %d: %d", i, var[i]);
            //System.out.println(" args " + i + ": " + var[i]);
        }

        System.out.println();
    }

    public static void main(String[] args) throws Exception {

        Class[] parameterTypes = new Class[] { String.class, int.class, int.class, int.class };

        //System.out.println(Arrays.toString(parameterTypes));

        Method varMeth = test.getClass().getDeclaredMethod("varargMethod", parameterTypes);
        System.out.println("varMeth" + varMeth);

        Object[] arguments = new Object[] { new String("my perfect string"),
                new Integer(10), new Integer(100), new Integer(47) };

        varMeth.invoke(test, arguments);

It throws exactly:

java.lang.NoSuchMethodException: ReflectionTest.varargMethod(java.lang.String, int, int, int)

  • How to solve this trouble?

3条回答
虎瘦雄心在
2楼-- · 2020-03-25 08:59

The parameter types should be Integer[].class for a varargs as varargs expression specifies that the parameter is a variable array parameter. So a correct way is:

Class[] parameterTypes = new Class[] { String.class, Integer[].class };

//System.out.println(Arrays.toString(parameterTypes));
ReflectionTest test = new ReflectionTest();
Method varMeth = test.getClass().getDeclaredMethod("varargMethod", 
                                                      parameterTypes);
System.out.println("varMeth" + varMeth);

Object[] arguments = new Object[] { new String("my perfect string"),
                                    new Integer[]{10, 100, 47}
                                  };

varMeth.invoke(test, arguments);
查看更多
We Are One
3楼-- · 2020-03-25 09:06

If you compile your .java file and then decompile your .class file, you will see this declaration of the method:

private transient void varargMethod(String string, Integer var[])

So here is your code fixed.

import java.lang.reflect.Method;

public class ReflectionTest {

    public ReflectionTest() {   }

    private void varargMethod(String string, Integer ... var) {

        System.out.println("vargarMethod() called");
        System.out.println(string + " Number of args: " + var.length
                + "\nContents");

        for (int i = 0; i < var.length; i++) {
            System.out.printf(" args %d: %d", i, var[i]);
            //System.out.println(" args " + i + ": " + var[i]);
        }

        System.out.println();
    }

    public static void main(String[] args) throws Exception {

        ReflectionTest test = new ReflectionTest(); 
        Class[] parameterTypes = new Class[] { String.class, Integer[].class };

        //System.out.println(Arrays.toString(parameterTypes));

        Method varMeth = test.getClass().getDeclaredMethod("varargMethod", parameterTypes);
        System.out.println("varMeth" + varMeth);

        Object[] arguments = new Object[] { new String("my perfect string"),
                new Integer[] { new Integer(10), new Integer(100), new Integer(47) } };

        varMeth.invoke(test, arguments);
    }
}
查看更多
爷、活的狠高调
4楼-- · 2020-03-25 09:14

Here's the fixed code, with minimal changes. Comments inline for changes:

import java.lang.reflect.Method;

public class ReflectionTest {

    public ReflectionTest() {}

    private void varargMethod(String string, Integer... var) {

        System.out.println("vargarMethod() called");
        System.out.println(string + " Number of args: " + var.length + "\nContents:");

        for (int i = 0; i < var.length; i++) {
            System.out.printf(" args %d: %d", i, var[i]);
        }

        System.out.println();
    }

    public static void main(String[] args) throws Exception {

        Class[] parameterTypes = new Class[] {
                                        String.class,
                                        Integer[].class // use array to represent varargs
                                      };

        //System.out.println(Arrays.toString(parameterTypes));
        ReflectionTest test = new ReflectionTest();
        Method varMeth = test.getClass().getDeclaredMethod("varargMethod", parameterTypes);
        System.out.println("varMeth" + varMeth);

        Object[] arguments = new Object[]{
                                    "my perfect string",
                                    new Integer[]{10, 100, 47} // again use array for varargs
                                  };

        varMeth.invoke(test, arguments);
    }

}
查看更多
登录 后发表回答