为指定 java 类生成 PlantUML puml文件工具( v2 )

news/2024/6/19 5:55:57 标签: java, 开发语言, plantpuml, puml, 类图
AttributePumlVO.java
java">

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;


@Getter
@Setter
public class AttributePumlVO implements Serializable {

    /**
     * 属性名称
     */
    private String name;

    /**
     * 属性类型
     */
    private Class type;

    @Override
    public String toString() {
        return "\ticon_hammer " + this.name + ": " + this.type.getSimpleName() + "\n";
    }
}

ClassPumlGenerate.java
java">
import lombok.Getter;
import lombok.Setter;
import org.reflections.Reflections;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;


@Getter
@Setter
public class ClassPumlGenerate {

    private Set<String> classIdentifiers = new HashSet<>();
    private List<ClassPumlVO> classPumlList = new ArrayList<>();

    private static final Set<String> JDK_METHOD_NAMES = new HashSet<>();
    private static final Set<String> JDK_CLASS_NAMES = new HashSet<>();
    private static final Set<String> JDK_ATTRIBUTE_NAMES = new HashSet<>();

    static {
        JDK_METHOD_NAMES.add( "wait" );
        JDK_METHOD_NAMES.add( "equals" );
        JDK_METHOD_NAMES.add( "toString" );
        JDK_METHOD_NAMES.add( "hashCode" );
        JDK_METHOD_NAMES.add( "notify" );
        JDK_METHOD_NAMES.add( "notifyAll" );
        JDK_METHOD_NAMES.add( "finalize" );

        JDK_CLASS_NAMES.add( "boolean" );
        JDK_CLASS_NAMES.add( "void" );
        JDK_CLASS_NAMES.add( "int" );
        JDK_CLASS_NAMES.add( "long" );
        JDK_CLASS_NAMES.add( "float" );
        JDK_CLASS_NAMES.add( "byte" );
        JDK_CLASS_NAMES.add( "double" );
        JDK_CLASS_NAMES.add( "short" );
        JDK_CLASS_NAMES.add( "[Ljava.lang.Object;" );
        JDK_CLASS_NAMES.add( "[B" );
        JDK_CLASS_NAMES.add( "[Ljava.lang.String;" );


        JDK_ATTRIBUTE_NAMES.add( "serialVersionUID" );
    }

    public void generatePumlForPackage( String packagePath,String outputPath,boolean ignoreInterface,boolean ignoreProperties ){
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter( outputPath ));
            this.classPumlList = new ArrayList<>();

            List<Class<?>> clazzList = this.getClasses(packagePath);
            for( Class clazz:clazzList ){
                this.generate( clazz,ignoreInterface,ignoreProperties);
            }

            writer.write( "@startuml\r\n" );
            writer.write( "!define icon_hammer <img:C:\\E\\sucai\\banshou3.png>\r\n" );
            writer.write( "!define icon_cube <img:C:\\E\\sucai\\cube_3.png>\r\n" );
            writer.write( "skinparam Class {\r\n" );
            writer.write( "\tBackgroundColor  #d3dcef/white\r\n" );
            writer.write( "}\r\n" );

            for( ClassPumlVO classPuml:classPumlList ){
                writer.write( classPuml.toString() );
            }

