简体   繁体   English

IllegalArgumentException:参数数量错误

[英]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.

相关问题 IllegalArgumentException:参数数量错误 - IllegalArgumentException: wrong number of arguments JUnit 参数化测试 - IllegalArgumentException:arguments 的错误编号 - JUnit Parameterized Tests - IllegalArgumentException: wrong number of arguments 调用引发IllegalArgumentException:参数数量错误 - invoke throws IllegalArgumentException: wrong number of arguments IllegalArgumentException:Java Constructor.newInstance() 中的参数数量错误 - IllegalArgumentException: wrong number of arguments in Java Constructor.newInstance() method.invoke生成IllegalArgumentException:参数数量错误 - method.invoke generates IllegalArgumentException:wrong number of arguments Java java.lang.IllegalArgumentException:参数数量错误 - Java java.lang.IllegalArgumentException: wrong number of arguments 反射错误:java.lang.IllegalArgumentException:参数数量错误 - Reflections error: java.lang.IllegalArgumentException: wrong number of arguments java.lang.IllegalArgumentException:参数个数错误调用构造函数时发生异常 - java.lang.IllegalArgumentException: wrong number of arguments Exception while invoking constructor 获取java.lang.IllegalArgumentException:以下代码段的参数数量错误 - Getting java.lang.IllegalArgumentException: wrong number of arguments error for below code snippet TestNG工厂,DataProvider和构造函数上的valargs导致“ java.lang.IllegalArgumentException:参数数量错误” 2 - TestNG Factory, DataProvider, and valargs on constructor is causing “java.lang.IllegalArgumentException: wrong number of arguments”2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM