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

PlantUMLUtils.java:
import org.reflections.Reflections;

import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
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;

public class PlantUmlUtils {

    private static final String ICON_METHOD = "icon_cube";
    private static final String ICON_FIELD = "icon_hammer";
    private static final Set<String> JDK_CLASS_NAMES = new HashSet<>();
    private static final Set<String> JDK_METHOD_NAMES = new HashSet<>();

    static {
        JDK_CLASS_NAMES.add( "void" );
        JDK_CLASS_NAMES.add( "Long" );
        JDK_CLASS_NAMES.add( "long" );
        JDK_CLASS_NAMES.add( "List" );
        JDK_CLASS_NAMES.add( "ArrayList" );
        JDK_CLASS_NAMES.add( "Collection" );
        JDK_CLASS_NAMES.add( "Set" );
        JDK_CLASS_NAMES.add( "HashSet" );
        JDK_CLASS_NAMES.add( "TreeSet" );
        JDK_CLASS_NAMES.add( "boolean" );
        JDK_CLASS_NAMES.add( "Boolean" );
        JDK_CLASS_NAMES.add( "int" );
        JDK_CLASS_NAMES.add( "Integer" );
        JDK_CLASS_NAMES.add( "Map" );
        JDK_CLASS_NAMES.add( "HashMap" );
        JDK_CLASS_NAMES.add( "TreeMap" );
        JDK_CLASS_NAMES.add( "double" );
        JDK_CLASS_NAMES.add( "Double" );
        JDK_CLASS_NAMES.add( "float" );
        JDK_CLASS_NAMES.add( "Float" );
        JDK_CLASS_NAMES.add( "Char" );
        JDK_CLASS_NAMES.add( "char" );
        JDK_CLASS_NAMES.add( "String" );
        JDK_CLASS_NAMES.add( "Class" );
        JDK_CLASS_NAMES.add( "JSONObject" );
        JDK_CLASS_NAMES.add( "JSONArray" );
        JDK_CLASS_NAMES.add( "Object" );

        JDK_METHOD_NAMES.add( "wait" );
    }

    public static void generatePumlFile4Controller( Class controllerClass,
                                                    String outputFilePath,
                                                    boolean printDirections,
                                                    boolean printInterface,
                                                    boolean printMapperAttribute ){
        FileWriter writer = null;
        try {
            //  将 lines写到  文件中
            writer = new FileWriter(outputFilePath);
            List<String> lines = new ArrayList<>();
            Set<String> class_interface_headers_exist = new HashSet<>();
            Set<String> directions_exist = new HashSet<>();
            Set<String> pojoClassNames_exist = new HashSet<>();
            List<Class<?>> returnTypeClassList = new ArrayList<>();

            generatePuml( controllerClass,
                    lines,
                    class_interface_headers_exist,
                    directions_exist,
                    pojoClassNames_exist,
                    returnTypeClassList,
                    printInterface,
                    printMapperAttribute );
            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( String line:lines ){
                writer.write( line );
                writer.write( "\r\n" );
            }
            if( printDirections ){
                for( String direction:directions_exist ){
                    writer.write( direction );
                    writer.write( "\r\n" );
                }
            }
            writer.write( "@enduml\r\n" );
        }catch ( Exception e ){
        }finally {
            if( writer != null ){
                try {
                    writer.close();
                }catch ( Exception e ){
                }
            }
        }
    }

    private static void generatePuml( Class controllerClass,
                                      List<String> lines,
                                      Set<String> class_interface_headers_exist,
                                      Set<String> directions_exist,
                                      Set<String> pojoClassNames_exist,
                                      List<Class<?>> returnTypeClassList,
                                      boolean printInterface,
                                      boolean printMapperAttribute ){
        try {

            // 打印 controllerClass 的信息
            PlantUmlUtils.printClassOrInterfacePuml( controllerClass,true,lines,class_interface_headers_exist,directions_exist,returnTypeClassList,printInterface,printMapperAttribute );

            //  打印方法的返回值的 class 信息
            for( Class<?> returnTypeClass:returnTypeClassList ){
                printPojoClassPuml( returnTypeClass,lines,pojoClassNames_exist,printMapperAttribute );
            }
        }catch ( Exception e ){
        }
    }

