@@ -1238,6 +1238,7 @@ public static List<Constructor<?>> findConstructors(Class<?> clazz, Predicate<Co
1238
1238
*/
1239
1239
public static List <Field > findFields (Class <?> clazz , Predicate <Field > predicate ,
1240
1240
HierarchyTraversalMode traversalMode ) {
1241
+
1241
1242
return streamFields (clazz , predicate , traversalMode ).collect (toUnmodifiableList ());
1242
1243
}
1243
1244
@@ -1252,21 +1253,23 @@ public static Stream<Field> streamFields(Class<?> clazz, Predicate<Field> predic
1252
1253
Preconditions .notNull (predicate , "Predicate must not be null" );
1253
1254
Preconditions .notNull (traversalMode , "HierarchyTraversalMode must not be null" );
1254
1255
1255
- return findAllFieldsInHierarchy (clazz , traversalMode ).stream (). filter ( predicate );
1256
+ return findAllFieldsInHierarchy (clazz , predicate , traversalMode ).stream ();
1256
1257
}
1257
1258
1258
- private static List <Field > findAllFieldsInHierarchy (Class <?> clazz , HierarchyTraversalMode traversalMode ) {
1259
+ private static List <Field > findAllFieldsInHierarchy (Class <?> clazz , Predicate <Field > predicate ,
1260
+ HierarchyTraversalMode traversalMode ) {
1261
+
1259
1262
Preconditions .notNull (clazz , "Class must not be null" );
1260
1263
Preconditions .notNull (traversalMode , "HierarchyTraversalMode must not be null" );
1261
1264
1262
1265
// @formatter:off
1263
- List <Field > localFields = getDeclaredFields (clazz ).stream ()
1266
+ List <Field > localFields = getDeclaredFields (clazz , predicate ).stream ()
1264
1267
.filter (field -> !field .isSynthetic ())
1265
1268
.collect (toList ());
1266
- List <Field > superclassFields = getSuperclassFields (clazz , traversalMode ).stream ()
1269
+ List <Field > superclassFields = getSuperclassFields (clazz , predicate , traversalMode ).stream ()
1267
1270
.filter (field -> !isFieldShadowedByLocalFields (field , localFields ))
1268
1271
.collect (toList ());
1269
- List <Field > interfaceFields = getInterfaceFields (clazz , traversalMode ).stream ()
1272
+ List <Field > interfaceFields = getInterfaceFields (clazz , predicate , traversalMode ).stream ()
1270
1273
.filter (field -> !isFieldShadowedByLocalFields (field , localFields ))
1271
1274
.collect (toList ());
1272
1275
// @formatter:on
@@ -1529,18 +1532,20 @@ private static List<Method> findAllMethodsInHierarchy(Class<?> clazz, Predicate<
1529
1532
1530
1533
/**
1531
1534
* Custom alternative to {@link Class#getFields()} that sorts the fields
1532
- * and converts them to a mutable list.
1535
+ * which match the supplied predicate and converts them to a mutable list.
1536
+ * @param predicate the field filter; never {@code null}
1533
1537
*/
1534
- private static List <Field > getFields (Class <?> clazz ) {
1535
- return toSortedMutableList (clazz .getFields ());
1538
+ private static List <Field > getFields (Class <?> clazz , Predicate < Field > predicate ) {
1539
+ return toSortedMutableList (clazz .getFields (), predicate );
1536
1540
}
1537
1541
1538
1542
/**
1539
1543
* Custom alternative to {@link Class#getDeclaredFields()} that sorts the
1540
- * fields and converts them to a mutable list.
1544
+ * fields which match the supplied predicate and converts them to a mutable list.
1545
+ * @param predicate the field filter; never {@code null}
1541
1546
*/
1542
- private static List <Field > getDeclaredFields (Class <?> clazz ) {
1543
- return toSortedMutableList (clazz .getDeclaredFields ());
1547
+ private static List <Field > getDeclaredFields (Class <?> clazz , Predicate < Field > predicate ) {
1548
+ return toSortedMutableList (clazz .getDeclaredFields (), predicate );
1544
1549
}
1545
1550
1546
1551
/**
@@ -1602,9 +1607,10 @@ private static List<Method> getDefaultMethods(Class<?> clazz) {
1602
1607
// @formatter:on
1603
1608
}
1604
1609
1605
- private static List <Field > toSortedMutableList (Field [] fields ) {
1610
+ private static List <Field > toSortedMutableList (Field [] fields , Predicate < Field > predicate ) {
1606
1611
// @formatter:off
1607
1612
return Arrays .stream (fields )
1613
+ .filter (predicate )
1608
1614
.sorted (ReflectionUtils ::defaultFieldSorter )
1609
1615
// Use toCollection() instead of toList() to ensure list is mutable.
1610
1616
.collect (toCollection (ArrayList ::new ));
@@ -1672,13 +1678,15 @@ private static List<Method> getInterfaceMethods(Class<?> clazz, Predicate<Method
1672
1678
return allInterfaceMethods ;
1673
1679
}
1674
1680
1675
- private static List <Field > getInterfaceFields (Class <?> clazz , HierarchyTraversalMode traversalMode ) {
1681
+ private static List <Field > getInterfaceFields (Class <?> clazz , Predicate <Field > predicate ,
1682
+ HierarchyTraversalMode traversalMode ) {
1683
+
1676
1684
List <Field > allInterfaceFields = new ArrayList <>();
1677
1685
for (Class <?> ifc : clazz .getInterfaces ()) {
1678
- List <Field > localInterfaceFields = getFields (ifc );
1686
+ List <Field > localInterfaceFields = getFields (ifc , predicate );
1679
1687
1680
1688
// @formatter:off
1681
- List <Field > superinterfaceFields = getInterfaceFields (ifc , traversalMode ).stream ()
1689
+ List <Field > superinterfaceFields = getInterfaceFields (ifc , predicate , traversalMode ).stream ()
1682
1690
.filter (field -> !isFieldShadowedByLocalFields (field , localInterfaceFields ))
1683
1691
.collect (toList ());
1684
1692
// @formatter:on
@@ -1694,12 +1702,14 @@ private static List<Field> getInterfaceFields(Class<?> clazz, HierarchyTraversal
1694
1702
return allInterfaceFields ;
1695
1703
}
1696
1704
1697
- private static List <Field > getSuperclassFields (Class <?> clazz , HierarchyTraversalMode traversalMode ) {
1705
+ private static List <Field > getSuperclassFields (Class <?> clazz , Predicate <Field > predicate ,
1706
+ HierarchyTraversalMode traversalMode ) {
1707
+
1698
1708
Class <?> superclass = clazz .getSuperclass ();
1699
1709
if (!isSearchable (superclass )) {
1700
1710
return Collections .emptyList ();
1701
1711
}
1702
- return findAllFieldsInHierarchy (superclass , traversalMode );
1712
+ return findAllFieldsInHierarchy (superclass , predicate , traversalMode );
1703
1713
}
1704
1714
1705
1715
private static boolean isFieldShadowedByLocalFields (Field field , List <Field > localFields ) {
0 commit comments