1

我正在制作一种测试程序,以便能够覆盖我在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

谁能帮我?

它说“对象不是声明类的实例”......但是它指的是什么“对象”?

4

2 回答 2

2

在您的Start课程中,您正在调用该Component.override()方法:

//         v--- This is the Class Object
a.override(this.getClass(), "add", new Class<?>[]{Integer.class,Integer.class});

a类型在哪里Component。您正在传递它this.getClass(),它是一个 Class 对象Start。然后在override()

//                          v--- Class object gets passed along here
over.put(name,new Overrider(cl,name,param));

您正在创建一个 new Overrider,并将 Class 对象提供Start给构造函数,该构造函数将Class<?> source;字段设置为StartClass 对象。然后,当您调用该Overrider.call()方法时,它会执行以下操作:

//     v--- and finally invoked here
return source.getMethod(name, overs).invoke(this, param);

并传递invoke()athis这是 的实例Componentsource而是 Class 的对象Start。在这一行中,“source”和“this”需要是同一个类,但又Start不是Component

于 2012-08-15T20:41:59.940 回答
0

您的代码非常复杂,但这是我的帮助。如果你有这样的一行:

klass.getMethod(name).invoke(obj)

然后错误说这obj不是klass.

于 2012-08-15T20:37:05.713 回答