Note that there are some explanatory texts on larger screens.

plurals
  1. POJava reflection method invoking error
    text
    copied!<p>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...</p> <p>Here is the main "component class":</p> <pre><code> 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&lt;?&gt; source; Class&lt;?&gt;[] overs; String name; public Overrider(Class&lt;?&gt; s,String n,Class&lt;?&gt;[] 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&lt;String,Component&gt; cmps; public HashMap&lt;String,Overrider&gt; over; public Component(){ cmps=new HashMap&lt;String,Component&gt;(); over=new HashMap&lt;String, Overrider&gt;(); } public void registerComponent(String nm,Component cm){ cmps.put(nm,cm); } public Component getComponent(String nm){ return cmps.get(nm); } public void override(Class&lt;?&gt; cl,String name,Class&lt;?&gt;[] 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&lt;?&gt;[] abc=new Class&lt;?&gt;[a.length]; for(int i=0;i&lt;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; } } </code></pre> <p>And this is the main class:</p> <pre><code>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&lt;?&gt;[]{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; } } </code></pre> <p>I'm getting this error when I start the program:</p> <pre><code>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 </code></pre> <p>Can anyone help me?</p> <p>it says "object is not an instance of declaring class"... But what "object" is it refering to?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload