Skip to content

Commit 3e6a5c0

Browse files
committed
Fix AbstractReader#readCollectionByType
1 parent 60a9f29 commit 3e6a5c0

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/main/java/net/elytrium/serializer/language/reader/AbstractReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ private Collection<Object> readCollectionByType(Field owner, Type type, Class<?>
297297
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
298298
throw new SerializableReadException(e);
299299
}
300-
} else if (Collection.class.isAssignableFrom(owner.getType())) {
300+
} else {
301301
try {
302-
Constructor<?> constructor = owner.getType().getDeclaredConstructor();
302+
Constructor<?> constructor = GenericUtils.unwrapClassParameterizedType(type).getDeclaredConstructor();
303303
constructor.setAccessible(true);
304304
return this.readCollection(owner, (Collection<Object>) constructor.newInstance(), collectionEntryType);
305305
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {

src/main/java/net/elytrium/serializer/utils/GenericUtils.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@
2323

2424
public class GenericUtils {
2525

26+
public static Class<?> unwrapClassParameterizedType(Type type) {
27+
if (type instanceof ParameterizedType parameterizedType) {
28+
if (parameterizedType.getRawType() instanceof Class<?> clazz) {
29+
return clazz;
30+
} else {
31+
throw new IllegalArgumentException("Can only unwrap Class<?> from ParameterizedType");
32+
}
33+
} else if (type instanceof Class<?> clazz) {
34+
return clazz;
35+
} else {
36+
throw new IllegalArgumentException("Can only unwrap Class<?> or ParameterizedType of Class<?>");
37+
}
38+
}
39+
2640
public static Type getParameterTypeFromSuperclass(Class<?> parent, Type type, Type superclass, int searchIndex) {
2741
Type superclassType = GenericUtils.getParameterTypeOrNull(parent, superclass, searchIndex);
2842
if (superclassType instanceof TypeVariable<?> typeVariable && type instanceof ParameterizedType parameterizedType) {

src/test/java/net/elytrium/serializer/SerializerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public String deserialize(String from) {
348348
@MapType(Int2ObjectLinkedOpenHashMap.class)
349349
public Int2ObjectMap<String> int2StringMapFastUtil2 = new Int2ObjectLinkedOpenHashMap<>(new int[] { 1, 15555, 44 }, new String[] { "v1", "v2", "v3" });
350350

351-
public final/*TODO non final*/ Int2ObjectLinkedOpenHashMap<LongArrayList> int2LongListMapFastUtil = new Int2ObjectLinkedOpenHashMap<>(
351+
public Int2ObjectLinkedOpenHashMap<LongArrayList> int2LongListMapFastUtil = new Int2ObjectLinkedOpenHashMap<>(
352352
new int[] { 1, -15555 },
353353
new LongArrayList[] { LongArrayList.of(1, 2, -3, 4), LongArrayList.of(3, 2, -3, 4) }
354354
);

0 commit comments

Comments
 (0)