乐于分享
好东西不私藏

IDEA看代码必备插件Call Graph 介绍及使用方法

本文最后更新于2025-12-31,某些文章具有时效性,若有错误或已失效,请在下方留言或联系老夜

IDEA看代码必备插件Call Graph 介绍及使用方法

◆ 绍#

Call Graph是一款IDEA插件,用于可视化基于IntelliJ平台的IDE的函数调用图。

这个插件的目标是让代码更容易理解,有助于读懂和调试代码。当前只支持Java。针对Typescript、Javascript或Python工具,可以使用作者的另外一款工具Codemap(https://codemap.app/)

介绍

◆ 安装#

打开idea的设置-插件,搜索Call Graph,安装即可:

安装

◆ 使用

◆ 

安装后,通过View – Tool Windows – Call Graph ,激活窗口

激活

激活窗口:

激活

Build Graph

激活后,需要先Build graph,让插件分析java代码,可以选择对整个工程或者针对某个项目。

然后点击Run.

build

这一步,根据工程大小,耗时不同,如果代码量比较大,可能会卡顿几十秒,不要着急。

◆ 查看graph

等Build完成,可以切换到Graph tab,查看结果了。

查看graph

简单说明:

  • 箭头 A->B,表示A函数调用B函数

  • 点击或者hover节点时,黄色的边代表上游调用(被谁调用),绿色代表下游(调用了谁)

  • 可以调节画布宽高等参数。

◆ 自定义Graph#

可以自定义是否显示class name和file path,自定义节点颜色等,同时支持搜索和过滤不同级别的函数,内外部函数等。

实战#

我们打开一个jhipster生成的默认工程,先Build。

等待了10几秒,出现图形:

把窗口设置为Float,这样可以最大化查看图形。

我们来看一个controller里的方法,比如UserResource的createUser。

Copy  /**
* {@code POST /admin/users} : Creates a new user.
* <p>
* Creates a new user if the login and email are not already used, and sends an
* mail with an activation link.
* The user needs to be activated on creation.
*
* @param userDTO the user to create.
* @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new user, or with status {@code 400 (Bad Request)} if the login or email is already in use.
* @throws URISyntaxException if the Location URI syntax is incorrect.
* @throws BadRequestAlertException {@code 400 (Bad Request)} if the login or email is already in use.
*/

@PostMapping("/users")
@PreAuthorize("hasAuthority(\"" + AuthoritiesConstants.ADMIN + "\")")
public ResponseEntity<User> createUser(@Valid @RequestBody AdminUserDTO userDTO) throws URISyntaxException {
log.debug("REST request to save User : {}", userDTO);

if (userDTO.getId() != null) {
throw new BadRequestAlertException("A new user cannot already have an ID", "userManagement", "idexists");
// Lowercase the user login before comparing with database
} else if (userRepository.findOneByLogin(userDTO.getLogin().toLowerCase()).isPresent()) {
throw new LoginAlreadyUsedException();
} else if (userRepository.findOneByEmailIgnoreCase(userDTO.getEmail()).isPresent()) {
throw new EmailAlreadyUsedException();
} else {
User newUser = userService.createUser(userDTO);
mailService.sendCreationEmail(newUser);
return ResponseEntity
.created(new URI("/api/admin/users/" + newUser.getLogin()))
.headers(HeaderUtil.createAlert(applicationName, "userManagement.created", newUser.getLogin()))
.body(newUser);
}
}

鼠标点击函数名,右键菜单查看Graph:

就能查看到调用关系图了:

由于是controller里的函数,只有下游调用,没有上游。

我们再看个有上下游的,比如MailService里的发送模板邮件:

透过图形,可以很方便地看到上游有哪些函数调用了sendEmailFromTemplate,也清楚的知道sendEmailFromTemplate依赖哪些函数。

来源:

https://www.toutiao.com/i6965675689848701473/

“IT大咖说”欢迎广大技术人员投稿,投稿邮箱:aliang@itdks.com

来都来了,走啥走,留个言呗~

 IT大咖说  |  关于版权 

由“IT大咖说(ID:itdakashuo)”原创的文章,转载时请注明作者、出处及微信公众号。投稿、约稿、转载请加微信:ITDKS10(备注:投稿),茉莉小姐姐会及时与您联系!

感谢您对IT大咖说的热心支持!

本站文章均为手工撰写未经允许谢绝转载:夜雨聆风 » IDEA看代码必备插件Call Graph 介绍及使用方法
×
订阅图标按钮