            writer.write( "@enduml\r\n" );
        } catch (Exception e) {
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception e) {
                }
            }
        }
    }

    public void generatePuml( Class clazz,String outputPath,boolean ignoreInterface,boolean ignoreProperties ){
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter( outputPath ));
            this.classPumlList = new ArrayList<>();
            this.generate( clazz,ignoreInterface,ignoreProperties);

            writer.write( "@startuml\r\n" );
            writer.write( "!define icon_hammer <img:C:\\E\\sucai\\banshou3.png>\r\n" );
            writer.write( "!define icon_cube <img:C:\\E\\sucai\\cube_3.png>\r\n" );
            writer.write( "skinparam Class {\r\n" );
            writer.write( "\tBackgroundColor  #d3dcef/white\r\n" );
            writer.write( "}\r\n" );

            for( ClassPumlVO classPuml:this.classPumlList ){
                writer.write( classPuml.toString() );
            }

            writer.write( "@enduml\r\n" );
        } catch (Exception e) {
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception e) {
                }
            }
        }
    }

    private void generate( Class clazz,boolean ignoreInterface,boolean ignoreProperties ){
        this.generate_inner( clazz,ignoreInterface,ignoreProperties );
    }

    public static void main(String[] args) {
        System.out.println( "xxxx$xxx".contains( "$" ) );
    }

    private void generate_inner(Class clazz,boolean ignoreInterface,boolean ignoreProperties) {
        boolean handleImplementClassList = false;
        // 只处理 class 和 interface
        if( clazz.isEnum() ){
            return;
        }
        String simpleClassName = clazz.getSimpleName();
        if( simpleClassName.toLowerCase(  ).endsWith( "properties" ) && ignoreProperties ){
            return;
        }

        // 防止重复处理
        String classIdentifier = clazz.isInterface() + " " + simpleClassName;
        if( this.classIdentifiers.contains( classIdentifier ) ){
            return;
        }

        String longClassName = clazz.getName();
        //  对jdk 以及框架类非业务的class 忽略处理
        if( longClassName.startsWith( "org." ) ||
            longClassName.startsWith( "java." ) ||
            longClassName.startsWith( "sun." ) ||
            longClassName.startsWith( "com.alibaba.fastjson." ) ||
            longClassName.startsWith( "tk.mybatis." ) ||
            longClassName.startsWith( "javax." )){
            return;
        }
        if( JDK_CLASS_NAMES.contains( longClassName ) ){
            return;
        }
        this.classIdentifiers.add( classIdentifier );

        if( clazz.isInterface() ){
            if( ignoreInterface ){
                this.generate_inner_4ImplementClassList( clazz,ignoreInterface,ignoreProperties );
                return;
            }else {
                handleImplementClassList = true;
            }
        }

        ClassPumlVO classPuml = new ClassPumlVO();
        classPuml.setShortName( simpleClassName );
        classPuml.setLongName( clazz.getName() );
        classPuml.setInterface( clazz.isInterface() );
        this.classPumlList.add( classPuml );

        //  获取该类直接声明的属性
        Field[] fields = clazz.getDeclaredFields();
        if( fields != null && fields.length > 0 ){
            List<AttributePumlVO> attributePumlList = new ArrayList<>();
            for( Field field:fields ){
                String fieldName = field.getName();
                if( JDK_ATTRIBUTE_NAMES.contains( fieldName ) ){
                    continue;
                }
                Class<?> fieldType = field.getType();
                if( fieldType != null && "org.slf4j.Logger".equals( fieldType.getName() ) ){
                    continue;
                }
                AttributePumlVO attributePuml = new AttributePumlVO();
                attributePuml.setName( fieldName );
                attributePuml.setType( fieldType );
                attributePumlList.add( attributePuml );

                //  对该属性类型对应的 class 进行递归处理
                this.generate_inner( field.getType(),ignoreInterface,ignoreProperties );
            }
            classPuml.setAttributePumlList( attributePumlList );
        }

        //  获取该类直接声明的方法
        Method[] methods = clazz.getDeclaredMethods();
        if( methods != null && methods.length > 0 ){
            List<MethodPumlVO> methodPumlList = new ArrayList<>();
            for( Method method:methods ){
                String methodName = method.getName();
                if( JDK_METHOD_NAMES.contains( methodName ) ){
                    continue;
                }
                if( methodName.contains( "$" ) ){
                    continue;
                }
                MethodPumlVO methodPuml = new MethodPumlVO();
                methodPuml.setName( methodName );
                methodPuml.setMethod( method );
                methodPuml.setReturnType( method.getReturnType() );
                methodPumlList.add( methodPuml );

                //  对该方法的返回类型对应的 class 进行递归处理
                this.generate_inner( method.getReturnType(),ignoreInterface,ignoreProperties );
            }
            classPuml.setMethodPumlList( methodPumlList );
        }
        if( handleImplementClassList ){
            // 当前 clazz是接口,获取其全部的实现类,递归调用此方法
            this.generate_inner_4ImplementClassList(clazz,ignoreInterface,ignoreProperties);
        }
    }

    private void generate_inner_4ImplementClassList(Class clazz, boolean ignoreInterface, boolean ignoreProperties) {
        if( clazz.getSimpleName().toLowerCase().endsWith( "mapper" ) ){
            return;
        }
        List<Class<?>> implementClassList = this.getImplementClassList4CurrentPackage(clazz);
        if( implementClassList == null || implementClassList.size() == 0 ){
            return;
        }
        for( Class implementClass:implementClassList ){
            this.generate_inner( implementClass,ignoreInterface,ignoreProperties );
        }
    }

    private List<Class<?>> getImplementClassList4CurrentPackage(Class clazz){
        String servicePackage = clazz.getPackage().getName();
        Reflections reflections = new Reflections(servicePackage);
        Set<Class<?>> subTypes = reflections.getSubTypesOf( clazz );
        if( subTypes == null || subTypes.size() == 0 ){
            return new ArrayList<>( 0 );
        }
        return new ArrayList<>(subTypes);
    }

    private List<Class<?>> getClasses(String packageName){
        //第一个class类的集合
        List<Class<?>> classes = new ArrayList<Class<?>>();
        //是否循环迭代
        boolean recursive = true;
        //获取包的名字 并进行替换
        String packageDirName = packageName.replace('.', '/');
        //定义一个枚举的集合 并进行循环来处理这个目录下的things
        Enumeration<URL> dirs;
        try {
            dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
            //循环迭代下去
            while (dirs.hasMoreElements()){
                //获取下一个元素
                URL url = dirs.nextElement();
                //得到协议的名称
                String protocol = url.getProtocol();
                //如果是以文件的形式保存在服务器上
                if ("file".equals(protocol)) {
                    //获取包的物理路径
                    String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
                    //以文件的方式扫描整个包下的文件 并添加到集合中
                    this.findAndAddClassesInPackageByFile(packageName, filePath, recursive, classes);
                } else if ("jar".equals(protocol)){
                    //如果是jar包文件
                    //定义一个JarFile
                    JarFile jar;
                    try {
                        //获取jar
                        jar = ((JarURLConnection) url.openConnection()).getJarFile();
                        //从此jar包 得到一个枚举类
                        Enumeration<JarEntry> entries = jar.entries();
                        //同样的进行循环迭代
                        while (entries.hasMoreElements()) {
                            //获取jar里的一个实体 可以是目录 和一些jar包里的其他文件 如META-INF等文件
                            JarEntry entry = entries.nextElement();
                            String name = entry.getName();
                            //如果是以/开头的
                            if (name.charAt(0) == '/') {
                                //获取后面的字符串
                                name = name.substring(1);
                            }
                            //如果前半部分和定义的包名相同
                            if (name.startsWith(packageDirName)) {
                                int idx = name.lastIndexOf('/');
                                //如果以"/"结尾 是一个包
                                if (idx != -1) {
                                    //获取包名 把"/"替换成"."
                                    packageName = name.substring(0, idx).replace('/', '.');
                                }
                                //如果可以迭代下去 并且是一个包
                                if ((idx != -1) || recursive){
                                    //如果是一个.class文件 而且不是目录
                                    if (name.endsWith(".class") && !entry.isDirectory()) {
                                        //去掉后面的".class" 获取真正的类名
                                        String className = name.substring(packageName.length() + 1, name.length() - 6);
                                        try {
                                            //添加到classes
                                            classes.add(Class.forName(packageName + '.' + className));
                                        } catch (ClassNotFoundException e) {
                                        }
                                    }
                                }
                            }
                        }
                    } catch (IOException e) {
                    }
                }
            }
        } catch (IOException e) {
        }
        return classes;
    }

    /**
     * 以文件的形式来获取包下的所有Class
     * @param packageName
     * @param packagePath
     * @param recursive
     * @param classes
     */
    private void findAndAddClassesInPackageByFile(String packageName, String packagePath, final boolean recursive, List<Class<?>> classes){
        //获取此包的目录 建立一个File
        File dir = new File(packagePath);
        //如果不存在或者 也不是目录就直接返回
        if (!dir.exists() || !dir.isDirectory()) {
            return;
        }
        //如果存在 就获取包下的所有文件 包括目录
        File[] dirfiles = dir.listFiles(new FileFilter() {
            //自定义过滤规则 如果可以循环(包含子目录) 或则是以.class结尾的文件(编译好的java类文件)
            public boolean accept(File file) {
                return (recursive && file.isDirectory()) || (file.getName().endsWith(".class"));
            }
        });
        //循环所有文件
        for (File file : dirfiles) {
            //如果是目录 则继续扫描
            if (file.isDirectory()) {
                findAndAddClassesInPackageByFile(packageName + "." + file.getName(),
                        file.getAbsolutePath(),
                        recursive,
                        classes);
            }else {
                //如果是java类文件 去掉后面的.class 只留下类名
                String className = file.getName().substring(0, file.getName().length() - 6);
                try {
                    //添加到集合中去
                    classes.add(Class.forName(packageName + '.' + className));
                } catch (ClassNotFoundException e) {
                }
            }
        }
    }
}

