1

I'm making a kind of test program to be able to override methods in classes for an api I'm making in java but I'm getting a weird error when trying to invoke a method from another class...

Here is the main "component class":

 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;
}
 }

And this is the main class:

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;
}

 }

I'm getting this error when I start the program:

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

Can anyone help me?

it says "object is not an instance of declaring class"... But what "object" is it refering to?

1
  • Whatever object you're trying to invoke a method on. Step through your code with a debugger.
    – Matt Ball
    Aug 15, 2012 at 20:34

2 Answers 2

2

In your Start class, you are calling the Component.override() method:

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

Where a is of type Component. You are passing it this.getClass(), which is a Class object for Start. Then in override():

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

You are creating a new Overrider, and giving the Class object for Start to the constructor, which sets the Class<?> source; field to the Start Class object. Then when you call the Overrider.call() method, it does this:

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

And passes invoke() a this which is an instance of Component, while source is a Class object for Start. In this line, "source" and "this" need to be the same class, but Start and Component aren't.

5
  • if your saying that I should pass 'source' as a parameter to the invoke method then it causes the same error as always...
    – Fred
    Aug 15, 2012 at 20:46
  • @Fred I'm saying because they are different, you are getting that error. Obviously, your source, which is a Class object for Start, doesn't have an add() method, so you can't invoke Start.add(), however, since the class Component has an add() method, the source should be a Component.Class.
    – Jon Lin
    Aug 15, 2012 at 20:51
  • Start does have an add method (at the bottom) which should override the add method in Component...
    – Fred
    Aug 15, 2012 at 20:53
  • 1
    @Fred then are you sure when you call source.getMethod(name, overs).invoke(this, param); that you are replacing this with an instance of Start? source is an Object of type Class, and this needs to be an instance of that type.
    – Jon Lin
    Aug 15, 2012 at 20:56
  • I have no idea how I missed that! Thanks for the help... I've been messing with this for over 3 hours today!
    – Fred
    Aug 15, 2012 at 20:58
0

Your code is quite convoluted, but this my help. If you have a line like this:

klass.getMethod(name).invoke(obj)

then the error says that obj is not an instance of klass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.