From 42050c45d9e1a787b0b08e663b91de8a23b2fe4d Mon Sep 17 00:00:00 2001 From: CaptainB Date: Sat, 26 Mar 2022 21:08:06 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E5=A2=9E=E5=8A=A0=20open=20url=20modu?= =?UTF-8?q?le=20=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/OpenUrlClassLoaderModule.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 backend/src/main/java/io/metersphere/config/OpenUrlClassLoaderModule.java diff --git a/backend/src/main/java/io/metersphere/config/OpenUrlClassLoaderModule.java b/backend/src/main/java/io/metersphere/config/OpenUrlClassLoaderModule.java new file mode 100644 index 0000000000..a1438973ca --- /dev/null +++ b/backend/src/main/java/io/metersphere/config/OpenUrlClassLoaderModule.java @@ -0,0 +1,41 @@ +package io.metersphere.config; + +import org.springframework.context.annotation.Configuration; + +import java.lang.reflect.Method; +import java.net.URLClassLoader; + +@Configuration +public class OpenUrlClassLoaderModule { + + static { + // If on Java 9+, open the URLClassLoader module to this module + // so we can access its API via reflection without producing a warning. + try { + openUrlClassLoaderModule(); + } catch (Throwable e) { + // ignore exception - will throw on Java 8 since the Module classes don't exist + e.printStackTrace(); + } + } + + private static void openUrlClassLoaderModule() throws Exception { + // This is effectively calling: + // + // URLClassLoader.class.getModule().addOpens( + // URLClassLoader.class.getPackageName(), + // ReflectionClassLoader.class.getModule() + // ); + // + // We use reflection since we build against Java 8. + + Class moduleClass = Class.forName("java.lang.Module"); + Method getModuleMethod = Class.class.getMethod("getModule"); + Method addOpensMethod = moduleClass.getMethod("addOpens", String.class, moduleClass); + + Object urlClassLoaderModule = getModuleMethod.invoke(URLClassLoader.class); + Object thisModule = getModuleMethod.invoke(OpenUrlClassLoaderModule.class); + + addOpensMethod.invoke(urlClassLoaderModule, URLClassLoader.class.getPackage().getName(), thisModule); + } +}