3
3
import org .jetbrains .annotations .NotNull ;
4
4
import org .jetbrains .annotations .Nullable ;
5
5
6
+ import java .lang .reflect .Field ;
6
7
import java .lang .reflect .Modifier ;
7
- import java .util .Arrays ;
8
- import java .util .Objects ;
8
+ import java .util .* ;
9
+ import java .util .function . Predicate ;
9
10
10
11
11
12
/**
@@ -25,18 +26,27 @@ public Stringable() {
25
26
}
26
27
27
28
/**
28
- * Converts an object to a {@link String} by getting all of its fields and their values
29
+ * Converts an object to a {@link String} by getting all of its fields and their values based on the given predicate
30
+ * <br>Will also get superclass fields
29
31
*
30
- * @param object the object to convert
32
+ * @param object the object to convert
33
+ * @param fieldPredicate the predicate to test the fields with (if null, all fields are included)
31
34
*
32
- * @return the object as a {@link String}
35
+ * @return the object as a {@link String}
33
36
*/
34
37
@ NotNull
35
- public static String toString (@ Nullable Object object ) {
38
+ public static String toString (@ Nullable Object object , @ Nullable Predicate < Field > fieldPredicate ) {
36
39
if (object == null ) return "null" ;
37
40
final Class <?> clazz = object .getClass ();
38
- return clazz .getSimpleName () + "{" + Arrays .stream (clazz .getDeclaredFields ())
39
- .filter (field -> !Modifier .isStatic (field .getModifiers ()))
41
+
42
+ // Get declared fields and fields from all superclasses
43
+ final List <Field > fields = new ArrayList <>();
44
+ for (Class <?> c = clazz ; c != null ; c = c .getSuperclass ()) fields .addAll (Arrays .asList (c .getDeclaredFields ()));
45
+
46
+ // Create the string
47
+ final boolean fieldPredicateNull = fieldPredicate == null ;
48
+ return clazz .getSimpleName () + "{" + fields .stream ()
49
+ .filter (field -> !Modifier .isStatic (field .getModifiers ()) && (fieldPredicateNull || fieldPredicate .test (field )))
40
50
.map (field -> {
41
51
final String entry ;
42
52
try {
@@ -53,4 +63,44 @@ public static String toString(@Nullable Object object) {
53
63
.reduce ((a , b ) -> a + ", " + b )
54
64
.orElse ("" ) + "}" ;
55
65
}
66
+
67
+ /**
68
+ * Converts an object to a {@link String} by getting all of its fields and their values, excluding fields with the given classes
69
+ *
70
+ * @param object the object to convert
71
+ * @param ignoredClasses the classes to exclude from the conversion
72
+ *
73
+ * @return the object as a {@link String}
74
+ */
75
+ @ NotNull
76
+ public static String toString (@ Nullable Object object , @ NotNull Class <?>... ignoredClasses ) {
77
+ final Set <Class <?>> ignoredClassesSet = new HashSet <>(Arrays .asList (ignoredClasses ));
78
+ return toString (object , field -> !ignoredClassesSet .contains (field .getType ()));
79
+ }
80
+
81
+ /**
82
+ * Converts an object to a {@link String} by getting all of its fields and their values, excluding fields with the given names
83
+ *
84
+ * @param object the object to convert
85
+ * @param ignoredFields the names of the fields to exclude from the conversion
86
+ *
87
+ * @return the object as a {@link String}
88
+ */
89
+ @ NotNull
90
+ public static String toString (@ Nullable Object object , @ NotNull String ... ignoredFields ) {
91
+ final Set <String > ignoredFieldsSet = new HashSet <>(Arrays .asList (ignoredFields ));
92
+ return toString (object , field -> !ignoredFieldsSet .contains (field .getName ()));
93
+ }
94
+
95
+ /**
96
+ * Converts an object to a {@link String} by getting all of its fields and their values
97
+ *
98
+ * @param object the object to convert
99
+ *
100
+ * @return the object as a {@link String}
101
+ */
102
+ @ NotNull
103
+ public static String toString (@ Nullable Object object ) {
104
+ return toString (object , (Predicate <Field >) null );
105
+ }
56
106
}
0 commit comments