diff options
author | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2022-07-03 07:34:26 +0700 |
---|---|---|
committer | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2022-07-03 07:34:26 +0700 |
commit | 1e285b84ec8dace3331b11dfa5b10a8af6cd8354 (patch) | |
tree | c2e30f4363b692fe959e4935423e0d24d567306a | |
parent | 74ec27a1d7208d7b22f715206ea143b399238e83 (diff) | |
download | OneConfig-1e285b84ec8dace3331b11dfa5b10a8af6cd8354.tar.gz OneConfig-1e285b84ec8dace3331b11dfa5b10a8af6cd8354.tar.bz2 OneConfig-1e285b84ec8dace3331b11dfa5b10a8af6cd8354.zip |
fix private fields not deserializing
-rw-r--r-- | api/OneConfig.api | 1 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/config/Config.java | 27 | ||||
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java | 23 |
3 files changed, 33 insertions, 18 deletions
diff --git a/api/OneConfig.api b/api/OneConfig.api index eea861a..c047917 100644 --- a/api/OneConfig.api +++ b/api/OneConfig.api @@ -162,6 +162,7 @@ public class cc/polyfrost/oneconfig/config/core/ConfigUtils { public static fun findAnnotation (Lcom/google/gson/FieldAttributes;Ljava/lang/Class;)Ljava/lang/annotation/Annotation; public static fun findAnnotation (Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/annotation/Annotation; public static fun findAnnotation (Ljava/lang/reflect/Field;Ljava/lang/Class;)Ljava/lang/annotation/Annotation; + public static fun getClassFields (Ljava/lang/Class;)Ljava/util/ArrayList; public static fun getClassOptions (Ljava/lang/Object;)Ljava/util/ArrayList; public static fun getField (Ljava/lang/reflect/Field;Ljava/lang/Object;)Ljava/lang/Object; public static fun getOption (Lcc/polyfrost/oneconfig/internal/config/annotations/Option;Ljava/lang/reflect/Field;Ljava/lang/Object;)Lcc/polyfrost/oneconfig/config/elements/BasicOption; 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; |