• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Problem with Reflection

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm trying to print out the methods of a class,
I have a simple java bean with two properties, say, "name" and "address" and corresponding getters/setters.
Then I initialize a class passing a reference to this object into its constructor and call a method that's supposed to loop over the bean's methods and print out their names... piece of cake.

The problem is that it prints out only the methods of the superclass Object not of the real runtime class.
Here's a snippet of my code:


I'm stuck, can you give me some help?
Thanks.
[ December 17, 2004: Message edited by: Lukas Alamar ]
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should work perfectly. Are you sure the object you're passing is is really an instance of your type, and not a plain Object -- or perhaps an array, which has only the methods of Object?
 
Lukas Alamar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, yes the object I'm passing is really an instance of my bean type,
and if I print out the name of the class in the constructor I get the right one but then I cannot see its menthods!


here's part of the output:

runtime class: class clo.batch.PropertyGeneratorTest$BeanData
Name: hashCode
Return Type: int
Parameter Types:
...it prints only the methodsd of the Object super class

could there be any other reasons for that?

 
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you running this? It seems to print out the Object methods last, so if your console doesn't have enough buffer space, then you might only see the Object methods, even though it printed out everything.

I tested this code, and it works:


and the results:
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class.getMethods() only finds public methods, but does include inherited methods. On the other hand, Class.getDeclaredMethods() finds all methods (private, package, protected...), but does not include inherited methods (you can get those from the superclass Class object, though).

Does this help at all?
 
Lukas Alamar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With class.getMethods() you can only see public methods??
Mine had default access ... :roll:
I dind't check the API spec for getMethods(), but I really couldn't see the point.

(you know when it's in front of you but you can't see it...)

Thanks.
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic