Introduce and() methods in MethodFilter & FieldFilter for composition
This commit introduces `and()` default methods in the MethodFilter and FieldFilter functional interfaces in ReflectionUtils in order to simplify uses cases that need to compose filter logic. Closes gh-26063
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2019 the original author or authors.
|
* Copyright 2002-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -824,6 +824,18 @@ public abstract class ReflectionUtils {
|
|||||||
* @param method the method to check
|
* @param method the method to check
|
||||||
*/
|
*/
|
||||||
boolean matches(Method method);
|
boolean matches(Method method);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a composite filter based on this filter <em>and</em> the provided
|
||||||
|
* filter.
|
||||||
|
* <p>If this filter does not match, the next filter will not be applied.
|
||||||
|
* @param next the next {@code MethodFilter}
|
||||||
|
* @return a composite {@code MethodFilter}
|
||||||
|
* @since 5.3.2
|
||||||
|
*/
|
||||||
|
default MethodFilter and(MethodFilter next) {
|
||||||
|
return method -> matches(method) && next.matches(method);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -852,6 +864,18 @@ public abstract class ReflectionUtils {
|
|||||||
* @param field the field to check
|
* @param field the field to check
|
||||||
*/
|
*/
|
||||||
boolean matches(Field field);
|
boolean matches(Field field);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a composite filter based on this filter <em>and</em> the provided
|
||||||
|
* filter.
|
||||||
|
* <p>If this filter does not match, the next filter will not be applied.
|
||||||
|
* @param next the next {@code FieldFilter}
|
||||||
|
* @return a composite {@code FieldFilter}
|
||||||
|
* @since 5.3.2
|
||||||
|
*/
|
||||||
|
default FieldFilter and(FieldFilter next) {
|
||||||
|
return field -> matches(field) && next.matches(field);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -186,12 +186,7 @@ class ReflectionUtilsTests {
|
|||||||
@Test
|
@Test
|
||||||
void doWithProtectedMethods() {
|
void doWithProtectedMethods() {
|
||||||
ListSavingMethodCallback mc = new ListSavingMethodCallback();
|
ListSavingMethodCallback mc = new ListSavingMethodCallback();
|
||||||
ReflectionUtils.doWithMethods(TestObject.class, mc, new ReflectionUtils.MethodFilter() {
|
ReflectionUtils.doWithMethods(TestObject.class, mc, method -> Modifier.isProtected(method.getModifiers()));
|
||||||
@Override
|
|
||||||
public boolean matches(Method m) {
|
|
||||||
return Modifier.isProtected(m.getModifiers());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
assertThat(mc.getMethodNames().isEmpty()).isFalse();
|
assertThat(mc.getMethodNames().isEmpty()).isFalse();
|
||||||
assertThat(mc.getMethodNames().contains("clone")).as("Must find protected method on Object").isTrue();
|
assertThat(mc.getMethodNames().contains("clone")).as("Must find protected method on Object").isTrue();
|
||||||
assertThat(mc.getMethodNames().contains("finalize")).as("Must find protected method on Object").isTrue();
|
assertThat(mc.getMethodNames().contains("finalize")).as("Must find protected method on Object").isTrue();
|
||||||
|
|||||||
@@ -98,10 +98,10 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|||||||
private static final List<Class<? extends Annotation>> JUPITER_ANNOTATION_TYPES =
|
private static final List<Class<? extends Annotation>> JUPITER_ANNOTATION_TYPES =
|
||||||
Arrays.asList(BeforeAll.class, AfterAll.class, BeforeEach.class, AfterEach.class, Testable.class);
|
Arrays.asList(BeforeAll.class, AfterAll.class, BeforeEach.class, AfterEach.class, Testable.class);
|
||||||
|
|
||||||
private static final MethodFilter autowiredTestOrLifecycleMethodFilter = method ->
|
private static final MethodFilter autowiredTestOrLifecycleMethodFilter =
|
||||||
(ReflectionUtils.USER_DECLARED_METHODS.matches(method) &&
|
ReflectionUtils.USER_DECLARED_METHODS
|
||||||
!Modifier.isPrivate(method.getModifiers()) &&
|
.and(method -> !Modifier.isPrivate(method.getModifiers()))
|
||||||
isAutowiredTestOrLifecycleMethod(method));
|
.and(SpringExtension::isAutowiredTestOrLifecycleMethod);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user