From 3c0e834348a4a6a47ee0dab6e1a213984207aaa6 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Thu, 1 Jun 2023 14:18:12 -0400 Subject: [PATCH] Fix `FieldFetch` not getting fields from superclasses --- .../java/emu/grasscutter/utils/Utils.java | 32 ++++++++++++++++--- .../grasscutter/utils/objects/FieldFetch.java | 10 +++--- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/main/java/emu/grasscutter/utils/Utils.java b/src/main/java/emu/grasscutter/utils/Utils.java index 6edb3201c..c063b5b32 100644 --- a/src/main/java/emu/grasscutter/utils/Utils.java +++ b/src/main/java/emu/grasscutter/utils/Utils.java @@ -1,8 +1,5 @@ package emu.grasscutter.utils; -import static emu.grasscutter.utils.FileUtils.getResourcePath; -import static emu.grasscutter.utils.lang.Language.translate; - import emu.grasscutter.Grasscutter; import emu.grasscutter.config.ConfigContainer; import emu.grasscutter.data.DataLoader; @@ -11,15 +8,20 @@ import emu.grasscutter.utils.objects.Returnable; import io.javalin.http.Context; import io.netty.buffer.*; import it.unimi.dsi.fastutil.ints.*; +import org.slf4j.Logger; + +import javax.annotation.Nullable; import java.io.*; +import java.lang.reflect.Field; import java.nio.charset.StandardCharsets; import java.nio.file.*; import java.time.*; import java.time.temporal.TemporalAdjusters; import java.util.*; import java.util.concurrent.ThreadLocalRandom; -import javax.annotation.Nullable; -import org.slf4j.Logger; + +import static emu.grasscutter.utils.FileUtils.getResourcePath; +import static emu.grasscutter.utils.lang.Language.translate; @SuppressWarnings({"UnusedReturnValue", "BooleanMethodIsAlwaysInverted"}) public final class Utils { @@ -484,6 +486,7 @@ public final class Utils { * * @param runnable The task to run. */ + @SuppressWarnings("BusyWait") public static void waitFor(Returnable runnable) { while (!runnable.invoke()) { try { @@ -493,4 +496,23 @@ public final class Utils { } } } + + /** + * Recursively finds all fields in a class. + * + * @param type The class to find fields in. + * @return A list of all fields in the class. + */ + public static List getAllFields(Class type) { + var fields = new LinkedList<>( + Arrays.asList(type.getDeclaredFields()) + ); + + // Check for superclasses. + if (type.getSuperclass() != null) { + fields.addAll(getAllFields(type.getSuperclass())); + } + + return fields; + } } diff --git a/src/main/java/emu/grasscutter/utils/objects/FieldFetch.java b/src/main/java/emu/grasscutter/utils/objects/FieldFetch.java index bd0584284..24207f6da 100644 --- a/src/main/java/emu/grasscutter/utils/objects/FieldFetch.java +++ b/src/main/java/emu/grasscutter/utils/objects/FieldFetch.java @@ -1,11 +1,11 @@ package emu.grasscutter.utils.objects; -import com.google.gson.JsonNull; -import com.google.gson.JsonObject; +import com.google.gson.*; import emu.grasscutter.server.dispatch.IDispatcher; +import emu.grasscutter.utils.Utils; + import java.lang.reflect.Field; -import java.util.Arrays; -import java.util.HashMap; +import java.util.*; public interface FieldFetch { /** @@ -18,7 +18,7 @@ public interface FieldFetch { // Prepare field properties. var fieldValues = new JsonObject(); var fieldMap = new HashMap(); - Arrays.stream(this.getClass().getDeclaredFields()) + Utils.getAllFields(this.getClass()) .forEach(field -> fieldMap.put(field.getName(), field)); // Find the values of all requested fields.