invoke问题,调用时总说java.lang.IllegalArgumentException: object is not an instance of declaring class
为了省事,我用反射功能循环一个类的所有set方法,给每个类中的属性赋值。但得到set方法后用invoke后出现java.lang.IllegalArgumentException: object is not an instance of declaring class
例如类Atest中有一个方法
public void settestStr(java.lang.String str) {
this.testStr = str;
}
我在类Btest中让类Atest的这个方法invoke。
Class[] c=new Class[1];
c[0]="".getClass();
java.lang.reflect.Method m=Atest.getClass().getDeclaredMethod("settestStr",c);
Object b=m.invoke("test",null);//这里出错
错误堆栈:
java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
请问这是为什么?