|
@@ -1,12 +1,20 @@
|
|
|
package org.springblade.manager.vo;
|
|
|
import cn.hutool.log.StaticLog;
|
|
|
import com.alibaba.fastjson.annotation.JSONField;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.lang.reflect.Modifier;
|
|
|
import org.springblade.common.utils.BaseUtils;
|
|
|
import org.springblade.common.utils.SystemUtils;
|
|
|
import org.springblade.core.tool.utils.StringPool;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.lang.reflect.Field;
|
|
|
+import java.net.JarURLConnection;
|
|
|
+import java.net.URL;
|
|
|
import java.util.*;
|
|
|
+import java.util.jar.JarEntry;
|
|
|
+import java.util.jar.JarFile;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -106,7 +114,7 @@ public interface DataModel {
|
|
|
}
|
|
|
|
|
|
/*扫描指定包,并获取所有实现DataModel的类,按照ID排序返回*/
|
|
|
- static List<Class<?>> findClassesByInterface(String packageName, Class<?> interfaceClass) {
|
|
|
+ /*static List<Class<?>> findClassesByInterface(String packageName, Class<?> interfaceClass) {
|
|
|
List<Class<?>> targetList = new ArrayList<>();
|
|
|
Map<Class<?>, String> classIdMap = new HashMap<>();
|
|
|
try {
|
|
@@ -154,6 +162,81 @@ public interface DataModel {
|
|
|
return id1.compareTo(id2);
|
|
|
});
|
|
|
return targetList;
|
|
|
+ }*/
|
|
|
+
|
|
|
+ static List<Class<?>> findClassesByInterface(String packageName, Class<?> interfaceClass) {
|
|
|
+ List<Class<?>> targetList = new ArrayList<>();
|
|
|
+ Map<Class<?>, String> classIdMap = new HashMap<>();
|
|
|
+ try {
|
|
|
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
|
|
+ String path = packageName.replace('.', '/');
|
|
|
+ Enumeration<URL> resources = classLoader.getResources(path);
|
|
|
+
|
|
|
+ while (resources.hasMoreElements()) {
|
|
|
+ URL resource = resources.nextElement();
|
|
|
+ if (resource == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (resource.getProtocol().equals("file")) {
|
|
|
+ File dir = new File(resource.getFile());
|
|
|
+ if (!dir.exists()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ File[] files = dir.listFiles();
|
|
|
+ if (files == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (File file : files) {
|
|
|
+ String fileName = file.getName();
|
|
|
+ if (fileName.endsWith(".class")) {
|
|
|
+ String className = packageName + '.' + fileName.substring(0, fileName.length() - 6);
|
|
|
+ loadClass(interfaceClass, targetList, classIdMap, className);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (resource.getProtocol().equals("jar")) {
|
|
|
+ JarURLConnection jarConnection = (JarURLConnection) resource.openConnection();
|
|
|
+ try (JarFile jarFile = jarConnection.getJarFile()) {
|
|
|
+ Enumeration<JarEntry> entries = jarFile.entries();
|
|
|
+ while (entries.hasMoreElements()) {
|
|
|
+ JarEntry entry = entries.nextElement();
|
|
|
+ String entryName = entry.getName();
|
|
|
+ if (entryName.startsWith(path) && entryName.endsWith(".class") && !entryName.contains("$")) {
|
|
|
+ String className = entryName.replace('/', '.').substring(0, entryName.length() - 6);
|
|
|
+ loadClass(interfaceClass, targetList, classIdMap, className);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ StaticLog.error("IOException: " + e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ targetList.sort((c1, c2) -> {
|
|
|
+ String id1 = classIdMap.get(c1);
|
|
|
+ String id2 = classIdMap.get(c2);
|
|
|
+ return id1.compareTo(id2);
|
|
|
+ });
|
|
|
+ return targetList;
|
|
|
+ }
|
|
|
+
|
|
|
+ static void loadClass(Class<?> interfaceClass, List<Class<?>> targetList, Map<Class<?>, String> classIdMap, String className) {
|
|
|
+ try {
|
|
|
+ Class<?> clazz = Class.forName(className);
|
|
|
+ if (interfaceClass.isAssignableFrom(clazz) && !clazz.isInterface() && java.lang.reflect.Modifier.isPublic(clazz.getModifiers())) {
|
|
|
+ try {
|
|
|
+ Field idField = clazz.getField("ID");
|
|
|
+ String idValue = (String) idField.get(null);
|
|
|
+ classIdMap.put(clazz, idValue);
|
|
|
+ } catch (NoSuchFieldException | IllegalAccessException e) {
|
|
|
+ StaticLog.error(e.getMessage());
|
|
|
+ }
|
|
|
+ targetList.add(clazz);
|
|
|
+ }
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ StaticLog.error("ClassNotFoundException: " + e.getMessage());
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
}
|