Is there a way to list all calls of equals() of a

2019-02-16 22:52发布

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?

5条回答
疯言疯语
2楼-- · 2019-02-16 23:10

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.

查看更多
三岁会撩人
3楼-- · 2019-02-16 23:14

What about modifying the overridden equals to something like :

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

Which give you the classic Object equals ? Or maybe a super.equals if you extends some class?

查看更多
forever°为你锁心
4楼-- · 2019-02-16 23:20

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
查看更多
不美不萌又怎样
5楼-- · 2019-02-16 23:24

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

查看更多
太酷不给撩
6楼-- · 2019-02-16 23:27

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.

查看更多
登录 后发表回答