• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection  RSS feed

 
Anirban dutta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to get all the public methods of a class through reflection. The problem is that the class contains overridden methods of it's super-classes, which is not shown in the getDeclaredMethods(), and in the getMethods() I get even the methods which are not overridden by the class, just inherited from the super-class.
Please tell me how I can get all the public methods of a class, only which have some implementation, either by overriding a super-class method or defining it's own method.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
I think he wants to also have any public inherited methods that have not been overridden.
So you could add something like :

Michael Morris
 
Anirban dutta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Michael,
I want to do exactly the opposite of what you have done. Meaning I want to have all thos methods which are of the same class as well as overridden by it.(NOT the methods which are not overridden by the class)
Thanks
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anirban,

... Meaning I want to have all thos methods which are of the same class as well as overridden by it.

But if you take Jim's loop and my loop together will that not accomplish your purpose?
Perhaps, I am still misunderstanding what you want (that is very possible, my wife tells me such things all the time ) but take a look at this and see if it does what you want:

Here are the results I got:

That lists all publically available methods of Sub: inherited, overridden and declared.
Michael Morris
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Please tell me how I can get all the public methods of a class, only which have some implementation, either by overriding a super-class method or defining it's own method.

Bear with me, I can be somewhat dense at times. Do you want all public methods that are either declared in the class or overridden in the class? In other words everything except the purely inherited methods.
Michael Morris
 
Anirban dutta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Michael,
YES, YES, YES . Now you have got the point exactly right. Your code gives me the 3 things: inherited, overridden and declared. I just want two: overridden and declared.
Now that you have understood my point, please give a solution for it. I am trying for it since a long time.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try the code I provided? :roll:
 
Anirban dutta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Jim,
Yes your code was the first one which I tried out tfor solving this problem. But it didn't solve my problem. It gave me only the declared methods of the class, not the methods which are overridden by the class.
Tha's what exactly my problem is.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Michael's code for classes Super and Sub above, I get the following for methods of class Sub:

Class Sub overrides Super's doSomething() method, and it's listed here. Isn't that what you want? Is something missing?
To speak precisely, we're seeing a listing for the overriding method which is defined in Sub - not the overridden method defined in Super. Is that your objection? The method signatures are the same of course, and you can't invoke the overridden method using an instance of the overriding class - so I don't see what point there would be to finding the overridden method instead of the overriding method, if that's really what you want. (Which I doubt since it's completely different than what you described in your first post.) If you do want this - what would it be used for? How would you try to use it to do something useful?
[ March 17, 2003: Message edited by: Jim Yingst ]
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jim,
look at the quote that Anirban mentioned to Michael,


Your code gives me the 3 things: inherited, overridden and declared. I just want two: overridden and declared.


the 'inherited' word is removed from the three kind of methods which means..
if super class has a method that didn't get overriden and got INHERITED (if it were having proper access modifiers etc) then "WE DON'T WANT THAT METHOD TO BE LISTED", right Anirban?
thanks
maulin.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is what you want:

Here are the results:

Sub overrides int Super.doSomething(), overloads doSomethingElse() and declares the two doMyOwnThing() methods.
Michael Morris
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[maulin]: if super class has a method that didn't get overriden and got INHERITED (if it were having proper access modifiers etc) then "WE DON'T WANT THAT METHOD TO BE LISTED", right Anirban?
Right, and using getDeclaredMethods() you don't get any inherited methods unless they were re-declared (overridden) in the current class. Isn't this exactly what was requested? Except that getDeclaredMethods() gives you private, package, and protected methods as well - which is why I put in a loop to filter out any non-public methods.
Look again at Michael's code for Super and Sub. There are two methods in Super which are not overridden: doSomethingElse(int), and doOneMoreThing(). When you run the code I gave in my first post, you get the output shown in my last post - WHICH DOES NOT CONTAIN EITHER OF THESE INHERITED METHODS. So this is why I'm confused. Please, can anyone name a specific method which was not listed but should be, or which was listed and should not be?
Michael's latest code seems to give the same output that my earlier code did, though it uses a slightly different technique.
[ March 17, 2003: Message edited by: Jim Yingst ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim is right. Like I said earlier I can be pretty dense sometimes His way is much more direct that my last post. I guess I just got wrapped up in semantics. I just should've looked at his results.
Michael Morris
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well your other code made sense for other interpretations of the problem; I wasn't really sure I'd understood it originally either, but from subsequent posts from Anirban I don't see a compelling alternate interpretation. So I guess I may have lucked out with my initial interpretation. Or maybe we're both still "wrong", but at least we're on the same page.
 
Anirban dutta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
Sorry I could not participate in the discussion yesterday, as I had a very busy day.
Thanks to all of you for the support you have provided for my problem, and special thanks to Maulin who exactly got to the point which I wanted to explain. I think the last code posted should solve my problem. I will try it and get back to all of you ASAP.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[slowly banging forehead on table]
sigh...
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe we should require all posts to be in Latin (like prescriptions) so there could only be one interpretation. :roll:
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may be the binary is even better :-)
isnt it?
regards
maulin.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!