You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2026/04/28 08:46:10 UTC
(shardingsphere) branch master updated: Refactor AgentPath and AgentPathTest to support jdk26 (#38656)
This is an automated email from the ASF dual-hosted git repository.
RaigorJiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new e8c2d0c4dfa Refactor AgentPath and AgentPathTest to support jdk26 (#38656)
e8c2d0c4dfa is described below
commit e8c2d0c4dfaeed273bc90c92585d1bf290474e88
Author: jiangML <10...@qq.com>
AuthorDate: Tue Apr 28 16:46:03 2026 +0800
Refactor AgentPath and AgentPathTest to support jdk26 (#38656)
* Refactor AgentPathTest to support jdk26
* Refactor AgentPath
---
.../agent/core/ShardingSphereAgent.java | 2 +-
.../shardingsphere/agent/core/path/AgentPath.java | 5 +-
.../agent/core/path/AgentPathTest.java | 93 ++--------------------
3 files changed, 11 insertions(+), 89 deletions(-)
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/ShardingSphereAgent.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/ShardingSphereAgent.java
index 18747d69527..61a455b9cea 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/ShardingSphereAgent.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/ShardingSphereAgent.java
@@ -49,7 +49,7 @@ public final class ShardingSphereAgent {
* @throws IOException IO exception
*/
public static void premain(final String args, final Instrumentation instrumentation) throws IOException {
- File rootPath = AgentPath.getRootPath();
+ File rootPath = AgentPath.getRootPath(ClassLoader.getSystemClassLoader());
Map<String, PluginConfiguration> pluginConfigs = PluginConfigurationLoader.load(rootPath);
Collection<JarFile> pluginJars = PluginJarLoader.load(rootPath);
Map<String, AdvisorConfiguration> advisorConfigs = AdvisorConfigurationLoader.load(pluginJars, pluginConfigs.keySet());
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/path/AgentPath.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/path/AgentPath.java
index 4d628c30684..ae6df6ef893 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/path/AgentPath.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/path/AgentPath.java
@@ -36,11 +36,12 @@ public final class AgentPath {
/**
* Get agent root path.
*
+ * @param classLoader classLoader
* @return agent root path
*/
- public static File getRootPath() {
+ public static File getRootPath(final ClassLoader classLoader) {
String classResourcePath = String.join("", AgentPath.class.getName().replaceAll("\\.", "/"), ".class");
- URL resource = Objects.requireNonNull(ClassLoader.getSystemClassLoader().getResource(classResourcePath), "Can not locate agent jar file.");
+ URL resource = Objects.requireNonNull(classLoader.getResource(classResourcePath), "Can not locate agent jar file.");
return getJarFile(resource.toString()).getParentFile();
}
diff --git a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/path/AgentPathTest.java b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/path/AgentPathTest.java
index d1d1bcca258..1373643c076 100644
--- a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/path/AgentPathTest.java
+++ b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/path/AgentPathTest.java
@@ -18,23 +18,16 @@
package org.apache.shardingsphere.agent.core.path;
import com.google.common.io.ByteStreams;
-import net.bytebuddy.agent.ByteBuddyAgent;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.io.InputStream;
-import java.lang.instrument.Instrumentation;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
@@ -54,32 +47,24 @@ class AgentPathTest {
@Test
void assertGetRootPath() throws IOException {
Path jarPath = createAgentJar(tempDir.resolve("agent-path.jar"));
- try (
- URLClassLoader customClassLoader = new URLClassLoader(new URL[]{jarPath.toUri().toURL()}, null);
- SystemClassLoaderContext ignored = new SystemClassLoaderContext(customClassLoader)) {
- assertThat(AgentPath.getRootPath(), is(jarPath.getParent().toFile()));
+ try (URLClassLoader customClassLoader = new URLClassLoader(new URL[]{jarPath.toUri().toURL()}, null)) {
+ assertThat(AgentPath.getRootPath(customClassLoader), is(jarPath.getParent().toFile()));
}
}
@Test
void assertGetRootPathWhenJarMissing() throws IOException {
URL resourceUrl = new URL("jar:file:/non-existent/path/agent-path.jar!/org/apache/shardingsphere/agent/core/path/AgentPath.class");
- ClassLoader customClassLoader = new SingleResourceClassLoader(resourceUrl);
- try (SystemClassLoaderContext ignored = new SystemClassLoaderContext(customClassLoader)) {
- IllegalStateException ex = assertThrows(IllegalStateException.class, AgentPath::getRootPath);
- assertThat(ex.getMessage(), is(String.format("Can not locate agent jar file by URL `%s`.", resourceUrl)));
- }
+ IllegalStateException ex = assertThrows(IllegalStateException.class, () -> AgentPath.getRootPath(new SingleResourceClassLoader(resourceUrl)));
+ assertThat(ex.getMessage(), is(String.format("Can not locate agent jar file by URL `%s`.", resourceUrl)));
}
@Test
void assertGetRootPathWhenUrlMalformed() throws IOException {
URL resourceUrl = new URL("jar:file:/invalid path!/org/apache/shardingsphere/agent/core/path/AgentPath.class");
- ClassLoader customClassLoader = new SingleResourceClassLoader(resourceUrl);
- try (SystemClassLoaderContext ignored = new SystemClassLoaderContext(customClassLoader)) {
- IllegalStateException ex = assertThrows(IllegalStateException.class, AgentPath::getRootPath);
- assertThat(ex.getMessage(), is(String.format("Can not locate agent jar file by URL `%s`.", resourceUrl)));
- assertThat(ex.getCause(), isA(URISyntaxException.class));
- }
+ IllegalStateException ex = assertThrows(IllegalStateException.class, () -> AgentPath.getRootPath(new SingleResourceClassLoader(resourceUrl)));
+ assertThat(ex.getMessage(), is(String.format("Can not locate agent jar file by URL `%s`.", resourceUrl)));
+ assertThat(ex.getCause(), isA(URISyntaxException.class));
}
private Path createAgentJar(final Path jarPath) throws IOException {
@@ -95,70 +80,6 @@ class AgentPathTest {
return jarPath;
}
- private static final class SystemClassLoaderContext implements AutoCloseable {
-
- private static final Field SYSTEM_CLASS_LOADER_FIELD = createSystemClassLoaderField();
-
- private static boolean javaLangOpened;
-
- private final ClassLoader originalClassLoader;
-
- private SystemClassLoaderContext(final ClassLoader classLoader) {
- try {
- originalClassLoader = (ClassLoader) SYSTEM_CLASS_LOADER_FIELD.get(null);
- SYSTEM_CLASS_LOADER_FIELD.set(null, classLoader);
- } catch (final IllegalAccessException ex) {
- throw new IllegalStateException("Failed to override system class loader.", ex);
- }
- }
-
- @Override
- public void close() {
- try {
- SYSTEM_CLASS_LOADER_FIELD.set(null, originalClassLoader);
- } catch (final IllegalAccessException ex) {
- throw new IllegalStateException("Failed to restore system class loader.", ex);
- }
- }
-
- private static Field createSystemClassLoaderField() {
- ensureJavaLangOpened();
- try {
- Method getDeclaredFieldsMethod = Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class);
- getDeclaredFieldsMethod.setAccessible(true);
- for (Field each : (Field[]) getDeclaredFieldsMethod.invoke(ClassLoader.class, false)) {
- if ("scl".equals(each.getName())) {
- each.setAccessible(true);
- return each;
- }
- }
- throw new IllegalStateException("System class loader field not found.");
- } catch (final ReflectiveOperationException ex) {
- throw new IllegalStateException("Failed to access system class loader field.", ex);
- }
- }
-
- private static void ensureJavaLangOpened() {
- if (javaLangOpened) {
- return;
- }
- try {
- Class<?> moduleClass = Class.forName("java.lang.Module");
- Method getModuleMethod = Class.class.getMethod("getModule");
- Object javaBaseModule = getModuleMethod.invoke(Object.class);
- Object currentModule = getModuleMethod.invoke(AgentPathTest.class);
- Method redefineModuleMethod = Instrumentation.class.getMethod("redefineModule", moduleClass, Set.class, Map.class, Map.class, Set.class, Map.class);
- redefineModuleMethod.invoke(ByteBuddyAgent.install(), javaBaseModule, Collections.emptySet(), Collections.emptyMap(),
- Collections.singletonMap("java.lang", Collections.singleton(currentModule)), Collections.emptySet(), Collections.emptyMap());
- javaLangOpened = true;
- } catch (final ClassNotFoundException | NoSuchMethodException ignored) {
- javaLangOpened = true;
- } catch (final ReflectiveOperationException ex) {
- throw new IllegalStateException("Failed to open java.lang module for reflection.", ex);
- }
- }
- }
-
private static final class SingleResourceClassLoader extends ClassLoader {
private final URL resourceUrl;