• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

calling methods with reflection

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because you are trying to invoke a method with argument using reflection , that doesnot expect any argument.
 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rahul,
To call invoke() , it expects two objects to pass.
Then how call.
 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rahul,
The invoke method expects two objects.
Then how call.
 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Ulf

this is doing fine.

Method[] theMethods=class.getDeclaredMethods();


Thanks:
Raj
 
reply
    Bookmark Topic Watch Topic
  • New Topic