Find all calls to method in java classes

旧时模样 提交于 2019-12-12 15:15:55

问题


I've a huge project with many classes. I've a very specific class; let's name it SuperFoo. I need to find all calls to the method equals() with argument of type Superfoo. Hope it's clear.

So, one more time... in thousands of java files (or bytecode?) I'd like to find all calls to the method java.lang.Object.equals(Object arg) but the argument to this call must be of type SuperFoo. For example:

public void doItWith(SuperFoo foo) {
    if (otherFoo.equals(foo)){
         // do something
    }
...
}

I checked out Browse-by-query, analyzing bytecode and just Java Search in Eclipse and in my opinion none of this works.


回答1:


A programmatic approach would be to use Aspect Oriented Programming (i.e. AspectJ). You would define a pointcut to capture the method call of interest

pointcut equals(Superfoo o) = call(boolean *.equals(Object)) && args(o);

and then use advice to select each occurrence and query the joinpoint object to get the static information i.e. where it appears in your code.

before(Superfoo o) : equals(o) {
  System.out.println("An occurence at "+thisJoinPoint.getSourceLocation());
}



回答2:


So, you are trying to find out the references of

public boolean equals(Object o) 

overridden in SuperFoo. If you have access to the source code, this could be done in eclipse. If you go for Call Hierarchy, eclipse returns back all the occurrence of Object's equals() method. So, you need try the following:

Open SuperFoor. Select the equals method. Right Click. References --> Project. In the resulting search window, on the extreme right, click on the triangle. Select "references to override". That's it. The results returned initially will be filtered and it will mostly contain references of equals overridden in SuperFoo.

Hope this helps.



来源:https://stackoverflow.com/questions/16996988/find-all-calls-to-method-in-java-classes

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