From 3e6a5c0ac935333adc818a04c1fbd5e1dad66741 Mon Sep 17 00:00:00 2001 From: Petr Ilin Date: Sun, 3 Sep 2023 01:35:37 +0300 Subject: [PATCH] Fix AbstractReader#readCollectionByType --- .../serializer/language/reader/AbstractReader.java | 4 ++-- .../elytrium/serializer/utils/GenericUtils.java | 14 ++++++++++++++ .../net/elytrium/serializer/SerializerTest.java | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/elytrium/serializer/language/reader/AbstractReader.java b/src/main/java/net/elytrium/serializer/language/reader/AbstractReader.java index 78cd4f9..c521dc0 100644 --- a/src/main/java/net/elytrium/serializer/language/reader/AbstractReader.java +++ b/src/main/java/net/elytrium/serializer/language/reader/AbstractReader.java @@ -297,9 +297,9 @@ private Collection readCollectionByType(Field owner, Type type, Class } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new SerializableReadException(e); } - } else if (Collection.class.isAssignableFrom(owner.getType())) { + } else { try { - Constructor constructor = owner.getType().getDeclaredConstructor(); + Constructor constructor = GenericUtils.unwrapClassParameterizedType(type).getDeclaredConstructor(); constructor.setAccessible(true); return this.readCollection(owner, (Collection) constructor.newInstance(), collectionEntryType); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { diff --git a/src/main/java/net/elytrium/serializer/utils/GenericUtils.java b/src/main/java/net/elytrium/serializer/utils/GenericUtils.java index 3e6fd12..f4d6c77 100644 --- a/src/main/java/net/elytrium/serializer/utils/GenericUtils.java +++ b/src/main/java/net/elytrium/serializer/utils/GenericUtils.java @@ -23,6 +23,20 @@ public class GenericUtils { + public static Class unwrapClassParameterizedType(Type type) { + if (type instanceof ParameterizedType parameterizedType) { + if (parameterizedType.getRawType() instanceof Class clazz) { + return clazz; + } else { + throw new IllegalArgumentException("Can only unwrap Class from ParameterizedType"); + } + } else if (type instanceof Class clazz) { + return clazz; + } else { + throw new IllegalArgumentException("Can only unwrap Class or ParameterizedType of Class"); + } + } + public static Type getParameterTypeFromSuperclass(Class parent, Type type, Type superclass, int searchIndex) { Type superclassType = GenericUtils.getParameterTypeOrNull(parent, superclass, searchIndex); if (superclassType instanceof TypeVariable typeVariable && type instanceof ParameterizedType parameterizedType) { diff --git a/src/test/java/net/elytrium/serializer/SerializerTest.java b/src/test/java/net/elytrium/serializer/SerializerTest.java index 5dc4bc4..7d1949f 100644 --- a/src/test/java/net/elytrium/serializer/SerializerTest.java +++ b/src/test/java/net/elytrium/serializer/SerializerTest.java @@ -348,7 +348,7 @@ public String deserialize(String from) { @MapType(Int2ObjectLinkedOpenHashMap.class) public Int2ObjectMap int2StringMapFastUtil2 = new Int2ObjectLinkedOpenHashMap<>(new int[] { 1, 15555, 44 }, new String[] { "v1", "v2", "v3" }); - public final/*TODO non final*/ Int2ObjectLinkedOpenHashMap int2LongListMapFastUtil = new Int2ObjectLinkedOpenHashMap<>( + public Int2ObjectLinkedOpenHashMap int2LongListMapFastUtil = new Int2ObjectLinkedOpenHashMap<>( new int[] { 1, -15555 }, new LongArrayList[] { LongArrayList.of(1, 2, -3, 4), LongArrayList.of(3, 2, -3, 4) } );