You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PlantUML Java API解析UML类图并访问图元?

解析PlantUML类图并遍历类元素(基于plantuml-mit库)

问题描述

我想要解析现有PlantUML类图以实现自定义DDL代码生成器,第一步尝试通过Java使用plantuml-mit库(版本1.2023.9)解析包含类图的*.puml文件,期望能遍历图中所有类。

请问如何从代码中访问图元素?目前我发现了以下两个类,它们的构造函数都需要UmlDatasource实例,但我不确定如何创建该实例:

net.sourceforge.plantuml.classdiagram.ClassDiagram
net.sourceforge.plantuml.classdiagram.ClassDiagramFactory

实现步骤与代码示例

1. 引入依赖

以Maven为例,在pom.xml中添加plantuml-mit依赖:

<dependency>
    <groupId>net.sourceforge.plantuml</groupId>
    <artifactId>plantuml-mit</artifactId>
    <version>1.2023.9</version>
</dependency>

2. 解析类图并遍历元素

不需要手动创建UmlDatasource,可以通过PlantUML的核心解析流程直接获取ClassDiagram实例,进而遍历所有类:

import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader;
import net.sourceforge.plantuml.classdiagram.ClassDiagram;
import net.sourceforge.plantuml.core.Diagram;
import net.sourceforge.plantuml.cucadiagram.IEntity;
import net.sourceforge.plantuml.cucadiagram.IGroup;
import net.sourceforge.plantuml.cucadiagram.LeafType;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

public class PlantUmlClassParser {
    public static void main(String[] args) throws IOException {
        // 读取目标puml文件内容
        File pumlFile = new File("path/to/your/class-diagram.puml");
        StringBuilder contentBuilder = new StringBuilder();
        try (FileReader reader = new FileReader(pumlFile)) {
            int charCode;
            while ((charCode = reader.read()) != -1) {
                contentBuilder.append((char) charCode);
            }
        }

        // 解析PlantUML源码生成图实例
        SourceStringReader sourceReader = new SourceStringReader(contentBuilder.toString());
        Diagram diagram = sourceReader.getDiagram(new FileFormatOption());

        // 转换为类图并遍历元素
        if (diagram instanceof ClassDiagram classDiagram) {
            // 遍历所有类实体
            List<IEntity> allEntities = classDiagram.getLeafs();
            for (IEntity entity : allEntities) {
                if (entity.getLeafType() == LeafType.CLASS) {
                    System.out.println("类名:" + entity.getName());
                    // 可扩展获取类的属性、方法等信息
                    // entity.getFields()、entity.getMethods()
                }
            }

            // 遍历包分组(如果有包结构)
            List<IGroup> groups = classDiagram.getGroups();
            for (IGroup group : groups) {
                System.out.println("\n包名:" + group.getName());
                List<IEntity> groupEntities = group.getLeafs();
                for (IEntity groupEntity : groupEntities) {
                    if (groupEntity.getLeafType() == LeafType.CLASS) {
                        System.out.println("包内类:" + groupEntity.getName());
                    }
                }
            }
        }
    }
}

关键说明

  • 无需手动实例化UmlDatasource:PlantUML的SourceStringReader会自动完成解析流程,生成包含完整类图信息的Diagram实例,直接强转为ClassDiagram即可使用。
  • 过滤类元素:通过LeafType.CLASS可以从所有实体中筛选出类,排除接口、枚举等其他类型元素。
  • 扩展提取信息:IEntity接口提供了获取类属性、方法、继承关系等的方法,可根据DDL生成的需求进一步提取相关数据。

内容的提问来源于stack exchange,提问作者eztam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 17:10:30