Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue with calling non static method through Reflection

 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends ,

I am trying to code soemthing liek junit with annotations



When i remove static on t and t2 method , calling Util class has issue ... ( With static it works fine )
 
Virendrasinh Gohil
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course. See the signature of

It clearly says, you need to pass an Object to execute this method. How can you execute non-static method without an object? Construct an object of that class (of course using reflection) and pass as argument to m.invoke(obj); Or in current scenario, just pass this
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Gohil ,

When i invokek this .



I am getting following exception

Exception in thread "main" java.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 com.xxx.frmk.AbstractJunitFrmk.<init>(AbstractJunitFrmk.java:25)
at com.xxx.Util.<init>(Util.java:10)
at com.xxx.Util.main(Util.java:29)

Any Clue , Please respond
Thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JavaDocs:

Parameters:
obj - the object the underlying method is invoked from


It looks like you are probably calling invoke and passing the class, not an instance of that class.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul ,

Reflection code is in Super class and class which has annotated methods is in Util (derived ) ,So i am instantiating same class

infact this.getClass().getName() prints com.xxx.Util , This is the same class which i am instanting
 
Virendrasinh Gohil
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Gowtham wrote:Hi Paul ,

Reflection code is in Super class and class which has annotated methods is in Util (derived ) ,So i am instantiating same class

infact this.getClass().getName() prints com.xxx.Util , This is the same class which i am instanting


Hi Ganesh,

Try calling it using "this". I mean, pass "this" in method.invoke();

Sorry for late reply.

Viren.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool Gohil

Its resolved Issue

Thanks a lot .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic