From 1e285b84ec8dace3331b11dfa5b10a8af6cd8354 Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Sun, 3 Jul 2022 07:34:26 +0700 Subject: fix private fields not deserializing --- .../java/cc/polyfrost/oneconfig/config/Config.java | 27 ++++++++++++++-------- .../oneconfig/config/core/ConfigUtils.java | 23 ++++++++++-------- 2 files changed, 32 insertions(+), 18 deletions(-) (limited to 'src/main/java/cc/polyfrost/oneconfig/config') 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 fields = ConfigUtils.getClassFields(clazz); for (Map.Entry 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 getClassOptions(Object object) { ArrayList options = new ArrayList<>(); - ArrayList 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 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 getClassFields(Class object) { + ArrayList 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; -- cgit