为什么Method.Invoke会抛出IllegalArgumentException?

时间:2017-04-22 11:47:53

标签: java methods reflection instance illegalargumentexception

对于编程实际任务,我们给出了一个示例.class文件。我们需要打印出所有采用1 x int输入和一些可打印输出的方法,然后允许用户使用命令行给出的一个输入来运行该方法。但是,当我尝试调用该方法时,抛出了IllegalArgumentException。

我抛出异常的代码:

// Request the user enter an integer and run the requested method.
private static void methodInvoke(Method inMethod, Class methodClass, Scanner scanner) throws 
NumberFormatException,IllegalAccessException,InvocationTargetException,InstantiationException,ClassNotFoundException
{
    Integer userNumber = 0;
    Object methodObject = methodClass.newInstance();

    System.out.println(inMethod.toString()); // Test to confirm printing the correct method.

    System.out.println("Enter a number to supply to: '" + inMethod.toString() + ":");
    userNumber = Integer.getInteger(scanner.nextLine());

    System.out.println(inMethod.invoke(methodObject, userNumber)); // Throws IllegalArgumentException here.
}

作为一些自动防故障检查,我已完成以下操作:

  • 打印整数以确认扫描程序正确读取它。
  • 测试了我知道代码的示例文件:
public class TestFile
{
    public int testMethod(int testInt)
    {
        return 2*testInt;
    }
}

发生命令行输出:

Enter a number to supply to: 'public int TestFile.testMethod(int):
1
Error: Invalid argument.
java.lang.IllegalArgumentException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at MethodMetrics.methodInvoke(MethodMetrics.java:79)
    at MethodMetrics.main(MethodMetrics.java:29)

任何有关原因的想法都将受到赞赏。我错过了一些明显的东西吗:)

编辑:这是选择方法的代码:

private static Method[] getIntMethods(Class classToTest) throws NullPointerException
    {
        int counter = 0;
        Method[] allFoundMethods = classToTest.getDeclaredMethods(); // Unnecessary to check for SecurityException here
        Method[] returnMethods = new Method[allFoundMethods.length];

        if(returnMethods.length > 0) // Only bother if the class has methods.
        {   
            for(Method mTest : allFoundMethods)
            {
                if(mTest.getParameterTypes().length == 1)
                {
                    if(mTest.getParameterTypes()[0].getName().equals("int"))
                    {
                        returnMethods[counter++] = mTest;
                    }
                }
            }
            returnMethods = Arrays.copyOf(returnMethods, counter);
        }

        return returnMethods;
    }

AND" methodInvoke"从主方法调用:

System.out.println("Select a method with the method index from above: '(0) to (n-1)':");
selectedMethod = scanner.nextLine();
methodInvoke(foundMethods[Integer.parseInt(selectedMethod)], scanner);

1 个答案:

答案 0 :(得分:3)

您的通话失败是因为您将int值传递给IllegalArgumentException参数。

invoke()的Javadoc说:

  

如果方法是实例方法并且指定的对象参数不是声明基础方法(或其子类或实现者)的类或接口的实例,则抛出null ;如果实际参数和形式参数的数量不同; 如果原始参数的展开转换失败;或者,如果在可能的解包后,参数值无法通过方法调用转换转换为相应的形式参数类型。

由于将int转换为IllegalArgumentException失败,您获得null

你有null值的原因是你正在调用错误的方法。 Integer.getInteger(String nm)的Javadoc说:

  

确定具有指定名称的系统属性的整数值。

     

如果没有具有指定名称的属性,如果指定的名称为空或null,或者属性没有正确的数字格式,则返回 Integer < /强>

解决方案:您应该致电Integer.valueOf(String s)

  

返回包含指定String值的{{1}}对象。该参数被解释为表示带符号的十进制整数。