无法使用带有反射的varargs参数调用方法 - NoSuchMethodException

时间:2013-12-07 11:31:57

标签: java reflection

我正在尝试使用反射来调用带有varargs参数的方法。

抓住了NoSuchMethodException。我无法弄清楚这里有什么问题。

代码:

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);

它完全抛出:

  

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

  • 如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

对于varargs,参数类型应为Integer[].class,因为varargs表达式指定参数是变量数组参数。所以正确的方法是:

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);

答案 1 :(得分:1)

如果编译.java文件然后反编译.class 文件,您将看到该方法的声明:

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

所以这是你的代码修复。

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

答案 2 :(得分:0)

这是固定代码,只需要很少的更改。内联更改的注释:

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

}