package com.matrix.core.enums;
|
|
|
import com.matrix.core.tools.LogUtil;
|
import org.springframework.boot.ApplicationArguments;
|
import org.springframework.boot.ApplicationRunner;
|
import org.springframework.core.Ordered;
|
import org.springframework.core.annotation.Order;
|
import org.springframework.stereotype.Component;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.net.JarURLConnection;
|
import java.net.URL;
|
import java.net.URLConnection;
|
import java.util.*;
|
import java.util.jar.JarEntry;
|
import java.util.jar.JarFile;
|
|
|
@Component
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
public class EnumsManager implements ApplicationRunner {
|
private final List<String> CLASS_NAME = new ArrayList<>();
|
private ClassLoader classLoader;
|
private static final String SUFFIX = ".class";
|
|
private Map<String, List<EnumsShowVo>> showAbleMap = new HashMap<>();
|
|
@Override
|
public void run(ApplicationArguments args) throws Exception {
|
LogUtil.info("扫描自定义枚举------------------");
|
initEnum("com.matrix");
|
LogUtil.info("扫描自定义枚举结束===============");
|
}
|
|
public List<EnumsShowVo> getShowEnum(String emumCode) {
|
return showAbleMap.get(emumCode);
|
}
|
|
|
private void initEnum(String... packages) {
|
classLoader = Thread.currentThread().getContextClassLoader();
|
|
for (String basePackage : packages) {
|
Enumeration<URL> resources = null;
|
try {
|
resources = classLoader.getResources(basePackage.replaceAll("\\.", "/"));
|
} catch (IOException e) {
|
return;
|
}
|
|
// 扫描当前工程和jar包
|
while (resources.hasMoreElements()) {
|
URL url = resources.nextElement();
|
if ("file".equals(url.getProtocol())) {
|
doFileScan(url.getPath());
|
} else if ("jar".equals(url.getProtocol())) {
|
doJarScan(basePackage, url);
|
}
|
}
|
}
|
|
// 初始化枚举数据
|
enumValue();
|
}
|
|
/**
|
* 扫描当前工程对应包下的所有类
|
*
|
* @param path
|
*/
|
private void doFileScan(String path) {
|
String rootPath = classLoader.getResource("").getPath();
|
File pathFile = new File(path);
|
|
File[] files = pathFile.listFiles();
|
if (files == null || files.length == 0) {
|
return;
|
}
|
|
for (File file : files) {
|
if (file.isDirectory()) {
|
String nextPath = path + "/" + file.getName();
|
doFileScan(nextPath);
|
} else if (file.getName().endsWith(SUFFIX)) {
|
if (!path.contains(rootPath)) {
|
return;
|
}
|
String subStr = path.substring(rootPath.length());
|
String className = (subStr + "/" + file.getName().replaceAll(SUFFIX, "")).replaceAll("/", "\\.");
|
CLASS_NAME.add(className);
|
}
|
}
|
}
|
|
/**
|
* 扫描jar包下对应包下所有类
|
*
|
* @param basePackage
|
* @param baseURL
|
*/
|
public void doJarScan(String basePackage, URL baseURL) {
|
basePackage = basePackage.replaceAll("\\.", "/");
|
JarFile jarFile;
|
try {
|
URLConnection urlConnection = baseURL.openConnection();
|
JarURLConnection jarUrl = (JarURLConnection) urlConnection;
|
jarFile = jarUrl.getJarFile();
|
} catch (IOException e) {
|
throw new RuntimeException("未找到资源");
|
}
|
|
Enumeration<JarEntry> entries = jarFile.entries();
|
while (entries.hasMoreElements()) {
|
JarEntry entry = entries.nextElement();
|
|
String name = entry.getName();
|
if (name.startsWith(basePackage)) {
|
if (name.endsWith(SUFFIX)) {
|
String className = name.replaceAll(SUFFIX, "").replaceAll("/", "\\.");
|
CLASS_NAME.add(className);
|
}
|
}
|
}
|
}
|
|
public void enumValue() {
|
for (String className : CLASS_NAME) {
|
Class<?> clazz = null;
|
try {
|
clazz = classLoader.loadClass(className);
|
} catch (ClassNotFoundException e) {
|
continue;
|
}
|
|
// 判断类是否为枚举类型
|
if (!clazz.isEnum()) {
|
continue;
|
}
|
|
// 判断ApiShowAble是否为类的父类
|
if (!EnumApiShowAble.class.isAssignableFrom(clazz)) {
|
continue;
|
}
|
|
Object[] constants = clazz.getEnumConstants();
|
EnumApiShowAble enumApiShowAble = (EnumApiShowAble) constants[0];
|
|
showAbleMap.put(enumApiShowAble.getEnumCode(), enumApiShowAble.getEnumsShowVos());
|
}
|
}
|
|
|
}
|