Skip to content

Commit

Permalink
Fix AbstractReader#readCollectionByType
Browse files Browse the repository at this point in the history
  • Loading branch information
hevav committed Sep 2, 2023
1 parent 60a9f29 commit 3e6a5c0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ private Collection<Object> 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<Object>) constructor.newInstance(), collectionEntryType);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/net/elytrium/serializer/utils/GenericUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/elytrium/serializer/SerializerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public String deserialize(String from) {
@MapType(Int2ObjectLinkedOpenHashMap.class)
public Int2ObjectMap<String> int2StringMapFastUtil2 = new Int2ObjectLinkedOpenHashMap<>(new int[] { 1, 15555, 44 }, new String[] { "v1", "v2", "v3" });

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

0 comments on commit 3e6a5c0

Please sign in to comment.