From ec121b13c42658bc5bf2d4ff4a43f856e7159f58 Mon Sep 17 00:00:00 2001 From: ruying408 <1877972603@qq.com> Date: Sat, 27 Jul 2024 15:07:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=92=E4=BB=B6=E4=BE=9D=E8=B5=96=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E6=96=B9jar=E5=A4=9A=E7=89=88=E6=9C=AC=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/cool/Welcome.java | 15 ++++++ .../service/DynamicJarLoaderService.java | 47 ++++++++++++++----- .../com/cool/core/util/CompilerUtils.java | 18 +++++++ 3 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/cool/Welcome.java diff --git a/src/main/java/com/cool/Welcome.java b/src/main/java/com/cool/Welcome.java new file mode 100644 index 0000000..4b4bd56 --- /dev/null +++ b/src/main/java/com/cool/Welcome.java @@ -0,0 +1,15 @@ +package com.cool; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequiredArgsConstructor +public class Welcome { + + @RequestMapping("/") + public String welcome() { + return "welcome"; + } +} diff --git a/src/main/java/com/cool/core/plugin/service/DynamicJarLoaderService.java b/src/main/java/com/cool/core/plugin/service/DynamicJarLoaderService.java index 11087e7..97a24df 100644 --- a/src/main/java/com/cool/core/plugin/service/DynamicJarLoaderService.java +++ b/src/main/java/com/cool/core/plugin/service/DynamicJarLoaderService.java @@ -9,6 +9,7 @@ import com.cool.core.config.PluginJson; import com.cool.core.exception.CoolPreconditions; import com.cool.core.plugin.config.DynamicJarClassLoader; import com.cool.core.util.AnnotationUtils; +import com.cool.core.util.CompilerUtils; import com.cool.modules.plugin.entity.PluginInfoEntity; import com.cool.modules.plugin.service.PluginInfoService; import java.io.File; @@ -74,19 +75,7 @@ public class DynamicJarLoaderService { try (JarFile jarFile = ((JarURLConnection) jarUrl.openConnection()).getJarFile()) { Enumeration entries = jarFile.entries(); while (entries.hasMoreElements()) { - JarEntry jarEntry = entries.nextElement(); - if (jarEntry.getName().endsWith(".class")) { - String className = jarEntry.getName().replace('/', '.').substring(0, - jarEntry.getName().length() - 6); - try { - // 加载类 - Class clazz = dynamicJarClassLoader.loadClass(className); - if (AnnotationUtils.hasCoolPluginAnnotation(clazz)) { - plugins.add(clazz); - } - } catch (NoClassDefFoundError ignored) { - } - } + loadClass(entries, dynamicJarClassLoader, plugins); } } @@ -99,6 +88,38 @@ public class DynamicJarLoaderService { return pluginJson; } + /** + * 加载class + */ + private static void loadClass(Enumeration entries, + DynamicJarClassLoader dynamicJarClassLoader, List> plugins) + throws ClassNotFoundException { + JarEntry jarEntry = entries.nextElement(); + + String entryName = jarEntry.getName(); + if (!entryName.endsWith(".class")) { + return; + } + String className = entryName.replace('/', '.').substring(0, entryName.length() - 6); + if (entryName.startsWith(CompilerUtils.META_INF_VERSIONS)) { + // 处理多版本类 + String jdkVersion = CompilerUtils.getJdkVersion(); + if (!entryName.startsWith(CompilerUtils.META_INF_VERSIONS + jdkVersion)) { + return; + } + // 替换版本目录 + className = className.replace((CompilerUtils.META_INF_VERSIONS + jdkVersion).replace("/", ".") + ".", ""); + } + try { + // 加载类 + Class clazz = dynamicJarClassLoader.loadClass(className); + if (AnnotationUtils.hasCoolPluginAnnotation(clazz)) { + plugins.add(clazz); + } + } catch (NoClassDefFoundError | UnsupportedClassVersionError ignored) { + } + } + /** * 注册插件 */ diff --git a/src/main/java/com/cool/core/util/CompilerUtils.java b/src/main/java/com/cool/core/util/CompilerUtils.java index 533f7ca..3e6506f 100644 --- a/src/main/java/com/cool/core/util/CompilerUtils.java +++ b/src/main/java/com/cool/core/util/CompilerUtils.java @@ -4,6 +4,8 @@ import cn.hutool.core.io.FileUtil; import com.mybatisflex.processor.MybatisFlexProcessor; import java.io.File; import java.io.IOException; +import java.lang.management.ManagementFactory; +import java.lang.management.RuntimeMXBean; import java.nio.charset.StandardCharsets; import java.util.List; import javax.annotation.processing.Processor; @@ -17,6 +19,22 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class CompilerUtils { + public final static String META_INF_VERSIONS = "META-INF/versions/"; + + // jdk版本 + private static String JVM_VERSION = null; + + /** + * 获取jdk版本 + */ + public static String getJdkVersion() { + if (JVM_VERSION == null) { + RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean(); + JVM_VERSION = runtimeMXBean.getSpecVersion(); + } + return JVM_VERSION; + } + /** * 创建文件, 先删除在创建 */