From 290ec20019977dfb1dbd3a8e6c152693c3337918 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Sun, 7 Apr 2024 10:29:45 +0200 Subject: fix config read errors --- .../skyhanni/utils/FeatureTogglesByDefaultAdapter.kt | 16 +++++++++++++--- .../java/at/hannibal2/skyhanni/utils/ReflectionUtils.kt | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) (limited to 'src/main/java/at/hannibal2') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/FeatureTogglesByDefaultAdapter.kt b/src/main/java/at/hannibal2/skyhanni/utils/FeatureTogglesByDefaultAdapter.kt index 011dcc10c..f78a0c4dc 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/FeatureTogglesByDefaultAdapter.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/FeatureTogglesByDefaultAdapter.kt @@ -1,6 +1,7 @@ package at.hannibal2.skyhanni.utils import at.hannibal2.skyhanni.config.FeatureToggle +import at.hannibal2.skyhanni.utils.ReflectionUtils.getDeclaredFieldOrNull import com.google.gson.Gson import com.google.gson.JsonObject import com.google.gson.JsonPrimitive @@ -49,8 +50,17 @@ object FeatureTogglesByDefaultAdapter : TypeAdapterFactory { // Read the actual JSON Object while (reader.peek() != JsonToken.END_OBJECT) { + // IllegalStateException: Expected NAME but was BOOLEAN + if (reader.peek() != JsonToken.NAME) { + reader.skipValue() + continue + } val name = reader.nextName() - val field = clazz.getDeclaredField(name) + val field = clazz.getDeclaredFieldOrNull(name) + if (field == null) { + println("field is in config file, but not in object file: $name") + continue + } val fieldType = gson.getAdapter(TypeToken.get(getType(type, field))) // Read the field data val data = fieldType.read(reader) @@ -70,10 +80,10 @@ object FeatureTogglesByDefaultAdapter : TypeAdapterFactory { // Check if this object has any feature toggles present if (t.fields.none { it.isAnnotationPresent(FeatureToggle::class.java) || - gson.getAdapter(TypeToken.get(getType(type, it))) is Adapter + gson.getAdapter(TypeToken.get(getType(type, it))) is Adapter }) return null val originalWrite = gson.getDelegateAdapter(this, type) return Adapter(originalWrite, t as Class, gson, type) } -} \ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ReflectionUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ReflectionUtils.kt index d8ebbaf68..1fe8b882c 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ReflectionUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ReflectionUtils.kt @@ -80,4 +80,5 @@ object ReflectionUtils { return packageLookup[shPackageName] } + fun Class<*>.getDeclaredFieldOrNull(name: String): Field? = declaredFields.firstOrNull { it.name == name } } -- cgit