Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to list all calls of equals() of a certain class using Eclipse?

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?

like image 449
Harry the Holtzman Avatar asked May 15 '12 14:05

Harry the Holtzman


3 Answers

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.

like image 90
aioobe Avatar answered Oct 19 '22 11:10

aioobe


Seems java is broken in that respect. So you need to do it the hard way and hope to catch all occurrences.

  • Create a copy of your class
  • Delete the old class
  • Fix all compiler-errors by renaming one by one.
  • Look for occurrences where the class is used as key for maps
  • Look for collections of the class ( indexOf(), contains() )
  • Look for calls to equals() or hashCode() of the class
  • Hope you didnt miss something
like image 34
Alex Avatar answered Oct 19 '22 12:10

Alex


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.

like image 2
NPE Avatar answered Oct 19 '22 12:10

NPE