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依賴哪些函數。

這次就介紹到這里,更多的功能待大家進一步發掘。


感謝您的認真閱讀。

如果你覺得有幫助,歡迎點贊支持!

不定期分享軟件開發經驗,歡迎關注作者, 一起交流軟件開發:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM