zl程序教程

您现在的位置是:首页 >  其他

当前栏目

golang源码分析利器:goplantuml

2023-02-18 16:32:25 时间

https://github.com/jfeliu007/goplantuml 可以根据golang源码生成platuml描述文件,然后我们通过plantuml工具生成图片、网页等格式,对于源码分析来说非常方便。安装:

% go get github.com/jfeliu007/goplantuml/parser
% go install github.com/jfeliu007/goplantuml/cmd/goplantuml@latest

使用说明非常详尽

 % goplantuml -h
Usage of goplantuml:
  -aggregate-private-members
        Show aggregations for private members. Ignored if -show-aggregations is not used.
  -hide-connections
        hides all connections in the diagram
  -hide-fields
        hides fields
  -hide-methods
        hides methods
  -hide-private-members
        Hide private fields and methods
  -ignore string
        comma separated list of folders to ignore
  -notes string
        Comma separated list of notes to be added to the diagram
  -output string
        output file path. If omitted, then this will default to standard output
  -recursive
        walk all directories recursively
  -show-aggregations
        renders public aggregations even when -hide-connections is used (do not render by default)
  -show-aliases
        Shows aliases even when -hide-connections is used
  -show-compositions
        Shows compositions even when -hide-connections is used
  -show-connection-labels
        Shows labels in the connections to identify the connections types (e.g. extends, implements, aggregates, alias of
  -show-implementations
        Shows implementations even when -hide-connections is used
  -show-options-as-note
        Show a note in the diagram with the none evident options ran with this CLI
  -title string

我们尝试生成类图

 goplantuml -recursive  learn/goreplay/goreplay > diagram_file_name.puml

然后有两种方案可视化:

1,安装vscode plantuml插件

Name: PlantUML
Id: jebbs.plantuml
Description: Rich PlantUML support for Visual Studio Code.
Version: 2.17.4
Publisher: jebbs
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=jebbs.plantuml

按下option + D 键就可以看到类图了。

2,通过plantUml.jar包生成

到https://github.com/plantuml/plantuml 项目可以下载最新的jar包

java -jar learn/plantuml.jar diagram_file_name.puml

默认情况因为mac没有Time字体库,会报下面的warning,因此需要安装一个字体库,地址是https://www.freebestfonts.com/timr45w-font#google_vignette

Warning: the fonts "Times" and "Times" are not available for the Java logical font "Serif", which may have unexpected appearance or behavior. Re-enable the "Times" font to remove this warning.

默认文件的分辨率最大为 4096x4096

(20.802 - 1410 Mo) 377 Mo - Width too large 60109. You should set PLANTUML_LIMIT_SIZE
(20.803 - 1410 Mo) 377 Mo - Height too large 18676. You should set PLANTUML_LIMIT_SIZE
(20.803 - 1410 Mo) 377 Mo - Creating image 4096x4096

我们可以通过-DPLANTUML_LIMIT_SIZE=60109来制定分辨率大小。同时可以加大jvm运行内存,否则会out of memory

 java -Xms1024m -Xmx8192m -jar learn/plantuml-SNAPSHOT.jar -v -DPLANTUML_LIMIT_SIZE=10240  diagram_file_name.puml

然后我们就可以得到下面的类图。

是不是很清晰?

PlantUML 是一个开源项目,支持快速绘制时序图、用例图、类图、活动图、组件图、状态图、对象图、部署图等。同时还支持非 UML 图的甘特图、架构图等。比如一些常见的语法:

(1) -> 来绘制参与者之间传递的消息, 而不必显式地声明参与者。

-->绘制一个虚线箭头。(<- <--)

注意:仅适用时序图 其他示意图规则是不同的

@startuml
Alice -> Bob: Authentication Request

(2)声明参与者(Declaring participant)

声明参与者的关键字有 actor,boundary,control,entity,database,collections

关键字as用于重命名参与者

@startuml
actor Foo1
boundary Foo2
control Foo3
entity Foo4
database Foo5
collections Foo6

比如下面这个简单的plantuml文件

@startuml
title 我的一天
participant 我
participant 家
participant 公司
participant 其它公司
participant 商场
==工作==
alt 周一至周五
我 -> 公司:上班
alt 需要外出
公司 -> 其它公司:外勤/出差
note left: 商务合作
其它公司 --> 公司:回公司
end
else 周末
我 -> 家:在家办公
end
==休息==
alt 有约
我 -> 商场:逛街
else
我 -> 家:宅
end
@enduml

生成的文件如下: