File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Issue with calling non static method through Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Issue with calling non static method through Reflection" Watch "Issue with calling non static method through Reflection" New topic
Author

Issue with calling non static method through Reflection

Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

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 )


Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
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

Joined: Mar 30, 2005
Posts: 225

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

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

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

Joined: Jun 09, 2004
Posts: 46
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

Joined: Mar 30, 2005
Posts: 225

cool Gohil

Its resolved Issue

Thanks a lot .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue with calling non static method through Reflection
 
Similar Threads
Annotation Handler in Java
Generating RESTful web services dynamically
Annotations and reuse
Regarding Inherited Annotation
toString() method is not overridden when a reference to Annotation is output in println()