使用 Java 反射调用 invoke 方法时出现 IllegalArgumentException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6680787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IllegalArgumentException when calling invoke method using Java Reflections
提问by Sarah Rushworth
I have a class that has a method as follows :-
我有一个具有如下方法的类:-
public void setCurrencyCode(List<String> newCurrencycode){
this.currencycode = newCurrencycode;
}
I am using Java Relections to invoke this method as follows :-
我正在使用 Java Reelections 调用此方法,如下所示:-
try {
List<String> value = new ArrayList<String>();
value.add("GB");
Class<?> clazz = Class.forName( "com.xxx.Currency" );
Object obj = clazz.newInstance();
Class param[] = { List.class };
Method method = obj.getClass().getDeclaredMethod( "setCurrencyCode", param );
method.invoke( value );
} catch(Exception e) {
System.out.println( "Exception : " + e.getMessage() );
}
However, an exception is raised on the "invoke" call :- java.lang.IllegalArgumentException: object is not an instance of declaring class
但是,在“调用”调用时引发异常:- java.lang.IllegalArgumentException: object is not an instance of declaring class
Any ideas?
有任何想法吗?
Thanks
谢谢
Sarah
莎拉
回答by Bohemian
You are not calling invoke()correctly: invoke()
expects the target object as the first parameter, then the parameters to the method call as the following parameters (since java 1.5, it's a varargs parameter)
您没有正确调用invoke():invoke()
期望目标对象作为第一个参数,然后方法调用的参数作为以下参数(从 java 1.5 开始,它是一个可变参数)
Try this:
试试这个:
try
{
List<String> value = new ArrayList<String>();
value.add("GB");
Class<?> clazz = Class.forName( "com.xxx.Currency" );
Object obj = clazz.newInstance();
// Since java 1.5, you don't need Class[] for params: it's a varargs now
Method method = clazz.getDeclaredMethod( "setCurrencyCode", List.class ); // you already have a reference to the class - no need for obj.getClass()
method.invoke( obj, value ); // invoke expects the target object, then the parameters
}
catch(Exception e)
{
System.out.println( "Exception : " + e.getMessage() );
}
}
回答by Andrzej Doyle
This means that the value
object you pass into invoke
is not an instance of the class on which the method
is defined. This is because the first argument of invoke is the object on which to makethe call, and the subsequent arguments are the parameters to the invoked method. (In this case it looks like value needs to be an instance of com.xxx.Currency
- which of course it isn't, because it's a List
.)
这意味着value
您传入的对象invoke
不是method
定义的类的实例。这是因为调用的第一个参数是要在其上的对象作出的呼叫,和随后的参数是参数被调用的方法。(在这种情况下,似乎 value 需要是一个实例com.xxx.Currency
- 当然不是,因为它是一个List
.)
Since you're calling a non-static method (and going to to trouble of creating a new instance), then for the reflective equivalent of obj.setCurrencyCode(value)
, at the end of your try block you'd need to call
由于您正在调用一个非静态方法(并且会遇到创建新实例的麻烦),那么对于反射等价物obj.setCurrencyCode(value)
,在您的 try 块的末尾,您需要调用
method.invoke(obj, value)
instead of your current single one-arg call.
而不是您当前的单个单参数调用。