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.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class PlantUMLUtils {
public static final Set JDK_CLASS_NAMES = new HashSet<>();
public static final Set JDK_METHOD_NAMES = new HashSet<>();
public static final Set serviceClassNames_alreadyPrint = new HashSet<>();
public static final Set pojoClassNames_alreadyPrint = new HashSet<>();
public static final Set umlLines = new HashSet<>();
public static final List> returnTypeClassList = new ArrayList<>();
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_METHOD_NAMES.add( "wait" );
}
private static void clear(){
serviceClassNames_alreadyPrint.clear();
pojoClassNames_alreadyPrint.clear();
umlLines.clear();
returnTypeClassList.clear();
}
public static void generatePumlFile4Controller( Class controllerClass,String outputFilePath ){
FileWriter writer = null;
try {
writer = new FileWriter( outputFilePath );
clear();
writer.write( "\r\n" );
writer.write( "@startuml\r\n" );
writer.write( "\r\n" );
writer.write( "\r\n" );
// 打印 controllerClass 的信息
PlantUMLUtils.printClassOrInterfacePuml( controllerClass,writer,true );
// 输出箭头指向类信息
printUmlLines( umlLines,writer );
// 打印方法的返回值的 class 信息
for( Class> returnTypeClass:returnTypeClassList ){
printPojoClassPuml( returnTypeClass,writer );
}
writer.write( "\r\n" );
writer.write( "\r\n" );
writer.write( "@enduml\r\n" );
}catch ( Exception e ){
e.printStackTrace();
}finally {
if( writer != null ){
try {
writer.close();
}catch ( Exception e ){
e.printStackTrace();
}
}
}
}
private static void printUmlLines(Set umlLines,FileWriter writer) throws IOException {
if( umlLines == null || umlLines.size() == 0 ){
return;
}
writer.write( "\r\n" );
for( UmlLineVO umlLine:umlLines ){
writer.write( umlLine.getFromName() + " " + umlLine.getLineType() + " " + umlLine.getToName() + "\r\n" );
}
writer.write( "\r\n" );
}
private static void handleField(Field field,FileWriter writer) 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,writer,false );
List> serviceClassList = MyReflectionUtils.getImplementClassList4CurrentPackage( fieldTypeClass );
if( serviceClassList != null ){
for( Class> serviceClass:serviceClassList ){
String serviceClassName = PlantUMLUtils.printClassOrInterfacePuml( serviceClass,writer,true );
// 记录 该 "service实现类" 实现 该 "service接口" 的箭头信息
UmlLineVO umlLine = new UmlLineVO();
umlLine.setFromName( serviceClassName );
umlLine.setLineType( "..|>" );
umlLine.setToName( fieldTypeClass.getSimpleName() + " : implements" );
umlLines.add( umlLine );
}
}
}else {
// 不是接口
PlantUMLUtils.printClassOrInterfacePuml( fieldTypeClass,writer,true );
}
}else if( fieldTypeClassName.toLowerCase().endsWith( "mapper" ) ){
// mapper 属性
PlantUMLUtils.printClassOrInterfacePuml( fieldTypeClass,writer,false );
}
}
/**
*
* @param clazz
* @param writer
* @param isClass
* @return ${className}
* @throws IOException
*/
public static String printClassOrInterfacePuml( Class clazz,FileWriter writer,Boolean isClass ) throws IOException {
String className = clazz.getSimpleName();
if( isClass ){
writer.write( "class " + className + " {\r\n" );
}else {
writer.write( "interface " + className + " {\r\n" );
}
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();
writer.write( "\t- " + fieldName + ": " + fieldTypeName + "\r\n" );
UmlLineVO umlLine = new UmlLineVO();
umlLine.setFromName( className );
umlLine.setLineType( "-->" );
umlLine.setToName( fieldTypeName + " : " + fieldName );
umlLines.add( umlLine );
}
}
}
// 追加方法列表
Method[] methods = clazz.getDeclaredMethods();
if( methods != null ){
for( Method method:methods ){
String methodName = method.getName();
if( JDK_METHOD_NAMES.contains( methodName ) ){
continue;
}
// 方法的返回值类型名称
Class> returnTypeClass = method.getReturnType();
String returnTypeClassName = returnTypeClass.getSimpleName();
returnTypeClassList.add( returnTypeClass );
writer.write( "\t+ " + methodName + "(): " + returnTypeClassName + "\r\n" );
if( !JDK_CLASS_NAMES.contains( returnTypeClassName ) ){
UmlLineVO umlLine = new UmlLineVO();
umlLine.setFromName( className );
umlLine.setLineType( "-->" );
umlLine.setToName( returnTypeClassName );
umlLines.add( umlLine );
}
}
}
writer.write( "}\r\n" );
if( isClass ){
Field[] fields = clazz.getDeclaredFields();
if( fields != null ){
for( Field field:fields ){
handleField( field,writer );
}
}
}
return className;
}
private static void printPojoClassPuml(Class> pojoClass,FileWriter writer ) throws IOException {
String pojoClassName = pojoClass.getSimpleName();
if( pojoClassNames_alreadyPrint.contains( pojoClassName ) ){
return;
}
if( JDK_CLASS_NAMES.contains( pojoClassName ) ){
// jdk 系统自己的类型,不输出( 只输出业务类型 )
return;
}
pojoClassNames_alreadyPrint.add( pojoClassName );
writer.write( "\r\n" );
writer.write( "class " + pojoClassName + " {\r\n" );
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();
writer.write( "\t- " + fieldName + ": " + fieldTypeName + "\r\n" );
}
}
writer.write( "}\r\n" );
writer.write( "\r\n" );
}
}
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
@Getter
@Setter
public class UmlLineVO implements Serializable ,Comparable{
private String fromName;
private String toName;
private String lineType;
@Override
public int compareTo(UmlLineVO other) {
if( this.getFromName().equals(other.getFromName() ) &&
this.getLineType().equals( other.getLineType() ) &&
this.getToName().equals( other.getToName() ) ){
return 0;
}
return 1;
}
}
import org.reflections.Reflections;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class MyReflectionUtils {
public static void main(String[] args) {
}
public static List> getImplementClassList4CurrentPackage(Class clazz){
String servicePackage = clazz.getPackage().getName();
// 扫描IDataValidator所在的包 com.lm.validate
Reflections reflections = new Reflections(servicePackage);
Set> subTypes = reflections.getSubTypesOf( clazz );
if( subTypes == null || subTypes.size() == 0 ){
return new ArrayList<>( 0 );
}
return new ArrayList<>(subTypes);
}
/**
* 从包package中获取所有的Class
* @param packageName
* @return
*/
public static List> getClasses(String packageName){
//第一个class类的集合
List> classes = new ArrayList>();
//是否循环迭代
boolean recursive = true;
//获取包的名字 并进行替换
String packageDirName = packageName.replace('.', '/');
//定义一个枚举的集合 并进行循环来处理这个目录下的things
Enumeration 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 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) {
e.printStackTrace();
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return classes;
}
/**
* 以文件的形式来获取包下的所有Class
* @param packageName
* @param packagePath
* @param recursive
* @param classes
*/
public static void findAndAddClassesInPackageByFile(String packageName, String packagePath, final boolean recursive, List> 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) {
e.printStackTrace();
}
}
}
}
}
import java.util.List;
public class Test {
public static void main(String[] args) {
List> controllerClassList = MyReflectionUtils.getClasses("com.xxx.controller");
for( Class> controllerClass:controllerClassList ){
String controllerClassName = controllerClass.getSimpleName();
String outputPath = "E:\\xxx\\xxx\\puml\\" + controllerClassName + ".puml";
PlantUMLUtils.generatePumlFile4Controller( controllerClass,outputPath );
System.out.println( outputPath + " 生成完毕..." );
}
}
}