我正在制作一种测试程序,以便能够覆盖我在java中制作的api的类中的方法,但是在尝试从另一个类调用方法时出现奇怪的错误......
这是主要的“组件类”:
package st.cmp;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Component {
public class Overrider{
Class<?> source;
Class<?>[] overs;
String name;
public Overrider(Class<?> s,String n,Class<?>[] o){
source=s;
overs=o;
name=n;
}
public Object call(Object[] param){
try {
return source.getMethod(name, overs).invoke(this, param);
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
};
public HashMap<String,Component> cmps;
public HashMap<String,Overrider> over;
public Component(){
cmps=new HashMap<String,Component>();
over=new HashMap<String, Overrider>();
}
public void registerComponent(String nm,Component cm){
cmps.put(nm,cm);
}
public Component getComponent(String nm){
return cmps.get(nm);
}
public void override(Class<?> cl,String name,Class<?>[] param){
over.put(name,new Overrider(cl,name,param));
}
public Object call(String mnm,Object[] a){
Overrider ov=over.get(mnm);
if(ov!=null){
ov.call(a);
}
Class<?>[] abc=new Class<?>[a.length];
for(int i=0;i<a.length;i++){
abc[i]=a[i].getClass();
}
try {
return this.getClass().getDeclaredMethod(mnm, abc).invoke(this,a);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException
| SecurityException e) {
// TODO Auto-generated catch block
try {
this.getClass().getDeclaredMethod(mnm, abc).invoke(this,a);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException
| SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return null;
}
public void test(String a){
System.out.print(a);
}
public int add(Integer a,Integer b){
return a+b;
}
}
这是主要课程:
package st;
import st.cmp.Component;
public class Start {
public static void main(String[] args)
{
new Start().start();
}
public void start(){
Component a=new Component();
a.call("test",new Object[]{a.call("add",new Object[]{1,5}).toString()});
a.override(this.getClass(), "add", new Class<?>[]{Integer.class,Integer.class});
a.call("test",new Object[]{a.call("add",new Object[]{1,5}).toString()});
}
public int add(Integer a,Integer b){
return a*b;
}
}
启动程序时出现此错误:
6java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at st.cmp.Component$Overrider.call(Component.java:22)
at st.cmp.Component.call(Component.java:64)
at st.Start.start(Start.java:16)
at st.Start.main(Start.java:8)
6
谁能帮我?
它说“对象不是声明类的实例”......但是它指的是什么“对象”?