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?
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 do o.equals(...)
. To determine whether or not o
could ever refer to a YourClass
object requires that you determine the runtime type of o
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:
Object o = "hello";
System.out.println(o.length());
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.
Seems java is broken in that respect. So you need to do it the hard way and hope to catch all occurrences.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With