aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost
diff options
context:
space:
mode:
authorWyvest <45589059+Wyvest@users.noreply.github.com>2022-07-03 07:34:26 +0700
committerWyvest <45589059+Wyvest@users.noreply.github.com>2022-07-03 07:34:26 +0700
commit1e285b84ec8dace3331b11dfa5b10a8af6cd8354 (patch)
treec2e30f4363b692fe959e4935423e0d24d567306a /src/main/java/cc/polyfrost
parent74ec27a1d7208d7b22f715206ea143b399238e83 (diff)
downloadOneConfig-1e285b84ec8dace3331b11dfa5b10a8af6cd8354.tar.gz
OneConfig-1e285b84ec8dace3331b11dfa5b10a8af6cd8354.tar.bz2
OneConfig-1e285b84ec8dace3331b11dfa5b10a8af6cd8354.zip
fix private fields not deserializing
Diffstat (limited to 'src/main/java/cc/polyfrost')
-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;