ClassPumlVO.java
java">

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;


@Getter
@Setter
public class ClassPumlVO implements Serializable {

    private boolean isInterface;
    private String longName;
    private String shortName;

    private List<AttributePumlVO> attributePumlList;
    private List<MethodPumlVO> methodPumlList;

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("");
        if( this.isInterface ){
            sb.append( "interface" );
        }else {
            sb.append( "class" );
        }
        sb.append( " " );
        sb.append( this.shortName );
        // sb.append( this.longName );
        sb.append( " {\n" );

        if( this.attributePumlList != null && this.attributePumlList.size() > 0 ){
            for( AttributePumlVO attributePuml:this.attributePumlList ){
                sb.append( attributePuml.toString() );
            }
        }
        if( this.methodPumlList != null && this.methodPumlList.size() > 0 ){
            for( MethodPumlVO methodPuml:methodPumlList ){
                sb.append( methodPuml.toString() );
            }
        }
        sb.append( "}\n" );
        return sb.toString();
    }
}

MethodPumlVO.java
java">

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.lang.reflect.Method;


@Getter
@Setter
public class MethodPumlVO implements Serializable {

    private String name;
    private Class returnType;
    private Method method;

    @Override
    public String toString() {
        return "\ticon_cube " + this.name + "(): " + this.returnType.getSimpleName() + "\n";
    }
}

