[英]IllegalArgumentException:wrong number of arguments
I got IllegalArgumentException: wrong number of arguments
when I call method.invoke() and I am using EasyMock. 我收到
IllegalArgumentException: wrong number of arguments
当我调用method.invoke()且使用EasyMock时IllegalArgumentException: wrong number of arguments
。 In my code invoke()
has only one argument. 在我的代码中,
invoke()
只有一个参数。
Code: 码:
if (applicationContext != null
&& applicationContext.containsBean(className)) {
Object obj = applicationContext.getBean(className);
String temp[] = signature.split(",");
Object[] arguments = new Object[temp.length];
Class[] parameterTypes = new Class[temp.length];
for (int i = 0; i < temp.length; i++) {
if(temp[i] != null && !temp[i].isEmpty()) {
Class cls = Class.forName(temp[i]);
parameterTypes[i] = cls;
if (temp[i].startsWith("java.lang.")) {
arguments[i] = body[i];
} else {
try {
arguments[i] = mapper.readValue(body[i], cls);
} catch (Exception e) {
// e.printStackTrace();
arguments[i] = body[i];
}
}
}
}
Method m = null;
if(null !=signature && !signature.isEmpty()) {
m = obj.getClass().getMethod(method, parameterTypes);
} else {
m = obj.getClass().getMethod(method);
}
Object response = m.invoke(obj);
return response;
} else {
throw new Exception("ApplicationContext not properly set");
}
EasyMock Test : EasyMock测试:
Object obj = new Object();
String[] body ={ "body" };
EasyMock.expect(applicationContext.containsBean("Object")).andReturn(true);
EasyMock.expect(applicationContext.getBean("Object")).andReturn(obj);
EasyMock.replay(applicationContext);
moduleInvocation.invokeService("Object", "equals", "java.lang.Object", "responseType",body );
EasyMock.verify(applicationContext);
You never pass the arguments
array into Method#invoke
. 您永远不会将
arguments
数组传递给Method#invoke
。 Your line 你的线
Object response = m.invoke(obj);
should be 应该
Object response = m.invoke(obj, arguments);
You need to pass arguments variable in invoke method as per the default signature Method#invoke(obj,args). 您需要根据默认签名Method#invoke(obj,args)在invoke方法中传递arguments变量。 Please see below link :
请看下面的链接:
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/reflect/Method.java#Method.invoke%28java.lang.Object%2Cjava.lang.Object%5B%5D%29 http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/reflect/Method.java#Method.invoke%28java.lang.Object%2Cjava。 lang.Object%5B%5D%29
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.