• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issue with calling non static method through Reflection

 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 )
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool Gohil

Its resolved Issue

Thanks a lot .
 
reply
    Bookmark Topic Watch Topic
  • New Topic