生成Java项目中的方法调用图

huangapple go评论82阅读模式
英文:

Generating Method Call Diagram in Java Project

问题

我有一个Java项目的源代码,我想要追踪每个方法,在每个方法内部调用了哪些方法。

示例:

foo(){}
bar(){}
bar2(){
   bar();
}
foo2(){
   bar();
   bar2();
}

main(){
   foo2();
}

结果将会是这样的:

main
|_foo2
  |_bar
  |_bar2
    |_bar

任何附加组件或插件都可以,由于这是公司内部项目,我希望工具是离线的。至于结果,不必像那样,图形或其他任何形式都可以。

添加一些信息:

  1. 我不能编辑代码。
  2. 我不能运行代码。
  3. 但(也许)我可以将它们添加到Eclipse IDE中。
  4. 该项目可能是WAR、EAR或JAR类型的。
英文:

I have a source code of a java Project and i want to trace for every method, what method called inside that method.

example:

foo(){}
bar(){}
bar2(){
   bar();
}
foo2(){
   bar();
   bar2();
}

main(){
   foo2();
}

the result will be like:

main
|_foo2
  |_bar
  |_bar2
    |_bar

any add-ons or plug-ins is okay and since it is an internal project of a company, i hope the tools is offline. For the result, it doesn't have to be like that, graph or anything is okay.

Adding some information:

  1. I cannot edit the code
  2. I cannot run the code
  3. but (maybe) i can add them to eclipse IDE
  4. The project is either in WAR, EAR, or JAR type

答案1

得分: 1

首先,如果您正在使用Eclipse作为您的集成开发环境(IDE),那么您可以右键单击一个方法,然后选择“打开调用层次结构”,然后Eclipse会显示调用所选方法的所有方法,以及从所选方法调用的所有方法。

其次,我们可以通过编程来找到它。

也就是说,通过使用Eclipse JDT内部类,这些类与Eclipse用于显示调用方法的类相同。

首先,我们需要导入以下两个类:

import org.eclipse.jdt.internal.corext.callhierarchy.CallHierarchy;
import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;

然后创建一个方法来获取调用者,并将所选方法作为输入传入(请注意,它将是您的API类型方法)。您应该从程序中调用此方法,以查找哪些方法调用了什么方法。

英文:

Firstly, If you are use Eclipse as your IDE, then you can right click on a method and choose "open call hierarchy"
then eclipse shows all the methods that call the selected method and all the methods that are called from the selected method .

Secondly We can find it by programming.

ie, by using the eclipse JDT internal classes, these are the same classes used by eclipse to show whose calling the method.

initially we need to importing below two classes,

    import org.eclipse.jdt.internal.corext.callhierarchy.CallHierarchy;

    import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;

And then Create a method to get callers, and that takes the selected method as an input.(please note it will be your API type method) you should call this method from your program for find which methods call what..

答案2

得分: 0

如果这是一个纯粹的Java项目,没有其他框架,实现这一目标的一种方法是使用AspectJ来拦截每个方法,并在Thread local map中为方法名称和计数器加1的条目。如果main为0,foo2为1,barbar2将为2,依此类推。
详见此部分的AspectJ文档。

英文:

If it’s a pure Java project with no other framework, one way to do this would be to use AspectJ to intercept every method, and put an entry in Thread local map for method name to counter + 1. If main is 0, foo2 would be 1, bar and bar2 would be 2, and so on.
See this section of AspectJ docs.

答案3

得分: 0

  • 你只需要使用 ajc 编译器,而不是 javac。这使你能够使用 AspectJ。使用切面,你可以定义 Pointcut 并记录你需要的内容。
  • 我认为另一个选择可能是在所有调用之后分析堆栈跟踪。
英文:
  • All you need is to use ajc compiler instead of javac. It makes you able to use AspectJ. Using aspects you can define Pointcut and log what you need.
  • Another option I think, could be analyze a stacktrase after all calls.

答案4

得分: 0

你可以使用类似于IntelliJ的Code Iris插件。

英文:

You can use a Plugin like Code Iris from IntelliJ

huangapple
  • 本文由 发表于 2020年9月12日 14:16:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63857437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定