我正在寻找一种从单独的主程序中提取调用printc(String)
方法的所有方法的方法。
在IntelliJ IDE中,我可以通过方法void printc(String txt)
并单击Ctrl+Alt+H
来获得预期的结果,如下所示:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9wcjNjMi5wbmcifQ==” alt =“给定方法的调用者]] [[Groovy-java project”>]
但是我想在自己的程序中实现这种功能(不使用IDE)
例如:
package com.controller;
import com.mgr.ServiceBean;
import com.ser.B;
**//Java file**
public class A extends ServiceBean {
void a() {
}
void a1() {
super.printc("calle of c");
}
void a2(B boo) {
super.printc("calle of c");
}
}
======
package com.ser
import com.mgr.C
import com.mgr.ServiceBean
**//Groovy file**
public class B extends ServiceBean {
void b1() {
}
void b2() {
super.printc("calle of c")
}
}
=====
package com.mgr
**//Groovy file**
trait C
{
String test() {
return "test"
}
boolean printc(String methodName) {
if(methodName!=null)
return true
false
}
}
=========
package com.mgr
**//Groovy file**
class ServiceBean implements C {
}
============
//Need logic which accepts
class Main{
public static void main(String args[]){
Main m=new Main();
m.FindUsage("C","void printc(String)")
}
Collection FindUsage(String ClassName, String methodname){
// XXX Logic missing
return **“[A.a1(),A.a2(Integer),B.b2()]”**
}
}
[NOTE:由于A,B,C类是完全不同的src,并且我没有运行它们,所以我无法使用堆栈跟踪来获取结果,main是一个单独的程序。
How can I find all the methods that call a given method in Java?->不包括Groovy类的调用者
我认为您正在寻找查找用法功能,请看一下,它可以为您提供帮助https://www.jetbrains.com/help/idea/find-usages-method-options.html
如果所有可能属于所需调用层次结构的类都在运行时可用,那么您可以遍历每个类及其方法,并创建一个拓扑排序图,从中可以遍历遍历调用层次结构。有关方法。