• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection : get what exactly method tries to override  RSS feed

 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All.

I`m fighting through homework about reflections and i have a task to identify , if method oveerides something and if yes, display waht exactly is being overriden.
I thought it would be quite straight forward, untill i found that @override does not get into runtime and i cannot get it from reflection .
Any idea how this task is regularly done ?

Thank you.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Gurianov wrote:
I thought it would be quite straight forward, untill i found that @override does not get into runtime and i cannot get it from reflection .


This answer is only provided because you mentioned "reflection". IMHO, when the reflection API is considered, I would think about (or to check) a possible flaw in the application design (to warrant the need to use the API).

The Class class (ie. reflection) has the getDeclareMethods() method to see what methods are actually declared in the class. If it is not found, then it has been inherited, and you can use the getSuperclass() method (and the getDeclareMethods() method again), to move up the hierarchy and find where the method has been inherited from.

Henry
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Henry.

I think i got it. If metod is overriden, there is no annotation at run time. SO i think one who created this task , wanted student to write annotation processor and cehcked source.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Gurianov wrote:thanks Henry.

I think i got it. If metod is overriden, there is no annotation at run time. SO i think one who created this task , wanted student to write annotation processor and cehcked source.


Checking the @Override annotation is the wrong way to go (even if it was available at runtime). The annotation is optional, so you can't rely on it being present on all overridden methods. You definitely shouldn't be writing a source parser to solve this problem.

A method is overridden if it has exactly the same signature as a method in a superclass (not including any Interfaces, which are also available by reflection). So the solution would be to walk the hierarchy of all superclasses in the inheritance tree and look for one that as the same signature as the method you are checking.


Edit: Just checked in Eclipse and it is actually adding @Override to implementations of Interface methods, so I was wrong. These need including too.
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THanks Mike.

However i`m still stuck on this.
I get only methods inherited from Object , if i call
Looks like i`m missing something , so i put it away for a while.
Plus Eclipse glitches heavily with autocompletion after classReflection.getSuperclass().
It either break whole autocompletion , i.e. nothing happens when you press Ctrl+Space , or (latest version) display dialog with some items names as "Error getting text".
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What methods were you expecting to get? I can't see your code so have no idea what the inheritance structure looks like.

If your class does not explicitly extend another class then its superclass will be Object, so you would only expect to see Object's methods when calling getSuperclass().getDeclaredMethods().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!