Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
02ef108
Support scanning for class path resources
mpkorstanje Jan 9, 2024
2f76493
Formatting
mpkorstanje Feb 25, 2024
277272a
Doc
mpkorstanje Feb 25, 2024
f683deb
Fix
mpkorstanje Feb 25, 2024
c457b58
Clean up todos
mpkorstanje Feb 29, 2024
c9b3b0e
Clean up todos
mpkorstanje Feb 29, 2024
e5a1751
Clean up
mpkorstanje Mar 7, 2024
2bab069
Extract common code in findClassesForPath and findResourcesForPath
mpkorstanje Mar 7, 2024
f34c4c9
Extract common file visitor code
mpkorstanje Mar 7, 2024
c84ad5e
Docs
mpkorstanje Mar 7, 2024
e43258e
Tests
mpkorstanje Mar 7, 2024
c374b93
Spotless
mpkorstanje Mar 7, 2024
440e0e7
Merge remote-tracking branch 'origin/main' into support-classpath-res…
mpkorstanje Mar 7, 2024
6cca62a
Update CHANGELOG
mpkorstanje Mar 7, 2024
704e584
Update CHANGELOG
mpkorstanje Mar 7, 2024
9f99477
Remove resource name filter predicate
mpkorstanje Jun 13, 2024
2df5201
Clean up to do.
mpkorstanje Jun 13, 2024
862cb3b
Merge remote-tracking branch 'origin/main' into support-classpath-res…
mpkorstanje Jun 13, 2024
f5d892b
Replace resource filter with predicate
mpkorstanje Jun 14, 2024
6e95eeb
Revert "Replace resource filter with predicate"
mpkorstanje Jun 14, 2024
e59cc59
Revert "Remove resource name filter predicate"
mpkorstanje Jun 14, 2024
767dcf4
Load canonical resources through classloader
mpkorstanje Jun 14, 2024
94215bc
Touch up
mpkorstanje Jun 14, 2024
794793a
Correctly resolve shadowed resources
mpkorstanje Jun 14, 2024
ac77e69
Fixup changelog
mpkorstanje Jun 14, 2024
79f13bf
Fix up java doc
mpkorstanje Jun 14, 2024
cdeb3ea
Merge remote-tracking branch 'origin/main' into support-classpath-res…
mpkorstanje Jun 14, 2024
966e21f
Fix tryToLoadResource
mpkorstanje Jun 15, 2024
358693f
Wrap class loader call in try
mpkorstanje Jun 15, 2024
917f004
Add more tryToLoadResource tests
mpkorstanje Jun 15, 2024
ca1f81b
Touchups
mpkorstanje Jun 15, 2024
59705ba
Merge remote-tracking branch 'origin/main' into support-classpath-res…
mpkorstanje Jun 15, 2024
7ffff49
Remove unnesesary try-catch
mpkorstanje Jun 15, 2024
2144abe
Add more reflection support tests
mpkorstanje Jun 15, 2024
27c0750
Merge branch 'main' into support-classpath-resources-scanning
mpkorstanje Jun 17, 2024
52e5884
Merge remote-tracking branch 'origin/main' into support-classpath-res…
mpkorstanje Jun 26, 2024
7df46a3
Find all available resources, including duplicates
mpkorstanje Jul 8, 2024
ee7b42a
Remove unused tryToLoadResource
mpkorstanje Jul 8, 2024
c0c9ee6
Suppress warnings
mpkorstanje Jul 8, 2024
8621935
Remove resourceNameFilter predicate from Javadoc
marcphilipp Jul 9, 2024
3a14696
Add test for finding duplicate resources
marcphilipp Jul 9, 2024
3f348b3
Merge branch 'main' into support-classpath-resources-scanning
marcphilipp Jul 9, 2024
b826b32
Clean up unused import
mpkorstanje Jul 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove resource name filter predicate
  • Loading branch information
