Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't invoke method with varargs parameters with reflection - NoSuchMethodException

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?
like image 398
catch23 Avatar asked Sep 19 '25 01:09

catch23


2 Answers

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);
like image 70
Sage Avatar answered Sep 20 '25 15:09

Sage


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);
    }
}
like image 41
peter.petrov Avatar answered Sep 20 '25 14:09

peter.petrov