I'm confronted with the following problem at the moment:
I have a certain class in which the equals() - Method is overridden. However I'm not sure if it is ever being used (either in my or in one of my collegues projects). Is there a way to find out? When I search for references, well, it just gives me ALL references to the Object equals() - Method (which are quite a few). There surely must be an easier way than scan through all of them...
Anyone got an idea?
I can't think of a method that would reliably do this purely through static analysis (I doubt one exists).
A practical method that might help is to set a breakpoint on the first line of the
equals()
method, and start the program in the Eclipse debugger. Whenever the method is called, Eclipse will break into the debugger. At this point you'll be able to examine the call stack to see who has called the method.To do this effectively you'd have to have some idea of which code paths can be expected to call the method. If you are unable to trigger the method, this doesn't prove that no one uses it.
What about modifying the overridden equals to something like :
Which give you the classic Object equals ? Or maybe a super.equals if you extends some class?
Seems java is broken in that respect. So you need to do it the hard way and hope to catch all occurrences.
Select method name in Eclipse -> Package explorer or Outline view and click F4. Alternatively you can select method -> right click -> Open Call Hierarchy
This will show all members calling this method in your workspace. Hope this helps
You're asking Eclipse to solve an impossible task.
To figure out if a particular overridden method is called or not is not statically decidable, which is why Eclipse over approximates the response.
Assume you have some field
Object o
and that you at some point doo.equals(...)
. To determine whether or noto
could ever refer to aYourClass
object requires that you determine the runtime type ofo
along every possible execution path, which simply can't be done statically.The reason why it is impossible is quite similar to why the compiler rejects the following code:
The best thing you can do is probably debug your program, either by setting a break point or by throwing for instance an UnsupportedOperationException from within the equals method.