我正在尝试调用一个方法并返回值;但是,我得到一个 IllegalArgumentException: wrong number of arguments
这是示例代码:
public class MyObjAnnoParser {
public void parse(Class clazz, Object obj) throws ClassNotFoundException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException, InstantiationException{
WatchLogAnno wlAnno= method.getAnnotation(WatchLogAnno.class);
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(WatchLogAnno.class)) {
String info = wlAnno.parentClass();
Class cls = Class.forName(info);
//error occurs here -- not sure what it means by wrong number
//the obj is wrapped as an Object array as can be seen....
Object objVal= method.invoke(cls.newInstance(), new Object[]{obj});
System.out.println(objVal);
}
}
}
}
注释类:
@Target(ElementType.METHOD )
@Retention(RetentionPolicy.RUNTIME)
public @interface WatchLogAnno {
String parentClass() default "";
}
MyObj 类:
public class MyObj {
private String summary;
@WatchLogAnno(parentClass = "com.stuff.MyObj")
public String getSummary(){
return summary;
}
public void setSummary(String summary){
this.summary = summary;
}
}
调用解析器的测试类:
public class MyObjAnnoParserTest {
public static void main(String [] args) throws Exception {
MyObjAnnoParser parser = new MyObjAnnoParser ();
parser.parse(Annotated.class);
MyObj myObj = new MyObj();
myObj.setSummary("Testing an entry for this piece");
parser.parse(myObj.class, myObj );
}
}
因此,正如我在上面的评论中列出的那样,当我调用 invoke.method 时,它会抛出 IllegalArgumentException...。
我确信这是一个简单的错误...感谢您的帮助...谢谢!