如何生成 Java 调用图,基于 Eclipse 的解决方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1982890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:03:56  来源:igfitidea点击:

How to generate a Java call graph, Eclipse based solutions

javaeclipsegraphcall-graph

提问by Guy

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.

我想分析和理解某个 Java 应用程序,我认为调用图会非常有用。我如何生成一个?我正在使用 Eclipse。

采纳答案by helios

Getting callstack

获取调用堆栈

1) If you can debugthe 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 :)

1) 如果您可以调试应用程序,只需放置一个断点(双击代码的左边距)并等待它停止。如果您不在那里,请转到 Debug Perspective,然后打开 Call stack View/Panel。它有调用堆栈:)

2) If you want to print this stack trace somewhere use an Exception:

2)如果您想在某处打印此堆栈跟踪,请使用异常:

Exception aux = new Exception("I'm here"); // not for throwing!
aux.printStackTrace(); // if you want it in stdout

or

或者

Exception aux = new Exception("I'm here"); // not for throwing!
StringWriter sw = new StringWriter();
aux.printStackTrace(new PrintWriter(sw));
String result = sw.toString(); // if you want it in a string

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:

Eclipse TPTP 提供分析:

http://www.eclipse.org/tptp/home/project_info/general/whatisTPTP.php

http://www.eclipse.org/tptp/home/project_info/general/whatisTPTP.php

回答by duffymo

Using the Eclipse Profilermight get you what you want.

使用Eclipse Profiler可能会得到您想要的。

回答by HeDinges

Fast and dirty, create a new exception and print the stacktrace.

又快又脏,创建一个新的异常并打印堆栈跟踪。

Exception e = new Exception();
e.printStackTrace();

回答by Kristopher Ives

I use the Eclipse profiler sometimes.

我有时会使用 Eclipse 分析器。

回答by wj.

Netbeans profileris very good for this !!

Netbeans 分析器对此非常有用!!

The profiling functions include CPU, memory and threads profiling as well as basic JVM monitoring ...

分析功能包括 CPU、内存和线程分析以及基本的 JVM 监控...

You can also use jconsolecommand (part of the jdk)

您还可以使用jconsole命令(jdk 的一部分

It launches a graphical console tool that enables you to monitor and manage Java applications and virtual machines on a local or remote machine.

它启动了一个图形控制台工具,使您能够监视和管理本地或远程机器上的 Java 应用程序和虚拟机。

回答by Aidos

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.

Thread.dumpStack() 允许您在不抛出异常或使用调试器的情况下输出当前调用堆栈。这将输出到控制台。

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.

您可以在代码的“热点”中调用 dumpStack() 以获取应用程序到达该点所需的调用堆栈。

回答by Georgios Gousios

I had exactly the same problem, so I 've written the java-callgraphsuite 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.

我遇到了完全相同的问题,所以我编写了java-callgraph工具套件。使用它,您可以创建动态(运行时)调用图和静态调用图,前提是您拥有程序及其依赖项的 jar 文件。

回答by giyom

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.

您无法在 Eclipse 中获得整个代码的图表。但是,您可以获得方法的调用者或被调用者的树视图。

In the source code, right-clickon the name of a method of interest, say main(String[] args), then click on Open Call Hierarchy. (Ctrl+Alt+H on Windows)

在源代码中,右键单击感兴趣的方法的名称,例如main(String[] args),然后单击 Open Call Hierarchy。(Windows 上的 Ctrl+Alt+H)

This will open a tree view of the hierarchy of callers (Caller Hierarchy). There is an option to view the Callee Hierarchy.

这将打开调用者层次结构(Caller Hierarchy)的树状视图。有一个选项可以查看 Callee Hierarchy。

To copy to a text file, right-clickon a node in the Call Hierarchy viewand click on Copy Expanded Hierarchy.

要复制到文本文件,请右键单击Call Hierarchy 视图中的节点,然后单击 Copy Expanded Hierarchy。

Drawbacks:

缺点:

  • expanding all tree nodes must done expanded manually (Shift+tap Right arrow repeatedly on Windows)
  • if there is not obvious starting point to the code, you'll have to do this for every method
  • 扩展所有树节点必须手动完成(在 Windows 上,Shift+点击右箭头重复)
  • 如果代码没有明显的起点,则必须对每种方法都执行此操作