在方法调用期间将数组作为参数传递时出现 IllegalArgumentException
IllegalArgumentException when passing an Array as parameter during method invocation
我一定错过了一些非常基本的东西。当我尝试在方法调用期间传递任何类型的数组时,出现错误。但是,当我正常执行时,它会起作用。
这是失败的完整代码
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) throws Exception {
// Normal
MyClass.sayHello(new String[] {"Whosebug"});
// Reflection
Method m = MyClass.class.getMethod("sayHello", String[].class);
m.invoke(null, new String[]{"Whosebug"});
}
static class MyClass {
public static void sayHello(String[] args) {
System.out.println("Hello " + args[0]);
}
}
}
抛出异常
Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
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:497)
at Main.main(Main.java:11)
String...
也不起作用。
问题是 invoke
的第二个参数是一个参数数组 - 您只指定了一个参数。
在大多数情况下这没问题,因为 Method.invoke
的第二个参数是可变参数,但由于您的参数已经是与 Object[]
兼容的数组,编译器不会创建包装数组。我希望您会收到这样的编译时警告:
Main.java:11: warning: non-varargs call of varargs method with inexact argument type for
last parameter;
m.invoke(null, new String[]{"Whosebug"});
^
cast to Object for a varargs call
cast to Object[] for a non-varargs call and to suppress this warning
您可以显式创建一个包装参数的数组,或者将参数转换为 Object
以便编译器需要自行包装它:
// Creates the wrapper array explicitly
m.invoke(null, new Object[] { new String[] { "Whosebug" } });
或
// Compiler creates the wrapper array because the argument type is just Object
m.invoke(null, (Object) new String[] { "Whosebug" });
我一定错过了一些非常基本的东西。当我尝试在方法调用期间传递任何类型的数组时,出现错误。但是,当我正常执行时,它会起作用。
这是失败的完整代码
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) throws Exception {
// Normal
MyClass.sayHello(new String[] {"Whosebug"});
// Reflection
Method m = MyClass.class.getMethod("sayHello", String[].class);
m.invoke(null, new String[]{"Whosebug"});
}
static class MyClass {
public static void sayHello(String[] args) {
System.out.println("Hello " + args[0]);
}
}
}
抛出异常
Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
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:497)
at Main.main(Main.java:11)
String...
也不起作用。
问题是 invoke
的第二个参数是一个参数数组 - 您只指定了一个参数。
在大多数情况下这没问题,因为 Method.invoke
的第二个参数是可变参数,但由于您的参数已经是与 Object[]
兼容的数组,编译器不会创建包装数组。我希望您会收到这样的编译时警告:
Main.java:11: warning: non-varargs call of varargs method with inexact argument type for
last parameter;
m.invoke(null, new String[]{"Whosebug"});
^
cast to Object for a varargs call
cast to Object[] for a non-varargs call and to suppress this warning
您可以显式创建一个包装参数的数组,或者将参数转换为 Object
以便编译器需要自行包装它:
// Creates the wrapper array explicitly
m.invoke(null, new Object[] { new String[] { "Whosebug" } });
或
// Compiler creates the wrapper array because the argument type is just Object
m.invoke(null, (Object) new String[] { "Whosebug" });