@@ -1354,11 +1354,11 @@ private static Optional<Method> findMethod(Class<?> clazz, Predicate<Method> pre
1354
1354
1355
1355
for (Class <?> current = clazz ; isSearchable (current ); current = current .getSuperclass ()) {
1356
1356
// 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
+ }
1362
1362
}
1363
1363
1364
1364
// Search for match in interfaces implemented by current type
@@ -1453,8 +1453,8 @@ private static List<Method> findAllMethodsInHierarchy(Class<?> clazz, Predicate<
1453
1453
Preconditions .notNull (traversalMode , "HierarchyTraversalMode must not be null" );
1454
1454
1455
1455
// @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 () ))
1458
1458
.collect (toList ());
1459
1459
List <Method > superclassMethods = getSuperclassMethods (clazz , predicate , traversalMode ).stream ()
1460
1460
.filter (method -> !isMethodShadowedByLocalMethods (method , localMethods ))
@@ -1495,26 +1495,24 @@ private static List<Field> getDeclaredFields(Class<?> clazz) {
1495
1495
1496
1496
/**
1497
1497
* 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.
1499
1499
*/
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 ());
1502
1502
}
1503
1503
1504
1504
/**
1505
1505
* 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.
1507
1507
*
1508
1508
* <p>In addition, the list returned by this method includes interface
1509
1509
* default methods which are either prepended or appended to the list of
1510
1510
* declared methods depending on the supplied traversal mode.
1511
1511
*/
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 ) {
1515
1513
// 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 ());
1518
1516
1519
1517
// Take the traversal mode into account in order to retain the inherited
1520
1518
// nature of interface default methods.
@@ -1531,23 +1529,23 @@ private static List<Method> getDeclaredMethods(Class<?> clazz, Predicate<Method>
1531
1529
/**
1532
1530
* Get a sorted, mutable list of all default methods present in interfaces
1533
1531
* 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.
1535
1533
*
1536
1534
* @see <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#d5e9652">Method Visibility</a>
1537
1535
* in the Java Language Specification
1538
1536
*/
1539
- private static List <Method > getDefaultMethods (Class <?> clazz , Predicate < Method > predicate ) {
1537
+ private static List <Method > getDefaultMethods (Class <?> clazz ) {
1540
1538
// @formatter:off
1541
1539
// Visible default methods are interface default methods that have not
1542
1540
// been overridden.
1543
1541
List <Method > visibleDefaultMethods = Arrays .stream (clazz .getMethods ())
1544
- .filter (predicate . and ( Method ::isDefault ) )
1542
+ .filter (Method ::isDefault )
1545
1543
.collect (toCollection (ArrayList ::new ));
1546
1544
if (visibleDefaultMethods .isEmpty ()) {
1547
1545
return visibleDefaultMethods ;
1548
1546
}
1549
1547
return Arrays .stream (clazz .getInterfaces ())
1550
- .map (ifc -> getMethods ( ifc , predicate ) )
1548
+ .map (ReflectionUtils :: getMethods )
1551
1549
.flatMap (List ::stream )
1552
1550
.filter (visibleDefaultMethods ::contains )
1553
1551
.collect (toCollection (ArrayList ::new ));
@@ -1563,10 +1561,9 @@ private static List<Field> toSortedMutableList(Field[] fields) {
1563
1561
// @formatter:on
1564
1562
}
1565
1563
1566
- private static List <Method > toSortedMutableList (Method [] methods , Predicate < Method > predicate ) {
1564
+ private static List <Method > toSortedMutableList (Method [] methods ) {
1567
1565
// @formatter:off
1568
1566
return Arrays .stream (methods )
1569
- .filter (predicate )
1570
1567
.sorted (ReflectionUtils ::defaultMethodSorter )
1571
1568
// Use toCollection() instead of toList() to ensure list is mutable.
1572
1569
.collect (toCollection (ArrayList ::new ));
@@ -1605,8 +1602,8 @@ private static List<Method> getInterfaceMethods(Class<?> clazz, Predicate<Method
1605
1602
for (Class <?> ifc : clazz .getInterfaces ()) {
1606
1603
1607
1604
// @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 ) ))
1610
1607
.collect (toList ());
1611
1608
1612
1609
List <Method > superinterfaceMethods = getInterfaceMethods (ifc , predicate , traversalMode ).stream ()
0 commit comments