@@ -1407,11 +1407,11 @@ private static Optional<Method> findMethod(Class<?> clazz, Predicate<Method> pre
14071407
14081408 for (Class <?> current = clazz ; isSearchable (current ); current = current .getSuperclass ()) {
14091409 // Search for match in current type
1410- List <Method > methods = current .isInterface () ? getMethods (current ) : getDeclaredMethods ( current , BOTTOM_UP );
1411- for ( Method method : methods ) {
1412- if (predicate . test ( method )) {
1413- return Optional . of ( method );
1414- }
1410+ List <Method > methods = current .isInterface () ? getMethods (current , predicate )
1411+ : getDeclaredMethods ( current , predicate , BOTTOM_UP );
1412+ if (! methods . isEmpty ( )) {
1413+ // Since the predicate has already been applied, return the first match.
1414+ return Optional . of ( methods . get ( 0 ));
14151415 }
14161416
14171417 // Search for match in interfaces implemented by current type
@@ -1506,8 +1506,8 @@ private static List<Method> findAllMethodsInHierarchy(Class<?> clazz, Predicate<
15061506 Preconditions .notNull (traversalMode , "HierarchyTraversalMode must not be null" );
15071507
15081508 // @formatter:off
1509- List <Method > localMethods = getDeclaredMethods (clazz , traversalMode ).stream ()
1510- .filter (predicate . and ( method -> !method .isSynthetic () ))
1509+ List <Method > localMethods = getDeclaredMethods (clazz , predicate , traversalMode ).stream ()
1510+ .filter (method -> !method .isSynthetic ())
15111511 .collect (toList ());
15121512 List <Method > superclassMethods = getSuperclassMethods (clazz , predicate , traversalMode ).stream ()
15131513 .filter (method -> !isMethodShadowedByLocalMethods (method , localMethods ))
@@ -1533,7 +1533,6 @@ private static List<Method> findAllMethodsInHierarchy(Class<?> clazz, Predicate<
15331533 /**
15341534 * Custom alternative to {@link Class#getFields()} that sorts the fields
15351535 * which match the supplied predicate and converts them to a mutable list.
1536- * @param predicate the field filter; never {@code null}
15371536 */
15381537 private static List <Field > getFields (Class <?> clazz , Predicate <Field > predicate ) {
15391538 return toSortedMutableList (clazz .getFields (), predicate );
@@ -1542,32 +1541,33 @@ private static List<Field> getFields(Class<?> clazz, Predicate<Field> predicate)
15421541 /**
15431542 * Custom alternative to {@link Class#getDeclaredFields()} that sorts the
15441543 * fields which match the supplied predicate and converts them to a mutable list.
1545- * @param predicate the field filter; never {@code null}
15461544 */
15471545 private static List <Field > getDeclaredFields (Class <?> clazz , Predicate <Field > predicate ) {
15481546 return toSortedMutableList (clazz .getDeclaredFields (), predicate );
15491547 }
15501548
15511549 /**
15521550 * Custom alternative to {@link Class#getMethods()} that sorts the methods
1553- * and converts them to a mutable list.
1551+ * which match the supplied predicate and converts them to a mutable list.
15541552 */
1555- private static List <Method > getMethods (Class <?> clazz ) {
1556- return toSortedMutableList (clazz .getMethods ());
1553+ private static List <Method > getMethods (Class <?> clazz , Predicate < Method > predicate ) {
1554+ return toSortedMutableList (clazz .getMethods (), predicate );
15571555 }
15581556
15591557 /**
15601558 * Custom alternative to {@link Class#getDeclaredMethods()} that sorts the
1561- * methods and converts them to a mutable list.
1559+ * methods which match the supplied predicate and converts them to a mutable list.
15621560 *
15631561 * <p>In addition, the list returned by this method includes interface
15641562 * default methods which are either prepended or appended to the list of
15651563 * declared methods depending on the supplied traversal mode.
15661564 */
1567- private static List <Method > getDeclaredMethods (Class <?> clazz , HierarchyTraversalMode traversalMode ) {
1565+ private static List <Method > getDeclaredMethods (Class <?> clazz , Predicate <Method > predicate ,
1566+ HierarchyTraversalMode traversalMode ) {
1567+
15681568 // Note: getDefaultMethods() already sorts the methods,
1569- List <Method > defaultMethods = getDefaultMethods (clazz );
1570- List <Method > declaredMethods = toSortedMutableList (clazz .getDeclaredMethods ());
1569+ List <Method > defaultMethods = getDefaultMethods (clazz , predicate );
1570+ List <Method > declaredMethods = toSortedMutableList (clazz .getDeclaredMethods (), predicate );
15711571
15721572 // Take the traversal mode into account in order to retain the inherited
15731573 // nature of interface default methods.
@@ -1584,23 +1584,23 @@ private static List<Method> getDeclaredMethods(Class<?> clazz, HierarchyTraversa
15841584 /**
15851585 * Get a sorted, mutable list of all default methods present in interfaces
15861586 * implemented by the supplied class which are also <em>visible</em> within
1587- * the supplied class.
1587+ * the supplied class and match the supplied predicate .
15881588 *
15891589 * @see <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#d5e9652">Method Visibility</a>
15901590 * in the Java Language Specification
15911591 */
1592- private static List <Method > getDefaultMethods (Class <?> clazz ) {
1592+ private static List <Method > getDefaultMethods (Class <?> clazz , Predicate < Method > predicate ) {
15931593 // @formatter:off
15941594 // Visible default methods are interface default methods that have not
15951595 // been overridden.
15961596 List <Method > visibleDefaultMethods = Arrays .stream (clazz .getMethods ())
1597- .filter (Method ::isDefault )
1597+ .filter (predicate . and ( Method ::isDefault ) )
15981598 .collect (toCollection (ArrayList ::new ));
15991599 if (visibleDefaultMethods .isEmpty ()) {
16001600 return visibleDefaultMethods ;
16011601 }
16021602 return Arrays .stream (clazz .getInterfaces ())
1603- .map (ReflectionUtils :: getMethods )
1603+ .map (ifc -> getMethods ( ifc , predicate ) )
16041604 .flatMap (List ::stream )
16051605 .filter (visibleDefaultMethods ::contains )
16061606 .collect (toCollection (ArrayList ::new ));
@@ -1617,9 +1617,10 @@ private static List<Field> toSortedMutableList(Field[] fields, Predicate<Field>
16171617 // @formatter:on
16181618 }
16191619
1620- private static List <Method > toSortedMutableList (Method [] methods ) {
1620+ private static List <Method > toSortedMutableList (Method [] methods , Predicate < Method > predicate ) {
16211621 // @formatter:off
16221622 return Arrays .stream (methods )
1623+ .filter (predicate )
16231624 .sorted (ReflectionUtils ::defaultMethodSorter )
16241625 // Use toCollection() instead of toList() to ensure list is mutable.
16251626 .collect (toCollection (ArrayList ::new ));
@@ -1658,8 +1659,8 @@ private static List<Method> getInterfaceMethods(Class<?> clazz, Predicate<Method
16581659 for (Class <?> ifc : clazz .getInterfaces ()) {
16591660
16601661 // @formatter:off
1661- List <Method > localInterfaceMethods = getMethods (ifc ).stream ()
1662- .filter (predicate . and ( method -> !isAbstract (method ) ))
1662+ List <Method > localInterfaceMethods = getMethods (ifc , predicate ).stream ()
1663+ .filter (method -> !isAbstract (method ))
16631664 .collect (toList ());
16641665
16651666 List <Method > superinterfaceMethods = getInterfaceMethods (ifc , predicate , traversalMode ).stream ()
0 commit comments