【发布时间】:2019-12-23 15:48:23
【问题描述】:
我正在寻找一种方法来从单独的主程序中提取所有调用printc(String) 方法的方法。
在 IntelliJ IDE 中,我可以通过转到方法 void printc(String txt) 并单击 Ctrl+Alt+H 来获得预期结果,如下所示:
)
但我想在我自己的程序中实现这样的功能 (不使用 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()]”**
}
}
注意:我不能使用堆栈跟踪来获取结果,因为 A、B、C 类是完全不同的 src,我没有运行它们,主要是一个单独的程序。
How can I find all the methods that call a given method in Java? --> 不包括 Groovy 类的调用者
【问题讨论】:
-
看看这个,我相信这回答了你的问题。 stackoverflow.com/questions/2650844/…
-
正如我所提到的,我正在寻找一个单独的主程序。我不能使用 StackTrace,因为它不在同一个程序中,而且我没有运行 A、B、C 类。
-
所以你只想要一个代码分析器?您是否检查过 codenarc 是否可以帮助您?
标签: java intellij-idea groovy code-analysis static-code-analysis