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

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

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

介绍

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

安装

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

使用

激活窗口:

激活

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

然后点击Run.

BUILD

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

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

简单说明:

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

自定义graph

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

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

Build

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

图形

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

Float

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

  1. /**
  2. * {@code POST /admin/users} : Creates a new user.
  3. * <p>
  4. * Creates a new user if the login and email are not already used, and sends an
  5. * mail with an activation link.
  6. * The user needs to be activated on creation.
  7. *
  8. * @param userDTO the user to create.
  9. * @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.
  10. * @throws URISyntaxException if the Location URI syntax is incorrect.
  11. * @throws BadRequestAlertException {@code 400 (Bad Request)} if the login or email is already in use.
  12. */
  13. @PostMapping("/users")
  14. @PreAuthorize("hasAuthority(\"" + AuthoritiesConstants.ADMIN + "\")")
  15. public ResponseEntity<User> createUser(@Valid @RequestBody AdminUserDTO userDTO) throws URISyntaxException {
  16. log.debug("REST request to save User : {}", userDTO);
  17. if (userDTO.getId() != null) {
  18. throw new BadRequestAlertException("A new user cannot already have an ID", "userManagement", "idexists");
  19. // Lowercase the user login before comparing with database
  20. } else if (userRepository.findOneByLogin(userDTO.getLogin().toLowerCase()).isPresent()) {
  21. throw new LoginAlreadyUsedException();
  22. } else if (userRepository.findOneByEmailIgnoreCase(userDTO.getEmail()).isPresent()) {
  23. throw new EmailAlreadyUsedException();
  24. } else {
  25. User newUser = userService.createUser(userDTO);
  26. mailService.sendCreationEmail(newUser);
  27. return ResponseEntity
  28. .created(new URI("/api/admin/users/" + newUser.getLogin()))
  29. .headers(HeaderUtil.createAlert(applicationName, "userManagement.created", newUser.getLogin()))
  30. .body(newUser);
  31. }
  32. }

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

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

Controller

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

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

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

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


感谢您的认真阅读。

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

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

版权声明:本文为xiaoqi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xiaoqi/p/idea-call-graph-usage.html