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

发布于 2024-08-16 14:41:15 字数 61 浏览 8 评论 0原文

我想分析和理解某个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

我一直都在从未离去 2024-08-23 14:41:15

获取调用堆栈

1) 如果您可以调试应用程序,只需放置一个断点(双击代码的左边距)并等待它停止即可。如果您不在调试视角,请转到“调试视角”,然后打开“调用堆栈视图/面板”。它有调用堆栈:)

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

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

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

获取方法引用

您可以通过右键单击“引用”、“工作区”来获取对方法的所有引用。它将搜索当前打开的项目中的所有调用。非常非常有用。

分析应用程序

(感谢那些回答分析器选项的人)

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:

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:

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

荭秂 2024-08-23 14:41:15

我遇到了完全相同的问题,所以我编写了 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.

初心 2024-08-23 14:41:15

Netbeans profiler 对此非常有用!

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

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

它启动图形控制台工具
使您能够监控和管理
Java 应用程序和虚拟机
在本地或远程计算机上。

Netbeans profiler is very good for this !!

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

You can also use jconsole command (part of the 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.

物价感观 2024-08-23 14:41:15

使用 Eclipse Profiler 可能会满足您的需求。

Using the Eclipse Profiler might get you what you want.

悲凉≈ 2024-08-23 14:41:15

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

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

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

Exception e = new Exception();
e.printStackTrace();
舟遥客 2024-08-23 14:41:15

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

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

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

要复制到文本文件,请右键单击调用层次结构视图中的节点,然后单击“复制扩展层次结构”。

缺点:

  • 展开所有树节点必须手动展开(在 Windows 上重复按住 Shift+点击向右箭头)
  • 如果代码没有明显的起点,则必须对每个方法都执行此操作

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:

  • 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
别低头,皇冠会掉 2024-08-23 14:41:15

我有时使用 Eclipse 分析器。

I use the Eclipse profiler sometimes.

源来凯始玺欢你 2024-08-23 14:41:15

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文