From 2f05a7c950b81865b0feb788309a4c13ddff05e0 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Mon, 15 Jan 2024 17:55:01 +0100 Subject: [PATCH] Revert "Harmonize application of method and field filters in search algorithms" This reverts commit a670d107cde5818f9cb5adfae94300f740d13fe8. See #3534 See #3553 See #3600 --- .../commons/util/ReflectionUtils.java | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java index 7339361241d7..271cdf8da08a 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java @@ -1354,11 +1354,11 @@ private static Optional findMethod(Class clazz, Predicate pre for (Class current = clazz; isSearchable(current); current = current.getSuperclass()) { // Search for match in current type - List methods = current.isInterface() ? getMethods(current, predicate) - : getDeclaredMethods(current, predicate, BOTTOM_UP); - if (!methods.isEmpty()) { - // Since the predicate has already been applied, return the first match. - return Optional.of(methods.get(0)); + List methods = current.isInterface() ? getMethods(current) : getDeclaredMethods(current, BOTTOM_UP); + for (Method method : methods) { + if (predicate.test(method)) { + return Optional.of(method); + } } // Search for match in interfaces implemented by current type @@ -1453,8 +1453,8 @@ private static List findAllMethodsInHierarchy(Class clazz, Predicate< Preconditions.notNull(traversalMode, "HierarchyTraversalMode must not be null"); // @formatter:off - List localMethods = getDeclaredMethods(clazz, predicate, traversalMode).stream() - .filter(method -> !method.isSynthetic()) + List localMethods = getDeclaredMethods(clazz, traversalMode).stream() + .filter(predicate.and(method -> !method.isSynthetic())) .collect(toList()); List superclassMethods = getSuperclassMethods(clazz, predicate, traversalMode).stream() .filter(method -> !isMethodShadowedByLocalMethods(method, localMethods)) @@ -1495,26 +1495,24 @@ private static List getDeclaredFields(Class clazz) { /** * Custom alternative to {@link Class#getMethods()} that sorts the methods - * which match the supplied predicate and converts them to a mutable list. + * and converts them to a mutable list. */ - private static List getMethods(Class clazz, Predicate predicate) { - return toSortedMutableList(clazz.getMethods(), predicate); + private static List getMethods(Class clazz) { + return toSortedMutableList(clazz.getMethods()); } /** * Custom alternative to {@link Class#getDeclaredMethods()} that sorts the - * methods which match the supplied predicate and converts them to a mutable list. + * methods and converts them to a mutable list. * *

In addition, the list returned by this method includes interface * default methods which are either prepended or appended to the list of * declared methods depending on the supplied traversal mode. */ - private static List getDeclaredMethods(Class clazz, Predicate predicate, - HierarchyTraversalMode traversalMode) { - + private static List getDeclaredMethods(Class clazz, HierarchyTraversalMode traversalMode) { // Note: getDefaultMethods() already sorts the methods, - List defaultMethods = getDefaultMethods(clazz, predicate); - List declaredMethods = toSortedMutableList(clazz.getDeclaredMethods(), predicate); + List defaultMethods = getDefaultMethods(clazz); + List declaredMethods = toSortedMutableList(clazz.getDeclaredMethods()); // Take the traversal mode into account in order to retain the inherited // nature of interface default methods. @@ -1531,23 +1529,23 @@ private static List getDeclaredMethods(Class clazz, Predicate /** * Get a sorted, mutable list of all default methods present in interfaces * implemented by the supplied class which are also visible within - * the supplied class and match the supplied predicate. + * the supplied class. * * @see Method Visibility * in the Java Language Specification */ - private static List getDefaultMethods(Class clazz, Predicate predicate) { + private static List getDefaultMethods(Class clazz) { // @formatter:off // Visible default methods are interface default methods that have not // been overridden. List visibleDefaultMethods = Arrays.stream(clazz.getMethods()) - .filter(predicate.and(Method::isDefault)) + .filter(Method::isDefault) .collect(toCollection(ArrayList::new)); if (visibleDefaultMethods.isEmpty()) { return visibleDefaultMethods; } return Arrays.stream(clazz.getInterfaces()) - .map(ifc -> getMethods(ifc, predicate)) + .map(ReflectionUtils::getMethods) .flatMap(List::stream) .filter(visibleDefaultMethods::contains) .collect(toCollection(ArrayList::new)); @@ -1563,10 +1561,9 @@ private static List toSortedMutableList(Field[] fields) { // @formatter:on } - private static List toSortedMutableList(Method[] methods, Predicate predicate) { + private static List toSortedMutableList(Method[] methods) { // @formatter:off return Arrays.stream(methods) - .filter(predicate) .sorted(ReflectionUtils::defaultMethodSorter) // Use toCollection() instead of toList() to ensure list is mutable. .collect(toCollection(ArrayList::new)); @@ -1605,8 +1602,8 @@ private static List getInterfaceMethods(Class clazz, Predicate ifc : clazz.getInterfaces()) { // @formatter:off - List localInterfaceMethods = getMethods(ifc, predicate).stream() - .filter(method -> !isAbstract(method)) + List localInterfaceMethods = getMethods(ifc).stream() + .filter(predicate.and(method -> !isAbstract(method))) .collect(toList()); List superinterfaceMethods = getInterfaceMethods(ifc, predicate, traversalMode).stream()