From baf83aec0d0c7d72326fe248843af4412a5f3fed Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 12 Oct 2018 15:23:21 +0200 Subject: [PATCH] Polish "Allow ClassPathResources to be filtered by FilteredClassLoader" Closes gh-14774 --- .../test/context/FilteredClassLoader.java | 34 ++++++++++++++----- .../context/FilteredClassLoaderTests.java | 2 +- ...t => FilteredClassLoaderTestsResource.txt} | 0 3 files changed, 26 insertions(+), 10 deletions(-) rename spring-boot-project/spring-boot-test/src/test/resources/org/springframework/boot/test/context/{FilteredClassLoaderTestResource.txt => FilteredClassLoaderTestsResource.txt} (100%) diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/FilteredClassLoader.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/FilteredClassLoader.java index ce12d524e5..16cd61d9fe 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/FilteredClassLoader.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/FilteredClassLoader.java @@ -18,6 +18,9 @@ package org.springframework.boot.test.context; import java.net.URL; import java.net.URLClassLoader; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; import java.util.function.Predicate; import org.springframework.core.io.ClassPathResource; @@ -33,14 +36,17 @@ import org.springframework.core.io.ClassPathResource; */ public class FilteredClassLoader extends URLClassLoader { - private final Predicate[] filters; + private final Collection> classesFilters; + + private final Collection> resourcesFilters; /** * Create a {@link FilteredClassLoader} that hides the given classes. * @param hiddenClasses the classes to hide */ public FilteredClassLoader(Class... hiddenClasses) { - this(ClassFilter.of(hiddenClasses)); + this(Collections.singleton(ClassFilter.of(hiddenClasses)), + Collections.emptyList()); } /** @@ -48,33 +54,43 @@ public class FilteredClassLoader extends URLClassLoader { * @param hiddenPackages the packages to hide */ public FilteredClassLoader(String... hiddenPackages) { - this(PackageFilter.of(hiddenPackages)); + this(Collections.singleton(PackageFilter.of(hiddenPackages)), + Collections.emptyList()); } /** - * Create a {@link FilteredClassLoader} that hides resources from the given packages. + * Create a {@link FilteredClassLoader} that hides resources from the given + * {@link ClassPathResource classpath resources}. * @param hiddenResources the resources to hide */ public FilteredClassLoader(ClassPathResource... hiddenResources) { - this(ClassPathResourceFilter.of(hiddenResources)); + this(Collections.emptyList(), + Collections.singleton(ClassPathResourceFilter.of(hiddenResources))); } /** * Create a {@link FilteredClassLoader} that filters based on the given predicate. * @param filters a set of filters to determine when a class name or resource should * be hidden. A {@link Predicate#test(Object) result} of {@code true} indicates a - * filtered class or resource. + * filtered class or resource. The input of the predicate can either be the binary + * name of a class or a resource name. */ @SafeVarargs public FilteredClassLoader(Predicate... filters) { + this(Arrays.asList(filters), Arrays.asList(filters)); + } + + private FilteredClassLoader(Collection> classesFilters, + Collection> resourcesFilters) { super(new URL[0], FilteredClassLoader.class.getClassLoader()); - this.filters = filters; + this.classesFilters = classesFilters; + this.resourcesFilters = resourcesFilters; } @Override protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { - for (Predicate filter : this.filters) { + for (Predicate filter : this.classesFilters) { if (filter.test(name)) { throw new ClassNotFoundException(); } @@ -84,7 +100,7 @@ public class FilteredClassLoader extends URLClassLoader { @Override public URL getResource(String name) { - for (Predicate filter : this.filters) { + for (Predicate filter : this.resourcesFilters) { if (filter.test(name)) { return null; } diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/FilteredClassLoaderTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/FilteredClassLoaderTests.java index 6034e75e4f..f297c66676 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/FilteredClassLoaderTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/FilteredClassLoaderTests.java @@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; public class FilteredClassLoaderTests { private static ClassPathResource TEST_RESOURCE = new ClassPathResource( - "org/springframework/boot/test/context/FilteredClassLoaderTestResource.txt"); + "org/springframework/boot/test/context/FilteredClassLoaderTestsResource.txt"); @Test public void loadClassWhenFilteredOnPackageShouldThrowClassNotFound() diff --git a/spring-boot-project/spring-boot-test/src/test/resources/org/springframework/boot/test/context/FilteredClassLoaderTestResource.txt b/spring-boot-project/spring-boot-test/src/test/resources/org/springframework/boot/test/context/FilteredClassLoaderTestsResource.txt similarity index 100% rename from spring-boot-project/spring-boot-test/src/test/resources/org/springframework/boot/test/context/FilteredClassLoaderTestResource.txt rename to spring-boot-project/spring-boot-test/src/test/resources/org/springframework/boot/test/context/FilteredClassLoaderTestsResource.txt