如何生成Java调用图,基于Eclipse的解决方案
我想分析和理解某个 Java 应用程序,我认为调用图会非常有用。我如何生成一个?我正在使用 Eclipse。
I'd like to analyze and understand a certain Java app and I think a call graph would be very useful. How do I generate one? I'm using Eclipse.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
获取调用堆栈
1) 如果您可以调试应用程序,只需放置一个断点(双击代码的左边距)并等待它停止即可。如果您不在调试视角,请转到“调试视角”,然后打开“调用堆栈视图/面板”。它有调用堆栈:)
2) 如果您想在某处打印此堆栈跟踪,请使用异常:
或
获取方法引用
您可以通过右键单击“引用”、“工作区”来获取对方法的所有引用。它将搜索当前打开的项目中的所有调用。非常非常有用。
分析应用程序
(感谢那些回答分析器选项的人)
Eclipse TPTP 提供分析:
http://www.eclipse.org/tptp/home/project_info/general/whatisTPTP.php
Getting callstack
1) If you can debug the application simply put a breakpoint (double click over the left margin of the code) and wait it to stop. Go to Debug Perspective if you're not there, and open the Call stack View/Panel. It has the call stack :)
2) If you want to print this stack trace somewhere use an Exception:
or
Obtaining method references
You can obtain all references to a method by right-clicking, References, Workspace. It will search all callings in your current open projects. Very very useful.
Profiling an app
(thanks those who had answered the profiler option)
Eclipse TPTP provides profiling:
http://www.eclipse.org/tptp/home/project_info/general/whatisTPTP.php
我遇到了完全相同的问题,所以我编写了 java-callgraph 工具套件。使用它,您可以创建动态(运行时)调用图和静态调用图,前提是您拥有程序及其依赖项的 jar 文件。
I had exactly the same problem, so I 've written the java-callgraph suite of tools. Using it, you can create both dynamic (runtime) call graphs and static call graphs, provided that you have the program's and its dependencies' jar files.
Netbeans profiler 对此非常有用!
您还可以使用 jconsole 命令(jdk 的一部分)
Netbeans profiler is very good for this !!
You can also use jconsole command (part of the jdk)
使用 Eclipse Profiler 可能会满足您的需求。
Using the Eclipse Profiler might get you what you want.
快速而肮脏,创建一个新的异常并打印堆栈跟踪。
Fast and dirty, create a new exception and print the stacktrace.
您无法在 Eclipse 中获得整个代码的图表。但是,您可以获得方法的调用者或被调用者的树视图。
在源代码中,右键单击感兴趣的方法的名称,例如main(String[] args),然后单击“Open Call Hierarchy”。 (Windows 上为 Ctrl+Alt+H)
这将打开调用者层次结构 (Caller Hierarchy) 的树视图。有一个选项可以查看被调用者层次结构。
要复制到文本文件,请右键单击调用层次结构视图中的节点,然后单击“复制扩展层次结构”。
缺点:
You cannot get a graph for the entire code in eclipse. However, you can get a tree view of callers or callees for a method.
In the source code, right-click on the name of a method of interest, say main(String[] args), then click on Open Call Hierarchy. (Ctrl+Alt+H on Windows)
This will open a tree view of the hierarchy of callers (Caller Hierarchy). There is an option to view the Callee Hierarchy.
To copy to a text file, right-click on a node in the Call Hierarchy view and click on Copy Expanded Hierarchy.
Drawbacks:
我有时使用 Eclipse 分析器。
I use the Eclipse profiler sometimes.
Thread.dumpStack() 允许您输出当前调用堆栈,而无需引发异常或使用调试器。这将输出到控制台。
您可以在代码的“热点”中调用 dumpStack() 来获取应用程序到达该点所需的调用堆栈。
Thread.dumpStack() allows you to output the current call stack without throwing an exception or using a debugger. This will be output to the console.
You could put a call to dumpStack() in "hotspots" of your code to get the calling stack the application took to get to that point.