[英]code to get all callers of a method of a Groovy-Java project
I am looking for a way to extract all the methods calling printc(String)
method from a separate main program.我正在寻找一种方法来从单独的主程序中提取所有调用
printc(String)
方法的方法。
In IntelliJ IDE I can do so by go to the method void printc(String txt)
and click Ctrl+Alt+H
to get the expected results like this:在 IntelliJ IDE 中,我可以转到方法
void printc(String txt)
并单击Ctrl+Alt+H
以获得如下预期结果:
)
)
but I would like to implement such functionality in my own program (without use of an IDE)但我想在我自己的程序中实现这样的功能(不使用 IDE)
For example:例如:
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 : i can't use stack trace to get the results as the A, B, C classes are completely different src and I am not running them, main is a separate program.注意:我不能使用堆栈跟踪来获得结果,因为 A、B、C 类是完全不同的 src,我没有运行它们,main 是一个单独的程序。
How can I find all the methods that call a given method in Java? 如何在 Java 中找到调用给定方法的所有方法? --> doesn't include caller of Groovy classes
--> 不包括 Groovy 类的调用者
我觉得你找个find使用功能,看看这个,可以帮到你https://www.jetbrains.com/help/idea/find-usages-method-options.html
如果可能出现在所需调用层次结构中的所有类在运行时都可用,那么您可以遍历每个类及其方法并创建一个拓扑排序图,从中您可以从相关方法遍历调用层次结构.
How can I find all the methods that call a given method in Java? 如何在 Java 中找到调用给定方法的所有方法? provides code for byte code scanning but it fails for the Groovy files, I end up doing a mixture of by reading each and every line in my source code as a string and then parsing them along with ASM bytecode analysis to extract the needed information of java files.
提供了用于字节码扫描的代码,但它对 Groovy 文件失败了,我最终通过将源代码中的每一行作为字符串读取,然后将它们与 ASM 字节码分析一起解析以提取所需的 java 信息文件。 Note: ASM doesn't work for the groovy files as byte code for java file and groovy file are different, in Groovy file you get a larger .class file compared to java's
注意:ASM 不适用于 groovy 文件,因为 java 文件和 groovy 文件的字节码不同,在 Groovy 文件中,与 java 相比,您获得更大的 .class 文件
Sample code:示例代码:
//stringValue =entire text
//charopen example: { or (
//char close: } or )
private String getFullMethod(String stringValue, String chartOpen, String charClose) {
String methodName = ''
int breaceStackIn = 0
boolean read = true, readOpen = true
if (stringValue != null) {
stringValue.eachWithIndex { it, index ->
if (it.equalsIgnoreCase(chartOpen) && readOpen) {
breaceStackIn += 1
read = true
} else if (it.equalsIgnoreCase(charClose) && readOpen) {
breaceStackIn -= 1
methodName += charClose
read = false
}
if (read && readOpen) {
methodName += it
}
if (breaceStackIn == 0 && read == false && readOpen) {
readOpen = false
}
}
}
methodName
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.