Skip to content

Commit 2f05a7c

Browse files
committed
Revert "Harmonize application of method and field filters in search algorithms"
This reverts commit a670d10. See #3534 See #3553 See #3600
1 parent 7789d32 commit 2f05a7c

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java

+21-24
Original file line numberDiff line numberDiff line change
@@ -1354,11 +1354,11 @@ private static Optional<Method> findMethod(Class<?> clazz, Predicate<Method> pre
13541354

13551355
for (Class<?> current = clazz; isSearchable(current); current = current.getSuperclass()) {
13561356
// Search for match in current type
1357-
List<Method> methods = current.isInterface() ? getMethods(current, predicate)
1358-
: getDeclaredMethods(current, predicate, BOTTOM_UP);
1359-
if (!methods.isEmpty()) {
1360-
// Since the predicate has already been applied, return the first match.
1361-
return Optional.of(methods.get(0));
1357+
List<Method> methods = current.isInterface() ? getMethods(current) : getDeclaredMethods(current, BOTTOM_UP);
1358+
for (Method method : methods) {
1359+
if (predicate.test(method)) {
1360+
return Optional.of(method);
1361+
}
13621362
}
13631363

13641364
// Search for match in interfaces implemented by current type
@@ -1453,8 +1453,8 @@ private static List<Method> findAllMethodsInHierarchy(Class<?> clazz, Predicate<
14531453
Preconditions.notNull(traversalMode, "HierarchyTraversalMode must not be null");
14541454

14551455
// @formatter:off
1456-
List<Method> localMethods = getDeclaredMethods(clazz, predicate, traversalMode).stream()
1457-
.filter(method -> !method.isSynthetic())
1456+
List<Method> localMethods = getDeclaredMethods(clazz, traversalMode).stream()
1457+
.filter(predicate.and(method -> !method.isSynthetic()))
14581458
.collect(toList());
14591459
List<Method> superclassMethods = getSuperclassMethods(clazz, predicate, traversalMode).stream()
14601460
.filter(method -> !isMethodShadowedByLocalMethods(method, localMethods))
@@ -1495,26 +1495,24 @@ private static List<Field> getDeclaredFields(Class<?> clazz) {
14951495

14961496
/**
14971497
* Custom alternative to {@link Class#getMethods()} that sorts the methods
1498-
* which match the supplied predicate and converts them to a mutable list.
1498+
* and converts them to a mutable list.
14991499
*/
1500-
private static List<Method> getMethods(Class<?> clazz, Predicate<Method> predicate) {
1501-
return toSortedMutableList(clazz.getMethods(), predicate);
1500+
private static List<Method> getMethods(Class<?> clazz) {
1501+
return toSortedMutableList(clazz.getMethods());
15021502
}
15031503

15041504
/**
15051505
* Custom alternative to {@link Class#getDeclaredMethods()} that sorts the
1506-
* methods which match the supplied predicate and converts them to a mutable list.
1506+
* methods and converts them to a mutable list.
15071507
*
15081508
* <p>In addition, the list returned by this method includes interface
15091509
* default methods which are either prepended or appended to the list of
15101510
* declared methods depending on the supplied traversal mode.
15111511
*/
1512-
private static List<Method> getDeclaredMethods(Class<?> clazz, Predicate<Method> predicate,
1513-
HierarchyTraversalMode traversalMode) {
1514-
1512+
private static List<Method> getDeclaredMethods(Class<?> clazz, HierarchyTraversalMode traversalMode) {
15151513
// Note: getDefaultMethods() already sorts the methods,
1516-
List<Method> defaultMethods = getDefaultMethods(clazz, predicate);
1517-
List<Method> declaredMethods = toSortedMutableList(clazz.getDeclaredMethods(), predicate);
1514+
List<Method> defaultMethods = getDefaultMethods(clazz);
1515+
List<Method> declaredMethods = toSortedMutableList(clazz.getDeclaredMethods());
15181516

15191517
// Take the traversal mode into account in order to retain the inherited
15201518
// nature of interface default methods.
@@ -1531,23 +1529,23 @@ private static List<Method> getDeclaredMethods(Class<?> clazz, Predicate<Method>
15311529
/**
15321530
* Get a sorted, mutable list of all default methods present in interfaces
15331531
* implemented by the supplied class which are also <em>visible</em> within
1534-
* the supplied class and match the supplied predicate.
1532+
* the supplied class.
15351533
*
15361534
* @see <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#d5e9652">Method Visibility</a>
15371535
* in the Java Language Specification
15381536
*/
1539-
private static List<Method> getDefaultMethods(Class<?> clazz, Predicate<Method> predicate) {
1537+
private static List<Method> getDefaultMethods(Class<?> clazz) {
15401538
// @formatter:off
15411539
// Visible default methods are interface default methods that have not
15421540
// been overridden.
15431541
List<Method> visibleDefaultMethods = Arrays.stream(clazz.getMethods())
1544-
.filter(predicate.and(Method::isDefault))
1542+
.filter(Method::isDefault)
15451543
.collect(toCollection(ArrayList::new));
15461544
if (visibleDefaultMethods.isEmpty()) {
15471545
return visibleDefaultMethods;
15481546
}
15491547
return Arrays.stream(clazz.getInterfaces())
1550-
.map(ifc -> getMethods(ifc, predicate))
1548+
.map(ReflectionUtils::getMethods)
15511549
.flatMap(List::stream)
15521550
.filter(visibleDefaultMethods::contains)
15531551
.collect(toCollection(ArrayList::new));
@@ -1563,10 +1561,9 @@ private static List<Field> toSortedMutableList(Field[] fields) {
15631561
// @formatter:on
15641562
}
15651563

1566-
private static List<Method> toSortedMutableList(Method[] methods, Predicate<Method> predicate) {
1564+
private static List<Method> toSortedMutableList(Method[] methods) {
15671565
// @formatter:off
15681566
return Arrays.stream(methods)
1569-
.filter(predicate)
15701567
.sorted(ReflectionUtils::defaultMethodSorter)
15711568
// Use toCollection() instead of toList() to ensure list is mutable.
15721569
.collect(toCollection(ArrayList::new));
@@ -1605,8 +1602,8 @@ private static List<Method> getInterfaceMethods(Class<?> clazz, Predicate<Method
16051602
for (Class<?> ifc : clazz.getInterfaces()) {
16061603

16071604
// @formatter:off
1608-
List<Method> localInterfaceMethods = getMethods(ifc, predicate).stream()
1609-
.filter(method -> !isAbstract(method))
1605+
List<Method> localInterfaceMethods = getMethods(ifc).stream()
1606+
.filter(predicate.and(method -> !isAbstract(method)))
16101607
.collect(toList());
16111608

16121609
List<Method> superinterfaceMethods = getInterfaceMethods(ifc, predicate, traversalMode).stream()

0 commit comments

Comments
 (0)