Skip to content

Commit 2bfeda6

Browse files
committed
New Stringable methods and get superclass fields
1 parent 94d5d9c commit 2bfeda6

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

src/main/java/xyz/srnyx/javautilities/parents/Stringable.java

+58-8
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import org.jetbrains.annotations.NotNull;
44
import org.jetbrains.annotations.Nullable;
55

6+
import java.lang.reflect.Field;
67
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;
910

1011

1112
/**
@@ -25,18 +26,27 @@ public Stringable() {
2526
}
2627

2728
/**
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
2931
*
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)
3134
*
32-
* @return the object as a {@link String}
35+
* @return the object as a {@link String}
3336
*/
3437
@NotNull
35-
public static String toString(@Nullable Object object) {
38+
public static String toString(@Nullable Object object, @Nullable Predicate<Field> fieldPredicate) {
3639
if (object == null) return "null";
3740
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)))
4050
.map(field -> {
4151
final String entry;
4252
try {
@@ -53,4 +63,44 @@ public static String toString(@Nullable Object object) {
5363
.reduce((a, b) -> a + ", " + b)
5464
.orElse("") + "}";
5565
}
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+
}
56106
}

0 commit comments

Comments
 (0)