@@ -1405,11 +1405,11 @@ private static Optional<Method> findMethod(Class<?> clazz, Predicate<Method> pre
1405
1405
1406
1406
for (Class <?> current = clazz ; isSearchable (current ); current = current .getSuperclass ()) {
1407
1407
// Search for match in current type
1408
- List <Method > methods = current .isInterface () ? getMethods (current , predicate )
1409
- : getDeclaredMethods ( current , predicate , BOTTOM_UP );
1410
- if (! methods . isEmpty ( )) {
1411
- // Since the predicate has already been applied, return the first match.
1412
- return Optional . of ( methods . get ( 0 ));
1408
+ List <Method > methods = current .isInterface () ? getMethods (current ) : getDeclaredMethods ( current , BOTTOM_UP );
1409
+ for ( Method method : methods ) {
1410
+ if (predicate . test ( method )) {
1411
+ return Optional . of ( method );
1412
+ }
1413
1413
}
1414
1414
1415
1415
// Search for match in interfaces implemented by current type
@@ -1504,8 +1504,8 @@ private static List<Method> findAllMethodsInHierarchy(Class<?> clazz, Predicate<
1504
1504
Preconditions .notNull (traversalMode , "HierarchyTraversalMode must not be null" );
1505
1505
1506
1506
// @formatter:off
1507
- List <Method > localMethods = getDeclaredMethods (clazz , predicate , traversalMode ).stream ()
1508
- .filter (method -> !method .isSynthetic ())
1507
+ List <Method > localMethods = getDeclaredMethods (clazz , traversalMode ).stream ()
1508
+ .filter (predicate . and ( method -> !method .isSynthetic () ))
1509
1509
.collect (toList ());
1510
1510
List <Method > superclassMethods = getSuperclassMethods (clazz , predicate , traversalMode ).stream ()
1511
1511
.filter (method -> !isMethodShadowedByLocalMethods (method , localMethods ))
@@ -1546,26 +1546,24 @@ private static List<Field> getDeclaredFields(Class<?> clazz) {
1546
1546
1547
1547
/**
1548
1548
* Custom alternative to {@link Class#getMethods()} that sorts the methods
1549
- * which match the supplied predicate and converts them to a mutable list.
1549
+ * and converts them to a mutable list.
1550
1550
*/
1551
- private static List <Method > getMethods (Class <?> clazz , Predicate < Method > predicate ) {
1552
- return toSortedMutableList (clazz .getMethods (), predicate );
1551
+ private static List <Method > getMethods (Class <?> clazz ) {
1552
+ return toSortedMutableList (clazz .getMethods ());
1553
1553
}
1554
1554
1555
1555
/**
1556
1556
* Custom alternative to {@link Class#getDeclaredMethods()} that sorts the
1557
- * methods which match the supplied predicate and converts them to a mutable list.
1557
+ * methods and converts them to a mutable list.
1558
1558
*
1559
1559
* <p>In addition, the list returned by this method includes interface
1560
1560
* default methods which are either prepended or appended to the list of
1561
1561
* declared methods depending on the supplied traversal mode.
1562
1562
*/
1563
- private static List <Method > getDeclaredMethods (Class <?> clazz , Predicate <Method > predicate ,
1564
- HierarchyTraversalMode traversalMode ) {
1565
-
1563
+ private static List <Method > getDeclaredMethods (Class <?> clazz , HierarchyTraversalMode traversalMode ) {
1566
1564
// Note: getDefaultMethods() already sorts the methods,
1567
- List <Method > defaultMethods = getDefaultMethods (clazz , predicate );
1568
- List <Method > declaredMethods = toSortedMutableList (clazz .getDeclaredMethods (), predicate );
1565
+ List <Method > defaultMethods = getDefaultMethods (clazz );
1566
+ List <Method > declaredMethods = toSortedMutableList (clazz .getDeclaredMethods ());
1569
1567
1570
1568
// Take the traversal mode into account in order to retain the inherited
1571
1569
// nature of interface default methods.
@@ -1582,23 +1580,23 @@ private static List<Method> getDeclaredMethods(Class<?> clazz, Predicate<Method>
1582
1580
/**
1583
1581
* Get a sorted, mutable list of all default methods present in interfaces
1584
1582
* implemented by the supplied class which are also <em>visible</em> within
1585
- * the supplied class and match the supplied predicate .
1583
+ * the supplied class.
1586
1584
*
1587
1585
* @see <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#d5e9652">Method Visibility</a>
1588
1586
* in the Java Language Specification
1589
1587
*/
1590
- private static List <Method > getDefaultMethods (Class <?> clazz , Predicate < Method > predicate ) {
1588
+ private static List <Method > getDefaultMethods (Class <?> clazz ) {
1591
1589
// @formatter:off
1592
1590
// Visible default methods are interface default methods that have not
1593
1591
// been overridden.
1594
1592
List <Method > visibleDefaultMethods = Arrays .stream (clazz .getMethods ())
1595
- .filter (predicate . and ( Method ::isDefault ) )
1593
+ .filter (Method ::isDefault )
1596
1594
.collect (toCollection (ArrayList ::new ));
1597
1595
if (visibleDefaultMethods .isEmpty ()) {
1598
1596
return visibleDefaultMethods ;
1599
1597
}
1600
1598
return Arrays .stream (clazz .getInterfaces ())
1601
- .map (ifc -> getMethods ( ifc , predicate ) )
1599
+ .map (ReflectionUtils :: getMethods )
1602
1600
.flatMap (List ::stream )
1603
1601
.filter (visibleDefaultMethods ::contains )
1604
1602
.collect (toCollection (ArrayList ::new ));
@@ -1614,10 +1612,9 @@ private static List<Field> toSortedMutableList(Field[] fields) {
1614
1612
// @formatter:on
1615
1613
}
1616
1614
1617
- private static List <Method > toSortedMutableList (Method [] methods , Predicate < Method > predicate ) {
1615
+ private static List <Method > toSortedMutableList (Method [] methods ) {
1618
1616
// @formatter:off
1619
1617
return Arrays .stream (methods )
1620
- .filter (predicate )
1621
1618
.sorted (ReflectionUtils ::defaultMethodSorter )
1622
1619
// Use toCollection() instead of toList() to ensure list is mutable.
1623
1620
.collect (toCollection (ArrayList ::new ));
@@ -1656,8 +1653,8 @@ private static List<Method> getInterfaceMethods(Class<?> clazz, Predicate<Method
1656
1653
for (Class <?> ifc : clazz .getInterfaces ()) {
1657
1654
1658
1655
// @formatter:off
1659
- List <Method > localInterfaceMethods = getMethods (ifc , predicate ).stream ()
1660
- .filter (method -> !isAbstract (method ))
1656
+ List <Method > localInterfaceMethods = getMethods (ifc ).stream ()
1657
+ .filter (predicate . and ( method -> !isAbstract (method ) ))
1661
1658
.collect (toList ());
1662
1659
1663
1660
List <Method > superinterfaceMethods = getInterfaceMethods (ifc , predicate , traversalMode ).stream ()
0 commit comments