📜  draaiom - Java (1)

📅  最后修改于: 2023-12-03 15:30:33.767000             🧑  作者: Mango

Draaiom - 解析Java类生成可视化类图

Draaiom是一个基于Java语言编写的开源工具,用于解析Java类文件并生成可视化类图,同时提供了简单易用的UI界面,方便程序员快速查看和分析类的结构,便于理解和调试Java程序。下面我们来详细介绍Draaiom的主要特点和功能。

主要特点
  1. 解析Java文件 - Draaiom支持解析Java文件并生成可视化的类图,类之间的依赖关系和继承关系一目了然。
  2. 生成类图 - Draaiom能够自动生成类图,展示类与类之间的关系图,方便理解和分析Java程序结构。
  3. GUI界面 - Draaiom提供简单易用的图形化用户界面,让您快速查看和分析Java类结构。
  4. 代码复用 - Draaiom支持代码复用,同一个项目中,程序员可以反复使用自己编写的类图。
程序示例

下面是一个简单的程序示例,演示如何使用Draaiom生成类图。

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.JFrame;

import com.draaiom.generate.ClassDiagramGenerator;
import com.draaiom.generate.uml.annotations.AnnotationsConfig;
import com.draaiom.generate.uml.classes.ClassConfig;
import com.draaiom.generate.uml.classes.ClassNameExtractor;
import com.draaiom.generate.uml.classes.ClassReader;
import com.draaiom.generate.uml.classes.ClassesConfig;
import com.draaiom.generate.uml.enums.ClassDirection;
import com.draaiom.generate.uml.generalization.GeneralizationReader;
import com.draaiom.generate.uml.generalization.GeneralizationsConfig;
import com.draaiom.generate.uml.interfaces.InterfaceConfig;
import com.draaiom.generate.uml.interfaces.InterfaceReader;
import com.draaiom.generate.uml.interfaces.InterfacesConfig;
import com.draaiom.generate.uml.primitives.PrimitivesConfig;
import com.draaiom.generate.uml.primitives.PrimitivesReader;
import com.draaiom.gui.DiagramPanel;
import com.draaiom.gui.DiagramWindow;
import com.draaiom.gui.DiagramWindowManager;
import com.draaiom.gui.actions.CloseDiagramWindowAction;
import com.draaiom.gui.actions.NewDiagramWindowAction;
import com.draaiom.gui.io.DiagramsReader;
import com.draaiom.gui.io.FileChooser;
import com.draaiom.gui.io.FileChooserResult;
import com.draaiom.gui.io.NewWindowConfiguration;
import com.draaiom.gui.io.NewWindowResult;

public class Test {

    private static Collection<Class<?>> readAllClasses(File[] files, boolean verbose) throws IOException {
        Collection<Class<?>> classes = new ArrayList<Class<?>>();
        PrimitivesConfig primitivesConfig = new PrimitivesConfig();
        PrimitivesReader.read(primitivesConfig, classes);
        ClassesConfig classesConfig = new ClassesConfig();
        ClassConfig classConfig =
            new ClassConfig()
                .withDirection(ClassDirection.DOWN)
                .withAnnotations(new AnnotationsConfig());
        classesConfig.addClassConfig(classConfig);

        ClassReader classReader = new ClassReader();
        ClassNameExtractor extractor = new ClassNameExtractor();
        for (File file : files) {
            if (verbose) {
                System.out.println("Reading File: " + file);
            }
            InputStream stream = null;
            try {
                stream = com.google.common.io.Files.newInputStreamSupplier(file).getInput();
                classes.addAll(classReader.read(stream, extractor, classesConfig));
            } catch (Exception e) {
                System.err.println("Failed to read " + file);
            } finally {
                stream.close();
            }
        }
        return classes;
    }

    public static void main(String[] args) throws Exception {
        FileChooserResult result = FileChooser.showDialog(null, true);
        if (result != null) {
            Collection<Class<?>> classes = readAllClasses(result.getFiles(), false);
            GeneralizationsConfig generalizationsConfig = new GeneralizationsConfig();
            GeneralizationReader.read(generalizationsConfig, classes);

            InterfacesConfig interfacesConfig = new InterfacesConfig();
            InterfaceConfig interfaceConfig = new InterfaceConfig();
            interfacesConfig.addInterfaceConfig(interfaceConfig);

            InterfaceReader interfaceReader = new InterfaceReader();
            for (File file : result.getFiles()) {
                InputStream stream = null;
                try {
                    stream = com.google.common.io.Files.newInputStreamSupplier(file).getInput();
                    interfaceReader.read(stream, interfacesConfig, classes);
                } catch (Exception e) {
                    System.err.println("Failed to read " + file);
                } finally {
                    stream.close();
                }
            }

            Collection<DiagramWindow> windows = new ArrayList<DiagramWindow>();
            for (Class<?> clazz : classes) {
                ClassDiagramGenerator cdgen = new ClassDiagramGenerator();
                DiagramPanel panel = new DiagramPanel();
                panel.setModel(cdgen.createClassDiagramModel(clazz));
                DiagramWindow window =
                    new DiagramWindow(panel, new CloseDiagramWindowAction(), new NewDiagramWindowAction());
                window.setTitle(clazz.getSimpleName());
                windows.add(window);
            }
            DiagramWindowManager diagramWindowManager = new DiagramWindowManager(windows);

            if (result.isReadOnly()) {
                for (DiagramWindow window : windows) {
                    window.setReadOnly(true);
                }
            }

            NewWindowResult newWindowResult = null;
            while (newWindowResult == null || newWindowResult.isOK()) {
                newWindowResult = diagramWindowManager.getMenuBar().openMenus(
                    new NewWindowConfiguration(),
                    new CloseDiagramWindowAction(),
                    new NewDiagramWindowAction());
            }
        }
    }
}
结语

Draaiom是一个功能强大的开源工具,支持解析Java文件并生成可视化的类图,提供了简单易用的UI界面,使程序员可以更加方便地理解和调试Java程序。如果你是一名Java程序员,Draaiom绝对是你不可错过的工具。