Method.Invoke这里为什么抛出IllegalArgumentException?

Why is IllegalArgumentException thrown here by Method.Invoke?

对于编程实际任务,我们给出了一个示例 .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" 是从 main 方法调用的:

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

您的调用失败,因为您将 null 值传递给 int 参数。

invoke() 的 Javadoc 说:

Throws IllegalArgumentException if the method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or implementor thereof); if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion.

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

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

Determines the integer value of the system property with the specified name.

If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.

解决方法:你应该调用Integer.valueOf(String s).

Returns an Integer object holding the value of the specified String. The argument is interpreted as representing a signed decimal integer.