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,查看结果了。

简单说明:

  • 箭头 A->B,表示A函数调用B函数
  • 点击或者hover节点时,黄色的边代表上游调用(被谁调用),绿色代表下游(调用了谁)
  • 可以调节画布宽高等参数。

自定义Graph#

自定义graph

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

实战#

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

Build

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

图形

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

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

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

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

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

这次就介绍到这里,更多的功能待大家进一步发掘。


感谢您的认真阅读。

如果你觉得有帮助,欢迎点赞支持!

不定期分享软件开发经验,欢迎关注作者, 一起交流软件开发:

关注作者

欢迎关注作者微信公众号, 一起交流软件开发:欢迎关注作者微信公众号

posted @   JadePeng  阅读(14823)  评论(3编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-05-24 多语言业务错误日志收集监控工具Sentry 安装与使用
点击右上角即可分享
微信分享提示
CONTENTS