    private static void handleField(Field field,
                                    List<String> lines,
                                    Set<String> class_interface_headers_exist,
                                    Set<String> directions_exist,
                                    List<Class<?>> returnTypeClassList,
                                    boolean printInterface,
                                    boolean printMapperAttribute ) throws IOException {
        if( Modifier.isStatic( field.getModifiers() ) ){
            // 忽略静态属性
            return;
        }
        // 判断是否是  service 属性 todo 判断依据需要优化
        Class<?> fieldTypeClass = field.getType();
        String fieldTypeClassName = fieldTypeClass.getSimpleName();
        if( fieldTypeClassName.toLowerCase().endsWith( "service" ) ){
            // service 属性
            // 字段的类型的 class
            if( Modifier.isInterface( fieldTypeClass.getModifiers() ) ){
                // 是接口
                PlantUmlUtils.printClassOrInterfacePuml( fieldTypeClass,false,lines,class_interface_headers_exist,directions_exist,returnTypeClassList,printInterface,printMapperAttribute );

                List<Class<?>> serviceClassList = getImplementClassList4CurrentPackage( fieldTypeClass );
                if( serviceClassList != null ){
                    for( Class<?> serviceClass:serviceClassList ){
                        String serviceClassName = PlantUmlUtils.printClassOrInterfacePuml( serviceClass,true,lines,class_interface_headers_exist,directions_exist,returnTypeClassList,printInterface,printMapperAttribute );

                        //  记录 该 "service实现类" 实现 该 "service接口" 的箭头信息
                        String directionLine = serviceClassName + " ..|> " + fieldTypeClass.getSimpleName() + " : implements";
                        directions_exist.add( directionLine );
                    }
                }
            }else {
                // 不是接口
                PlantUmlUtils.printClassOrInterfacePuml( fieldTypeClass,true,lines,class_interface_headers_exist,directions_exist,returnTypeClassList,printInterface,printMapperAttribute );
            }
        }else if( fieldTypeClassName.toLowerCase().endsWith( "mapper" ) ){
            // mapper 属性
            PlantUmlUtils.printClassOrInterfacePuml( fieldTypeClass,false,lines,class_interface_headers_exist,directions_exist,returnTypeClassList,printInterface,printMapperAttribute );
        }
    }

    /**
     *
     * @param clazz
     * @param isClass
     * @return ${className}
     * @throws IOException
     */
    private static String printClassOrInterfacePuml( Class clazz,
                                                     Boolean isClass,
                                                     List<String> lines,
                                                     Set<String> class_interface_headers_exist,
                                                     Set<String> directions_exist,
                                                     List<Class<?>> returnTypeClassList,
                                                     boolean printInterface,
                                                     boolean printMapperAttribute ) throws IOException {
        String className = clazz.getSimpleName();
        if( !isClass && !printInterface ){
            return className;
        }
        String header = null;
        if( isClass ){
            header = "class " + className + " {";
        }else {
            header = "interface " + className + " {";
        }
        if( class_interface_headers_exist.contains( header ) ){
            // 防止重复输出
            return className;
        }
        class_interface_headers_exist.add( header );
        lines.add( header );

        if( isClass ){
            //  追加属性列表
            Field[] fields = clazz.getDeclaredFields();
            if( fields != null ){
                for( Field field:fields ){
                    if( Modifier.isStatic( field.getModifiers() ) ){
                        // 静态属性不输出
                        continue;
                    }

                    String fieldName = field.getName();
                    String fieldTypeName = field.getType().getSimpleName();
                    if( fieldTypeName.toLowerCase().endsWith( "mapper" ) && !printMapperAttribute ){
                        continue;
                    }
                    lines.add( "\t" + ICON_FIELD + " " + fieldName + ": " + fieldTypeName );
                    String direction_line = className + " --> " +  fieldTypeName + " : " + fieldName;
                    directions_exist.add( direction_line );
                }
            }
        }

        // 追加方法列表
        Method[] methods = clazz.getDeclaredMethods();
        if( methods != null ){
            for( Method method:methods ){
                String methodName = method.getName();
                if( JDK_METHOD_NAMES.contains( methodName ) ){
                    continue;
                }
                if( methodName.startsWith( "lambda$" ) ){
                    continue;
                }

                // 方法的返回值类型名称
                Class<?> returnTypeClass = method.getReturnType();
                String returnTypeClassName = returnTypeClass.getSimpleName();
                returnTypeClassList.add( returnTypeClass );
                lines.add( "\t" + ICON_METHOD + " " + methodName + "(): " + returnTypeClassName );

                if( !JDK_CLASS_NAMES.contains( returnTypeClassName ) ){
                    String directionLine = className + " --> " + returnTypeClassName;
                    directions_exist.add( directionLine );
                }
            }
        }
        lines.add( "}" );

        if( isClass ){
            Field[] fields = clazz.getDeclaredFields();
            if( fields != null ){
                for( Field field:fields ){
                    handleField( field,lines,class_interface_headers_exist,directions_exist,returnTypeClassList,printInterface,printMapperAttribute );
                }
            }
        }
        return className;
    }

