wood burning stoves 2.0*
The moose likes Java in General and the fly likes calling methods with 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 "calling methods with reflection" Watch "calling methods with reflection" New topic
Author

calling methods with reflection

raj baig
Ranch Hand

Joined: Jul 11, 2006
Posts: 96
hi,
i have a class with two methods

public void metOne(){
System.out.println(" Hello ");
}

public String metTwo(){
return " Hai ";
}

in another class i am invoking the two methods



It is giving me exception:

java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at TestReflect.showMethods(TestReflect.java:22)
at TestReflect.main(TestReflect.java:7)


can you tell me the mistake.

Thanks in advance,
raj.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
This is because you are trying to invoke a method with argument using reflection , that doesnot expect any argument.


Rahul Bhattacharjee
LinkedIn - Blog
raj baig
Ranch Hand

Joined: Jul 11, 2006
Posts: 96
Hi Rahul,
To call invoke() , it expects two objects to pass.
Then how call.
raj baig
Ranch Hand

Joined: Jul 11, 2006
Posts: 96
Hi Rahul,
The invoke method expects two objects.
Then how call.
raj baig
Ranch Hand

Joined: Jul 11, 2006
Posts: 96
HI All,
How can i call only user defined methods only not the Object class methods.
when i call invoke method it is calling my class methods as well Object
class methods.
How can i restrict Object class methods.

Thank you for your reply.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41029
    
  43
I'm not quite sure what you're asking, but try this:



This only works for methods that have no parameters, like the two you defined, not for some of the ones defined in Object.


Ping & DNS - my free Android networking tools app
raj baig
Ranch Hand

Joined: Jul 11, 2006
Posts: 96
hi ulf,
i dis same as you said. It is calling my methods + Object class methods.
But i don't want the object class methods to execute. How can i restrict.

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41029
    
  43
But i don't want the object class methods to execute.

Your code iterates through all methods; if that's not what you want, then don't do it

The Method class has the getDeclaringClass method which will tell you for any method the class in which it is declared. If that's not your own class (i.e., the method was inherited), then you can skip it. At least that's how I read the javadocs, I've never used the method.
raj baig
Ranch Hand

Joined: Jul 11, 2006
Posts: 96
HI Ulf

this is doing fine.

Method[] theMethods=class.getDeclaredMethods();


Thanks:
Raj
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling methods with reflection
 
Similar Threads
Cloning
Java Bean help
Problem with Reflection
Reflection in JSP
TreeMap containsKey issue