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

我的梦境 提交于 2019-12-30 08:22:29

问题


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?


回答1:


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.




回答2:


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



回答3:


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




回答4:


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.




回答5:


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?



来源:https://stackoverflow.com/questions/10602696/is-there-a-way-to-list-all-calls-of-equals-of-a-certain-class-using-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!