    private static void printPojoClassPuml( Class<?> pojoClass,
                                            List<String> lines,Set<String> pojoClassNames_exist,
                                            boolean printMapperAttribute ) throws IOException {
        String pojoClassName = pojoClass.getSimpleName();
        if( pojoClassNames_exist.contains( pojoClassName ) ){
            return;
        }

        if( JDK_CLASS_NAMES.contains( pojoClassName ) ){
            // jdk 系统自己的类型,不输出( 只输出业务类型 )
            return;
        }

        pojoClassNames_exist.add( pojoClassName );
        lines.add( "class " + pojoClassName + " {" );

        Field[] fields = pojoClass.getDeclaredFields();
        if( fields != null ){
            for( Field field:fields ){
                if( Modifier.isStatic( field.getModifiers() ) ){
                    // 静态属性不输出
                    continue;
                }
                String fieldName = field.getName();
                // todo 如果是复合对象,是不是也要递归??
                String fieldTypeName = field.getType().getSimpleName();
                if( fieldTypeName.toLowerCase().endsWith( "mapper" ) && !printMapperAttribute ){
                    continue;
                }
                lines.add( "\t" + ICON_FIELD + " " + fieldName + ": " + fieldTypeName);
            }
        }
        lines.add( "}" );
    }

    public static void generatePumlFile4AllControllers(String controllerPackageAbsPath,
                                                       String outputFilePath,
                                                       boolean printDirections,
                                                       boolean printInterface,
                                                       boolean printMapperAttribute) {
        FileWriter writer = null;
        try {
            //  将 lines写到  文件中
            writer = new FileWriter(outputFilePath);
            List<String> lines = new ArrayList<>();
            Set<String> class_interface_headers_exist = new HashSet<>();
            Set<String> directions_exist = new HashSet<>();
            Set<String> pojoClassNames_exist = new HashSet<>();
            List<Class<?>> returnTypeClassList = new ArrayList<>();

            List<Class<?>> controllerClassList = getClasses(controllerPackageAbsPath );
            for (Class<?> controllerClass:controllerClassList){
                generatePuml( controllerClass,
                        lines,
                        class_interface_headers_exist,
                        directions_exist,
                        pojoClassNames_exist,
                        returnTypeClassList,
                        printInterface,
                        printMapperAttribute );
            }
            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( String line:lines ){
                writer.write( line );
                writer.write( "\r\n" );
            }
            if( printDirections ){
                for( String direction:directions_exist ){
                    writer.write( direction );
                    writer.write( "\r\n" );
                }
            }
            writer.write( "@enduml\r\n" );
        }catch ( Exception e ){
        }finally {
            if( writer != null ){
                try {
                    writer.close();
                }catch ( Exception e ){
                }
            }
        }
    }


    private static List<Class<?>> getImplementClassList4CurrentPackage(Class clazz){
        String servicePackage = clazz.getPackage().getName();
        // 扫描IDataValidator所在的包 com.lm.validate
        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);
    }


    /**
     * 从包package中获取所有的Class
     * @param packageName
     * @return
     */
    private static 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");
                    //以文件的方式扫描整个包下的文件 并添加到集合中
                    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 static 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) {
                }
            }
        }
    }
}
AttributePumlVO.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:

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<>();

    static {
        JDK_METHOD_NAMES.add( "wait" );

        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;" );
    }

    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 );
    }

    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 ){
                AttributePumlVO attributePuml = new AttributePumlVO();
                attributePuml.setName( field.getName() );
                attributePuml.setType( field.getType() );
                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 ){
                MethodPumlVO methodPuml = new MethodPumlVO();
                methodPuml.setName( method.getName() );
                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:
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:
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";
    }
}

Test.java: 


public class Test {
 
    public static void main(String[] args) {
        Class<XxxController> controllerClass = XxxController.class;
        String controllerClassName = controllerClass.getSimpleName();
        String outputPath = "C:\\E\\xxx\\xxx\\xxx\\xxx\\xxx-xxx-xxx\\src\\main\\resources\\puml\\" + controllerClassName + ".puml";
        PlantUMLUtils.generatePumlFile4Controller( controllerClass,outputPath,false,false,false );
        System.out.println( outputPath + " 生成完毕..." );

        String outputFilePath = "C:\\E\\xxx\\xxx\\xxx\\xxx\\xxx-xxx-xxx\\src\\main\\resources\\puml\\all.puml";
        PlantUMLUtils.generatePumlFile4AllControllers( "com.goldwind.ipark.base.controller",outputFilePath,false,false,false);

        
        ClassPumlGenerate classPumlGenerate = new ClassPumlGenerate();
        Class<XxxService> clazz = XxxService.class;
        String outputPath = "C:\\E\\xxx\\xxx\\xxx\\erkxxx\xxx-xxx-xxx\\src\\main\\java\\xxx\\xxx\\puml\\" + clazz.getSimpleName() + ".puml.txt";
        classPumlGenerate.generatePuml( clazz,outputPath,true,true );


    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值