diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java index 32a0eb7af86..ec9ef0f6cff 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.regex.Pattern; @@ -50,6 +51,7 @@ import org.eclipse.aether.transport.http.HttpTransporterFactory; import org.springframework.core.annotation.MergedAnnotation; import org.springframework.core.annotation.MergedAnnotations; import org.springframework.util.AntPathMatcher; +import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.util.StringUtils; /** @@ -60,6 +62,8 @@ import org.springframework.util.StringUtils; */ final class ModifiedClassPathClassLoader extends URLClassLoader { + private static final Map, ModifiedClassPathClassLoader> cache = new ConcurrentReferenceHashMap<>(); + private static final Pattern INTELLIJ_CLASSPATH_JAR_PATTERN = Pattern.compile(".*classpath(\\d+)?\\.jar"); private final ClassLoader junitLoader; @@ -78,6 +82,10 @@ final class ModifiedClassPathClassLoader extends URLClassLoader { } static ModifiedClassPathClassLoader get(Class testClass) { + return cache.computeIfAbsent(testClass, ModifiedClassPathClassLoader::compute); + } + + private static ModifiedClassPathClassLoader compute(Class testClass) { ClassLoader classLoader = testClass.getClassLoader(); return new ModifiedClassPathClassLoader(processUrls(extractUrls(classLoader), testClass), classLoader.getParent(), classLoader);