首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >IDEA看代码必备插件Call Graph 介绍及使用方法

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

作者头像
IT大咖说
发布2021-07-19 17:16:32
发布2021-07-19 17:16:32
3K0
举报
文章被收录于专栏:IT大咖说IT大咖说

◆ 介绍#

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。

代码语言:javascript
复制
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/

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-06-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 IT大咖说 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ◆ 介绍#
  • ◆ 安装#
  • ◆ 使用
  • ◆ 激活
  • Build Graph
  • ◆ 查看graph
  • ◆ 自定义Graph#
  • ◆ 实战#
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档