From 7c4fd786806c7a9433eda7dd374f082b17b0afad Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sat, 24 Jun 2023 16:28:53 +0200 Subject: [PATCH] Update some dependencies + Gson now natively supports record --- pandalib-chat/pom.xml | 6 +- .../java/fr/pandacube/lib/core/json/Json.java | 17 ---- .../lib/core/json/RecordTypeAdapter.java | 88 ------------------- 3 files changed, 3 insertions(+), 108 deletions(-) delete mode 100644 pandalib-core/src/main/java/fr/pandacube/lib/core/json/RecordTypeAdapter.java diff --git a/pandalib-chat/pom.xml b/pandalib-chat/pom.xml index 6d50cf2..e51fe76 100644 --- a/pandalib-chat/pom.xml +++ b/pandalib-chat/pom.xml @@ -26,17 +26,17 @@ net.kyori adventure-api - 4.11.0 + 4.14.0 net.kyori adventure-platform-bungeecord - 4.1.1 + 4.3.0 net.kyori adventure-text-serializer-plain - 4.11.0 + 4.14.0 diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/json/Json.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/json/Json.java index 495f4cb..7646abc 100644 --- a/pandalib-core/src/main/java/fr/pandacube/lib/core/json/Json.java +++ b/pandalib-core/src/main/java/fr/pandacube/lib/core/json/Json.java @@ -5,7 +5,6 @@ import com.google.gson.GsonBuilder; import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapterFactory; import com.google.gson.reflect.TypeToken; -import fr.pandacube.lib.util.Log; import java.util.ArrayList; import java.util.List; @@ -90,23 +89,7 @@ public class Json { - private static boolean hasGsonNativeRecordSupport() { - try { - com.google.gson.internal.reflect.ReflectionHelper.class.getDeclaredField("RECORD_HELPER"); - return true; - } catch (NoClassDefFoundError e) { - Log.warning("Unable to check Gson supporting records. Assuming it does not. " + e); - return false; - } catch (NoSuchFieldException e) { - return false; - } - } - - - static { - if (!hasGsonNativeRecordSupport()) - registerTypeAdapterFactory(RecordTypeAdapter.FACTORY); registerTypeAdapterFactory(StackTraceElementAdapter.FACTORY); registerTypeAdapterFactory(ThrowableAdapter.FACTORY); } diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/json/RecordTypeAdapter.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/json/RecordTypeAdapter.java deleted file mode 100644 index 5cbbf2b..0000000 --- a/pandalib-core/src/main/java/fr/pandacube/lib/core/json/RecordTypeAdapter.java +++ /dev/null @@ -1,88 +0,0 @@ -package fr.pandacube.lib.core.json; - -import com.google.gson.Gson; -import com.google.gson.TypeAdapter; -import com.google.gson.TypeAdapterFactory; -import com.google.gson.reflect.TypeToken; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonToken; -import com.google.gson.stream.JsonWriter; - -import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.RecordComponent; -import java.util.HashMap; -import java.util.Map; - -// from https://github.com/google/gson/issues/1794#issuecomment-812964421 -/* package */ class RecordTypeAdapter extends TypeAdapter { - - public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() { - @Override - public TypeAdapter create(Gson gson, TypeToken type) { - @SuppressWarnings("unchecked") - Class clazz = (Class) type.getRawType(); - if (!clazz.isRecord() || clazz == Record.class) { - return null; - } - return new RecordTypeAdapter<>(gson, this, type); - } - }; - - - private final Gson gson; - private final TypeAdapterFactory factory; - private final TypeToken type; - - public RecordTypeAdapter(Gson gson, TypeAdapterFactory factory, TypeToken type) { - this.gson = gson; - this.factory = factory; - this.type = type; - } - - @Override - public void write(JsonWriter out, T value) throws IOException { - gson.getDelegateAdapter(factory, type).write(out, value); - } - - @Override - public T read(JsonReader reader) throws IOException { - if (reader.peek() == JsonToken.NULL) { - reader.nextNull(); - return null; - } else { - @SuppressWarnings("unchecked") - Class clazz = (Class) type.getRawType(); - - RecordComponent[] recordComponents = clazz.getRecordComponents(); - Map> typeMap = new HashMap<>(); - for (RecordComponent recordComponent : recordComponents) { - typeMap.put(recordComponent.getName(), TypeToken.get(recordComponent.getGenericType())); - } - var argsMap = new HashMap(); - reader.beginObject(); - while (reader.hasNext()) { - String name = reader.nextName(); - argsMap.put(name, gson.getAdapter(typeMap.get(name)).read(reader)); - } - reader.endObject(); - - var argTypes = new Class[recordComponents.length]; - var args = new Object[recordComponents.length]; - for (int i = 0; i < recordComponents.length; i++) { - argTypes[i] = recordComponents[i].getType(); - args[i] = argsMap.get(recordComponents[i].getName()); - } - Constructor constructor; - try { - constructor = clazz.getDeclaredConstructor(argTypes); - constructor.setAccessible(true); - return constructor.newInstance(args); - } catch (NoSuchMethodException | InstantiationException | SecurityException | IllegalAccessException - | IllegalArgumentException | InvocationTargetException e) { - throw new RuntimeException(e); - } - } - } -}