[英]Argument type mismatch: IllegalArgumentException on simple case
我有一个关于IllegalArgumentException的著名问题,我不知道为什么。 这是我的课:
public class DataMapper {
... An lot of others methods that does not have the same name (because I created a method specifically for checking this exception
private void hello(String ahem) {
logger.info("Hey !");
}
}
在我的测试案例(尝试调用我的方法)中:
@Test
public void test() {
Class<?> targetClass = DataMapper.class;
try {
Object t = targetClass.newInstance();
Class<?>[] cArg = new Class[1];
cArg[0] = String.class;
Method method = targetClass.getDeclaredMethod("hello", cArg);
method.setAccessible(true);
method.invoke(t, cArg);
} catch(NoSuchMethodException e) {
logger.error("Name does not exist : ", e);
Assert.fail();
} catch (Exception e) {
logger.error("It is broken : ", e);
Assert.fail();
}
}
它始终位于IllegalArgumentException上。 Method
对象听起来符合我的期望,例如:
任何想法在那里发生了什么? 在任何重复标记之前,我已经检查过这些标记,没有什么完全一样,也没有用:
这是一个方法列表,实际上他有两个或更多个具有相同名称但参数不同的方法。 第二个是错误的,因为他正在传递String []作为参数,并且编译器为完整的参数列表误解了该对象。 第三个是因为他忘了通过辩论。
在这一行:
method.invoke(t, cArg);
您必须传递String
而不是cArg
,而cArg
是Class
的数组。 类似于以下内容的方法将起作用:
method.invoke(t, "Test");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.