aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/config
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/config')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/Config.java27
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java23
2 files changed, 32 insertions, 18 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java
index dd5a578..9c70aa6 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/Config.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java
@@ -29,10 +29,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Optional;
+import java.util.*;
import java.util.function.Supplier;
public class Config {
@@ -166,6 +163,7 @@ public class Config {
*/
protected void deserializePart(JsonObject json, Object instance) {
Class<?> clazz = instance.getClass();
+ ArrayList<Field> fields = ConfigUtils.getClassFields(clazz);
for (Map.Entry<String, JsonElement> element : json.entrySet()) {
String name = element.getKey();
JsonElement value = element.getValue();
@@ -177,12 +175,23 @@ public class Config {
}
}
try {
- Field field = clazz.getField(name);
- TypeAdapter<?> adapter = gson.getAdapter(field.getType());
- Object object = adapter.fromJsonTree(value);
- field.setAccessible(true);
- field.set(instance, object);
+ Field field = null;
+ for (Field f : fields) {
+ if (f.getName().equals(name)) {
+ field = f;
+ break;
+ }
+ }
+ if (field != null) {
+ TypeAdapter<?> adapter = gson.getAdapter(field.getType());
+ Object object = adapter.fromJsonTree(value);
+ field.setAccessible(true);
+ field.set(instance, object);
+ } else {
+ System.out.println("Could not deserialize " + name + " in class " + clazz.getSimpleName());
+ }
} catch (Exception ignored) {
+ System.out.println("Could not deserialize " + name + " in class " + clazz.getSimpleName());
}
}
}
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java b/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java
index 4c860e4..2b3c90b 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java
@@ -47,15 +47,7 @@ public class ConfigUtils {
public static ArrayList<BasicOption> getClassOptions(Object object) {
ArrayList<BasicOption> options = new ArrayList<>();
- ArrayList<Field> fields = new ArrayList<>(Arrays.asList(object.getClass().getDeclaredFields()));
- Class<?> parentClass = object.getClass();
- Class<?> clazz = object.getClass();
- while (true) {
- clazz = clazz.getSuperclass();
- if (clazz != null && clazz != parentClass) fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
- else break;
- parentClass = clazz;
- }
+ ArrayList<Field> fields = getClassFields(object.getClass());
for (Field field : fields) {
Option option = findAnnotation(field, Option.class);
if (option == null) continue;
@@ -64,6 +56,19 @@ public class ConfigUtils {
return options;
}
+ public static ArrayList<Field> getClassFields(Class<?> object) {
+ ArrayList<Field> fields = new ArrayList<>(Arrays.asList(object.getDeclaredFields()));
+ Class<?> parentClass = object;
+ Class<?> clazz = object;
+ while (true) {
+ clazz = clazz.getSuperclass();
+ if (clazz != null && clazz != parentClass) fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
+ else break;
+ parentClass = clazz;
+ }
+ return fields;
+ }
+
public static BasicOption addOptionToPage(OptionPage page, Option option, Field field, Object instance, @Nullable Migrator migrator) {
BasicOption configOption = getOption(option, field, instance);
if (configOption == null) return null;