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

I am currently facing the following problem:

I have a specific class in which the equals () method is overridden. However, I'm not sure if it is ever used (either in mine or in one of my colleagues' projects). Is there any way to find out? When I search for links, well, it just gives me ALL references to Object equals () - Method (of which there are quite a lot). Of course, there should be an easier way than scanning all of them ...

Anyone got an idea?

+7
source share
5 answers

You ask Eclipse to solve an impossible task.

To find out if a particular overridden method is called or not, it is not statically decidable, so Eclipse comes close to the answer.

Suppose you have an Object o field and that at some point you are doing o.equals(...) . To determine whether o can ever refer to a YourClass object, you need to determine the type of execution o for each possible execution path, which simply cannot be done statically.

The reason this is not possible is very similar to why the compiler rejects the following code:

 Object o = "hello"; System.out.println(o.length()); 

The best you can do is probably debug your program by setting a breakpoint or throwing, for example, an UnsupportedOperationException from the equals method.

+5
source

I can’t come up with a method that reliably does this purely through static analysis (I doubt that it exists).

A practical method that can help is to set a breakpoint on the first line of the equals() method and run the program in the Eclipse debugger. Whenever a method is called, Eclipse will go into the debugger. At this point, you can check the call stack to see who called this method.

To do this efficiently, you will need to have an idea of ​​what code paths you can call to invoke a method. If you cannot call a method, this does not prove that no one is using it.

+1
source

Select a method name in Eclipse β†’ Package explorer or Outline view and press F4. Alternatively, you can choose a method β†’ ​​right-click β†’ Open Call Hierarchy
This will show all members calling this method in your workspace. Hope this helps

+1
source

How about changing the overridden value:

 log.debug // Whatever you want return this == obj; 

What gives you a classic object equal? Or maybe super.equals if you extend some class?

+1
source

It seems that java is broken in this regard. So you need to make it the hard way and hope to catch all the cases.

  • Create a copy of your class
  • Delete old class
  • Fix all compiler errors by renaming them one by one.
  • Look at cases when a class is used as a key for cards
  • Look at the class collections (indexOf (), contains ())
  • Look for equals () or hashCode () class calls
  • I hope you haven’t missed anything.
+1
source

All Articles