mpkorstanje committed Jun 13, 2024
commit 9f99477c6ba8bda8a06edefb55492c405b34780d
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,14 @@ public static List<Class<?>> findAllClassesInClasspathRoot(URI root, Predicate<C
* @param root the URI for the classpath root in which to scan; never
* {@code null}
* @param resourceFilter the resource type filter; never {@code null}
* @param resourceNameFilter the resource name filter; never {@code null}
* @return an immutable list of all such resources found; never {@code null}
* but potentially empty
* @see #findAllResourcesInPackage(String, Predicate, Predicate)
* @see #findAllResourcesInModule(String, Predicate, Predicate)
* @see #findAllResourcesInPackage(String, Predicate)
* @see #findAllResourcesInModule(String, Predicate)
*/
@API(status = EXPERIMENTAL, since = "1.11")
public static List<Resource> findAllResourcesInClasspathRoot(URI root, Predicate<Resource> resourceFilter,
Predicate<String> resourceNameFilter) {

return ReflectionUtils.findAllResourcesInClasspathRoot(root, resourceFilter, resourceNameFilter);
public static List<Resource> findAllResourcesInClasspathRoot(URI root, Predicate<Resource> resourceFilter) {
return ReflectionUtils.findAllResourcesInClasspathRoot(root, resourceFilter);
}

/**
Expand Down Expand Up @@ -196,18 +193,15 @@ public static Stream<Class<?>> streamAllClassesInClasspathRoot(URI root, Predica
* @param root the URI for the classpath root in which to scan; never
* {@code null}
* @param resourceFilter the resource type filter; never {@code null}
* @param resourceNameFilter the resources name filter; never {@code null}
* @return a stream of all such classes found; never {@code null}
* but potentially empty
* @since 1.10
* @see #streamAllResourcesInPackage(String, Predicate, Predicate)
* @see #streamAllResourcesInModule(String, Predicate, Predicate)
* @see #streamAllResourcesInPackage(String, Predicate)
* @see #streamAllResourcesInModule(String, Predicate)
*/
@API(status = EXPERIMENTAL, since = "1.11")
public static Stream<Resource> streamAllResourcesInClasspathRoot(URI root, Predicate<Resource> resourceFilter,
Predicate<String> resourceNameFilter) {

return ReflectionUtils.streamAllResourcesInClasspathRoot(root, resourceFilter, resourceNameFilter);
public static Stream<Resource> streamAllResourcesInClasspathRoot(URI root, Predicate<Resource> resourceFilter) {
return ReflectionUtils.streamAllResourcesInClasspathRoot(root, resourceFilter);
}

/**
Expand All @@ -230,7 +224,6 @@ public static Stream<Resource> streamAllResourcesInClasspathRoot(URI root, Predi
*/
public static List<Class<?>> findAllClassesInPackage(String basePackageName, Predicate<Class<?>> classFilter,
Predicate<String> classNameFilter) {

return ReflectionUtils.findAllClassesInPackage(basePackageName, classFilter, classNameFilter);
}

Expand All @@ -246,17 +239,14 @@ public static List<Class<?>> findAllClassesInPackage(String basePackageName, Pre
* scanning; must not be {@code null} and must be valid in terms of Java
* syntax
* @param resourceFilter the resource type filter; never {@code null}
* @param resourceNameFilter the resource name filter; never {@code null}
* @return an immutable list of all such classes found; never {@code null}
* but potentially empty
* @see #findAllResourcesInClasspathRoot(URI, Predicate, Predicate)
* @see #findAllResourcesInModule(String, Predicate, Predicate)
* @see #findAllResourcesInClasspathRoot(URI, Predicate)
* @see #findAllResourcesInModule(String, Predicate)
*/
@API(status = EXPERIMENTAL, since = "1.11")
public static List<Resource> findAllResourcesInPackage(String basePackageName, Predicate<Resource> resourceFilter,
Predicate<String> resourceNameFilter) {

return ReflectionUtils.findAllResourcesInPackage(basePackageName, resourceFilter, resourceNameFilter);
public static List<Resource> findAllResourcesInPackage(String basePackageName, Predicate<Resource> resourceFilter) {
return ReflectionUtils.findAllResourcesInPackage(basePackageName, resourceFilter);
}

/**
Expand Down Expand Up @@ -297,18 +287,16 @@ public static Stream<Class<?>> streamAllClassesInPackage(String basePackageName,
* scanning; must not be {@code null} and must be valid in terms of Java
* syntax
* @param resourceFilter the resource type filter; never {@code null}
* @param resourceNameFilter the resource name filter; never {@code null}
* @return a stream of all such resources found; never {@code null}
* but potentially empty
* @since 1.10
* @see #streamAllResourcesInClasspathRoot(URI, Predicate, Predicate)
* @see #streamAllResourcesInModule(String, Predicate, Predicate)
* @see #streamAllResourcesInClasspathRoot(URI, Predicate)
* @see #streamAllResourcesInModule(String, Predicate)
*/
@API(status = EXPERIMENTAL, since = "1.11")
public static Stream<Resource> streamAllResourcesInPackage(String basePackageName,
Predicate<Resource> resourceFilter, Predicate<String> resourceNameFilter) {

return ReflectionUtils.streamAllResourcesInPackage(basePackageName, resourceFilter, resourceNameFilter);
Predicate<Resource> resourceFilter) {
return ReflectionUtils.streamAllResourcesInPackage(basePackageName, resourceFilter);
}

/**
Expand Down Expand Up @@ -346,18 +334,15 @@ public static List<Class<?>> findAllClassesInModule(String moduleName, Predicate
* @param moduleName the name of the module to scan; never {@code null} or
* <em>empty</em>
* @param resourceFilter the resource type filter; never {@code null}
* @param resourceNameFilter the resource name filter; never {@code null}
* @return an immutable list of all such resources found; never {@code null}
* but potentially empty
* @since 1.1.1
* @see #findAllResourcesInClasspathRoot(URI, Predicate, Predicate)
* @see #findAllResourcesInPackage(String, Predicate, Predicate)
* @see #findAllResourcesInClasspathRoot(URI, Predicate)
* @see #findAllResourcesInPackage(String, Predicate)
*/
@API(status = EXPERIMENTAL, since = "1.11")
public static List<Resource> findAllResourcesInModule(String moduleName, Predicate<Resource> resourceFilter,
Predicate<String> resourceNameFilter) {

return ReflectionUtils.findAllResourcesInModule(moduleName, resourceFilter, resourceNameFilter);
public static List<Resource> findAllResourcesInModule(String moduleName, Predicate<Resource> resourceFilter) {
return ReflectionUtils.findAllResourcesInModule(moduleName, resourceFilter);
}

/**
Expand Down Expand Up @@ -396,18 +381,15 @@ public static Stream<Class<?>> streamAllClassesInModule(String moduleName, Predi
* @param moduleName the name of the module to scan; never {@code null} or
* <em>empty</em>
* @param resourceFilter the resource type filter; never {@code null}
* @param resourceNameFilter the resource name filter; never {@code null}
* @return a stream of all such resources found; never {@code null}
* but potentially empty
* @since 1.10
* @see #streamAllResourcesInClasspathRoot(URI, Predicate, Predicate)
* @see #streamAllResourcesInPackage(String, Predicate, Predicate)
* @see #streamAllResourcesInClasspathRoot(URI, Predicate)
* @see #streamAllResourcesInPackage(String, Predicate)
*/
@API(status = EXPERIMENTAL, since = "1.11")
public static Stream<Resource> streamAllResourcesInModule(String moduleName, Predicate<Resource> resourceFilter,
Predicate<String> resourceNameFilter) {

return ReflectionUtils.streamAllResourcesInModule(moduleName, resourceFilter, resourceNameFilter);
public static Stream<Resource> streamAllResourcesInModule(String moduleName, Predicate<Resource> resourceFilter) {
return ReflectionUtils.streamAllResourcesInModule(moduleName, resourceFilter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
/**
* Represents a resource on the classpath.
*
* @see ReflectionSupport#findAllResourcesInClasspathRoot(URI, Predicate, Predicate)
* @see ReflectionSupport#findAllResourcesInPackage(String, Predicate, Predicate)
* @see ReflectionSupport#findAllResourcesInModule(String, Predicate, Predicate)
* @see ReflectionSupport#streamAllResourcesInClasspathRoot(URI, Predicate, Predicate)
* @see ReflectionSupport#streamAllResourcesInPackage(String, Predicate, Predicate)
* @see ReflectionSupport#streamAllResourcesInModule(String, Predicate, Predicate)
* @see ReflectionSupport#findAllResourcesInClasspathRoot(URI, Predicate)
* @see ReflectionSupport#findAllResourcesInPackage(String, Predicate)
* @see ReflectionSupport#findAllResourcesInModule(String, Predicate)
* @see ReflectionSupport#streamAllResourcesInClasspathRoot(URI, Predicate)
* @see ReflectionSupport#streamAllResourcesInPackage(String, Predicate)
* @see ReflectionSupport#streamAllResourcesInModule(String, Predicate)
*/
@API(status = EXPERIMENTAL, since = "1.11")
public interface Resource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,10 @@ private void processResourceFileSafely(Path baseDir, String basePackageName, Res
try {
String fullyQualifiedResourceName = determineFullyQualifiedResourceName(baseDir, basePackageName,
resourceFile);
if (resourceFilter.match(fullyQualifiedResourceName)) {
Resource resource = new ClasspathResource(fullyQualifiedResourceName, resourceFile.toUri());
// Always use "resourceFilter.test" to include future predicates.
if (resourceFilter.test(resource)) {
resourceConsumer.accept(resource);
}
Resource resource = new ClasspathResource(fullyQualifiedResourceName, resourceFile.toUri());
// Always use "resourceFilter.test" to include future predicates.
if (resourceFilter.test(resource)) {
resourceConsumer.accept(resource);
}
}
catch (Throwable throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,12 +964,11 @@ public static List<Class<?>> findAllClassesInClasspathRoot(URI root, Predicate<C

/**
* @since 1.11
* @see org.junit.platform.commons.support.ReflectionSupport#findAllResourcesInClasspathRoot(URI, Predicate, Predicate)
* @see #findAllResourcesInClasspathRoot(URI, Predicate)
*/
public static List<Resource> findAllResourcesInClasspathRoot(URI root, Predicate<Resource> resourceFilter,
Predicate<String> resourceNameFilter) {
public static List<Resource> findAllResourcesInClasspathRoot(URI root, Predicate<Resource> resourceFilter) {
// unmodifiable since returned by public, non-internal method(s)
return findAllResourcesInClasspathRoot(root, ResourceFilter.of(resourceNameFilter, resourceFilter));
return findAllResourcesInClasspathRoot(root, ResourceFilter.of(resourceFilter));
}

/**
Expand All @@ -983,11 +982,10 @@ public static Stream<Class<?>> streamAllClassesInClasspathRoot(URI root, Predica

/**
* @since 1.11
* @see org.junit.platform.commons.support.ReflectionSupport#streamAllResourcesInClasspathRoot(URI, Predicate, Predicate)
* @see #streamAllResourcesInClasspathRoot(URI, Predicate)
*/
public static Stream<Resource> streamAllResourcesInClasspathRoot(URI root, Predicate<Resource> resourceFilter,
Predicate<String> resourceNameFilter) {
return streamAllResourcesInClasspathRoot(root, ResourceFilter.of(resourceNameFilter, resourceFilter));
public static Stream<Resource> streamAllResourcesInClasspathRoot(URI root, Predicate<Resource> resourceFilter) {
return streamAllResourcesInClasspathRoot(root, ResourceFilter.of(resourceFilter));
}

/**
Expand All @@ -1000,8 +998,8 @@ public static List<Class<?>> findAllClassesInClasspathRoot(URI root, ClassFilter
/**
* @since 1.11
*/
public static List<Resource> findAllResourcesInClasspathRoot(URI root, ResourceFilter classFilter) {
return Collections.unmodifiableList(classpathScanner.scanForResourcesInClasspathRoot(root, classFilter));
public static List<Resource> findAllResourcesInClasspathRoot(URI root, ResourceFilter resourceFilter) {
return Collections.unmodifiableList(classpathScanner.scanForResourcesInClasspathRoot(root, resourceFilter));
}

/**
Expand Down Expand Up @@ -1031,10 +1029,9 @@ public static List<Class<?>> findAllClassesInPackage(String basePackageName, Pre
* @since 1.11
* @see org.junit.platform.commons.support.ReflectionSupport#findAllClassesInPackage(String, Predicate, Predicate)
*/
public static List<Resource> findAllResourcesInPackage(String basePackageName, Predicate<Resource> resourceFilter,
Predicate<String> resourceNameFilter) {
public static List<Resource> findAllResourcesInPackage(String basePackageName, Predicate<Resource> resourceFilter) {
// unmodifiable since returned by public, non-internal method(s)
return findAllResourcesInPackage(basePackageName, ResourceFilter.of(resourceNameFilter, resourceFilter));
return findAllResourcesInPackage(basePackageName, ResourceFilter.of(resourceFilter));
}

/**
Expand All @@ -1048,11 +1045,11 @@ public static Stream<Class<?>> streamAllClassesInPackage(String basePackageName,

/**
* since 1.11
* @see org.junit.platform.commons.support.ReflectionSupport#streamAllResourcesInPackage(String, Predicate, Predicate)
* @see #streamAllResourcesInPackage(String, Predicate)
*/
public static Stream<Resource> streamAllResourcesInPackage(String basePackageName,
Predicate<Resource> resourceFilter, Predicate<String> resourceNameFilter) {
return streamAllResourcesInPackage(basePackageName, ResourceFilter.of(resourceNameFilter, resourceFilter));
Predicate<Resource> resourceFilter) {
return streamAllResourcesInPackage(basePackageName, ResourceFilter.of(resourceFilter));
}

/**
Expand Down Expand Up @@ -1096,12 +1093,11 @@ public static List<Class<?>> findAllClassesInModule(String moduleName, Predicate

/**
* @since 1.11
* @see org.junit.platform.commons.support.ReflectionSupport#findAllResourcesInModule(String, Predicate, Predicate)
* @see #findAllResourcesInModule(String, Predicate)
*/
public static List<Resource> findAllResourcesInModule(String moduleName, Predicate<Resource> resourceFilter,
Predicate<String> resourceNameFilter) {
public static List<Resource> findAllResourcesInModule(String moduleName, Predicate<Resource> resourceFilter) {
// unmodifiable since returned by public, non-internal method(s)
return findAllResourcesInModule(moduleName, ResourceFilter.of(resourceNameFilter, resourceFilter));
return findAllResourcesInModule(moduleName, ResourceFilter.of(resourceFilter));
}

/**
Expand All @@ -1115,11 +1111,10 @@ public static Stream<Class<?>> streamAllClassesInModule(String moduleName, Predi

/**
* @since 1.11
* @see org.junit.platform.commons.support.ReflectionSupport#streamAllResourcesInModule(String, Predicate, Predicate)
* @see #streamAllResourcesInModule(String, Predicate)
*/
public static Stream<Resource> streamAllResourcesInModule(String moduleName, Predicate<Resource> resourceFilter,
Predicate<String> resourceNameFilter) {
return streamAllResourcesInModule(moduleName, ResourceFilter.of(resourceNameFilter, resourceFilter));
public static Stream<Resource> streamAllResourcesInModule(String moduleName, Predicate<Resource> resourceFilter) {
return streamAllResourcesInModule(moduleName, ResourceFilter.of(resourceFilter));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,20 @@
public class ResourceFilter implements Predicate<Resource> {

/**
* Create a {@link ResourceFilter} instance that accepts all names but filters resources.
* Create a {@link ResourceFilter} instance that filters resources.
*/
public static ResourceFilter of(Predicate<Resource> resourcePredicate) {
return of(name -> true, resourcePredicate);
return new ResourceFilter(resourcePredicate);
}

/**
* Create a {@link ResourceFilter} instance that filters by resource names and resources.
*/
public static ResourceFilter of(Predicate<String> namePredicate, Predicate<Resource> resourcePredicate) {
return new ResourceFilter(namePredicate, resourcePredicate);
}

private final Predicate<String> namePredicate;
private final Predicate<Resource> resourcePredicate;

private ResourceFilter(Predicate<String> namePredicate, Predicate<Resource> resourcePredicate) {
this.namePredicate = Preconditions.notNull(namePredicate, "name predicate must not be null");
private ResourceFilter(Predicate<Resource> resourcePredicate) {
this.resourcePredicate = Preconditions.notNull(resourcePredicate, "resource predicate must not be null");
}

public boolean match(String name) {
return namePredicate.test(name);
}

public boolean match(Resource resource) {
return resourcePredicate.test(resource);
}

/**
* @implNote This implementation combines all tests stored in the predicates
* of this instance. Any new predicate must be added to this test method as
* well.
*/
@Override
public boolean test(Resource resource) {
return match(resource.getName()) && match(resource);
return resourcePredicate.test(resource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ List<Resource> scan(ModuleReference reference) {
try (Stream<String> names = reader.list()) {
// @formatter:off
return names.filter(name -> !name.endsWith(".class"))
.filter(resourceFilter::match)
.map(this::loadResourceUnchecked)
// Always use ".filter(resourceFilter)" to include future predicates.
.filter(resourceFilter)
Expand Down
Loading