Browse Source

Cache ModifiedClassPathClassLoaders

Closes gh-18694
pull/18697/head
Phillip Webb 6 years ago
parent
commit
83d4d94e8b
  1. 8
      spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java

8
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; @@ -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; @@ -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; @@ -60,6 +62,8 @@ import org.springframework.util.StringUtils;
*/
final class ModifiedClassPathClassLoader extends URLClassLoader {
private static final Map<Class<?>, 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 { @@ -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);

Loading…
Cancel
Save