使用示例:

java">public static void main(String[] args) throws ClassNotFoundException, IOException {
        ClassPumlGenerate classPumlGenerate = new ClassPumlGenerate();
        Class clazz = XxxService.class;
        String outputPath = "C:\\E\\xxx\\xxx\\xxx\\xxx\\xxx-xxx-xxx\\src\\main\\resources\\puml\\xxx\\puml\\" + clazz.getSimpleName() + ".puml";
        classPumlGenerate.generatePuml( clazz,outputPath,true,true );

    }
}


http://www.niftyadmin.cn/n/5126246.html

相关文章

LinkedBlockingQueue源码解析

概念 LinkedBlockingQueue是非固定容量队列&#xff0c;内部是通过链表存放数据&#xff0c;并通过两把互斥锁&#xff08;分别为取时互斥锁和放时互斥锁&#xff0c;这也是与ArrayBlockingQueue的本质区别&#xff0c;这样可以提高性能&#xff09;来控制访问数据的同步问题。…

网络协议--TFTP:简单文件传送协议

15.1 引言 TFTP(Trivial File Transfer Protocol)即简单文件传送协议&#xff0c;最初打算用于引导无盘系统&#xff08;通常是工作站或X终端&#xff09;。和将在第27章介绍的使用TCP的文件传送协议&#xff08;FTP&#xff09;不同&#xff0c;为了保持简单和短小&#xff0…

[量化投资-学习笔记002]Python+TDengine从零开始搭建量化分析平台-MA均线的多种实现方式

MA 均线时最基本的技术指标&#xff0c;也是最简单&#xff0c;最不常用的&#xff08;通常使用EMA、SMA&#xff09;。 以下用两种不同的计算方法和两种不同的画图方法进行展示和说明。 MA 均线指标公式 MA (N)(C1 C2 C3 …C N )/N目录 方式一1.SQL 直接查询均值2.使用 pyp…

基于单片机16位智能抢答器设计

**单片机设计介绍&#xff0c;1645【毕设课设】基于单片机16位智能抢答器设计&#xff08;裁判功能、LCD数码管显示&#xff09;汇编 文章目录 一 概要二、功能设计设计思路 三、 软件设计原理图 五、 程序程序文档 六、 文章目录 一 概要 基于单片机16位智能抢答器设计&#x…

字符串中的strcpy和strncpy区别

strcpy:函数原型是char *strcpy(char* dest, const char *src)&#xff0c;含义是将src中的字符串复制到dest中。 strncpy:函数原型是char *strncpy(char *dest const char *src,int n&#xff09;&#xff0c;表示把src所指向的字符串中以src地址开始的前n个字节复制到dest所…

在3台不联网的 CentOS 7.8 服务器上部署 Elasticsearch 6.8 集群

为了在3台不联网的 CentOS 7.8 服务器上部署 Elasticsearch 6.8.23 集群&#xff0c;并考虑到path.data和path.logs的配置&#xff0c;我们可以按照以下步骤进行操作&#xff1a; 1. 准备工作 1.1 从有网络的机器下载 Elasticsearch 6.8.23 的 RPM 包&#xff1a; https://w…

C/C++面试常见问题——const关键字的作用和用法

首先我们需要一下const关键字的定义&#xff0c;const名叫常量限定符&#xff0c;当const修饰变量时&#xff0c;就是在告诉编译器该变量只可访问不可修改&#xff0c;而编译器对于被const修饰的变量有一个优化&#xff0c;编译器不会专门为其开辟空间&#xff0c;而是将变量名…

nginx 访问静态文件404错误

nginx 访问静态文件404错误 可能原因&#xff1a; 1、文件路径不对 2、nginx未重启 3、路径冲突 ,优先级&#xff08;详读&#xff1a;nginx location规则以及优先级详解&#xff09; 如以下配置 server {listen 80;server_name localhost;location / {proxy_pass …