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。

  /**
     * {@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 @ 2021-05-24 10:24  JadePeng  阅读(8551)  评论(3编辑  收藏  举报