|
2 | 2 |
|
3 | 3 | import lombok.extern.slf4j.Slf4j; |
4 | 4 | import net.discordjug.javabot.util.ExceptionLogger; |
| 5 | +import net.discordjug.javabot.util.GsonUtils; |
5 | 6 | import net.discordjug.javabot.util.Pair; |
6 | 7 |
|
7 | 8 | import org.jetbrains.annotations.NotNull; |
|
10 | 11 | import java.lang.reflect.Field; |
11 | 12 | import java.lang.reflect.Modifier; |
12 | 13 | import java.util.Arrays; |
13 | | -import java.util.HashMap; |
14 | | -import java.util.Map; |
15 | 14 | import java.util.Optional; |
16 | | -import java.util.function.Function; |
17 | 15 |
|
18 | 16 | /** |
19 | 17 | * Utility class for resolving JSON files. |
20 | 18 | */ |
21 | 19 | @Slf4j |
22 | 20 | public class ReflectionUtils { |
23 | | - private static final Map<Class<?>, Function<String, Object>> propertyTypeParsers = new HashMap<>(); |
24 | | - |
25 | | - static { |
26 | | - propertyTypeParsers.put(Integer.class, Integer::parseInt); |
27 | | - propertyTypeParsers.put(int.class, Integer::parseInt); |
28 | | - propertyTypeParsers.put(Long.class, Long::parseLong); |
29 | | - propertyTypeParsers.put(long.class, Long::parseLong); |
30 | | - propertyTypeParsers.put(Float.class, Float::parseFloat); |
31 | | - propertyTypeParsers.put(float.class, Float::parseFloat); |
32 | | - propertyTypeParsers.put(Double.class, Double::parseDouble); |
33 | | - propertyTypeParsers.put(double.class, Double::parseDouble); |
34 | | - propertyTypeParsers.put(Boolean.class, Boolean::parseBoolean); |
35 | | - propertyTypeParsers.put(String.class, s -> s); |
36 | | - } |
37 | | - |
38 | 21 | private ReflectionUtils() { |
39 | 22 | } |
40 | 23 |
|
@@ -92,47 +75,17 @@ private static Field findDeclaredField(Class<?> cl, String name) throws NoSuchFi |
92 | 75 | } |
93 | 76 |
|
94 | 77 | /** |
95 | | - * Gets a mapping of properties and their type, recursively for the given |
96 | | - * type. |
97 | | - * |
98 | | - * @param parentPropertyName The root property name to append child field |
99 | | - * names to. This is null for the base case. |
100 | | - * @param parentClass The class to search for properties in. |
101 | | - * @return The map of properties and their types. |
102 | | - * @throws IllegalAccessException If a field cannot have its value obtained. |
103 | | - */ |
104 | | - public static @NotNull Map<String, Class<?>> getFields(@NotNull String parentPropertyName, @NotNull Class<?> parentClass) throws IllegalAccessException { |
105 | | - Map<String, Class<?>> fieldsMap = new HashMap<>(); |
106 | | - for (Field field : parentClass.getDeclaredFields()) { |
107 | | - // Skip transient fields. |
108 | | - if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) continue; |
109 | | - field.setAccessible(true); |
110 | | - String fieldPropertyName = parentPropertyName == null ? field.getName() : parentPropertyName + "." + field.getName(); |
111 | | - // Check if the field represents a "leaf" property, one which does not have any children. |
112 | | - if (propertyTypeParsers.containsKey(field.getType())) { |
113 | | - fieldsMap.put(fieldPropertyName, field.getType()); |
114 | | - } else { |
115 | | - Map<String, Class<?>> childFieldsMap = getFields(fieldPropertyName, field.getType()); |
116 | | - fieldsMap.putAll(childFieldsMap); |
117 | | - } |
118 | | - } |
119 | | - return fieldsMap; |
120 | | - } |
121 | | - |
122 | | - /** |
123 | | - * Sets the value of a field to a certain value, using {@link ReflectionUtils#propertyTypeParsers} |
124 | | - * to try and parse the correct value. |
| 78 | + * Sets the value of a field to a new value. |
125 | 79 | * |
126 | 80 | * @param field The field to set. |
127 | 81 | * @param parent The object whose property value to set. |
128 | 82 | * @param s The string representation of the value. |
| 83 | + * @return Returns the new value. |
129 | 84 | * @throws IllegalAccessException If the field cannot be set. |
130 | 85 | */ |
131 | | - public static void set(@NotNull Field field, @NotNull Object parent, @NotNull String s) throws IllegalAccessException { |
132 | | - Function<String, Object> parser = propertyTypeParsers.get(field.getType()); |
133 | | - if (parser == null) { |
134 | | - throw new IllegalArgumentException("No supported property type parser for the type " + field.getType().getSimpleName()); |
135 | | - } |
136 | | - field.set(parent, parser.apply(s)); |
| 86 | + public static Object set(@NotNull Field field, @NotNull Object parent, @NotNull String s) throws IllegalAccessException { |
| 87 | + Object value = field.getType() == String.class ? s : GsonUtils.fromJson(s, field.getGenericType()); |
| 88 | + field.set(parent, value); |
| 89 | + return value; |
137 | 90 | } |
138 | 91 | } |
0 commit comments