Skip to content

Commit 899972a

Browse files
committed
Ability to set custom map/collection type on non parameterized types
1 parent b21627b commit 899972a

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ public Object readByType(@Nullable Field owner, @Nullable Object holder, Type ty
214214
}
215215
} else if (type instanceof Class<?> clazz) {
216216
if (Map.class.isAssignableFrom(clazz)) {
217-
return this.readMap(owner);
218-
} else if (List.class.isAssignableFrom(clazz)) {
219-
return this.readList(owner);
217+
return this.readMapByType(owner, type);
218+
} else if (Collection.class.isAssignableFrom(clazz)) {
219+
return this.readCollectionByType(owner, type, clazz);
220220
} else if (String.class.isAssignableFrom(clazz)) {
221221
return this.readString(owner);
222222
} else if (Character.class.isAssignableFrom(clazz) || char.class.isAssignableFrom(clazz)) {
@@ -257,8 +257,8 @@ public Object readByType(@Nullable Field owner, @Nullable Object holder, Type ty
257257
}
258258

259259
@SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE")
260-
private Collection<Object> readCollectionByType(Field owner, ParameterizedType parameterizedType, Class<?> clazz) {
261-
Type collectionEntryType = GenericUtils.getParameterType(Collection.class, parameterizedType, 0);
260+
private Collection<Object> readCollectionByType(Field owner, Type type, Class<?> clazz) {
261+
Type collectionEntryType = GenericUtils.getParameterType(Collection.class, type, 0);
262262
if (owner != null) {
263263
CollectionType collectionType = owner.getAnnotation(CollectionType.class);
264264
if (collectionType != null) {
@@ -270,7 +270,7 @@ private Collection<Object> readCollectionByType(Field owner, ParameterizedType p
270270
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
271271
throw new SerializableReadException(e);
272272
}
273-
} else {
273+
} else if (Collection.class.isAssignableFrom(owner.getType())) {
274274
try {
275275
//noinspection unchecked
276276
return this.readCollection(owner,
@@ -293,9 +293,9 @@ private Collection<Object> readCollectionByType(Field owner, ParameterizedType p
293293
}
294294

295295
@SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE")
296-
private Map<Object, Object> readMapByType(Field owner, ParameterizedType parameterizedType) {
297-
Type mapKeyType = GenericUtils.getParameterType(Map.class, parameterizedType, 0);
298-
Type mapValueType = GenericUtils.getParameterType(Map.class, parameterizedType, 1);
296+
private Map<Object, Object> readMapByType(Field owner, Type type) {
297+
Type mapKeyType = GenericUtils.getParameterType(Map.class, type, 0);
298+
Type mapValueType = GenericUtils.getParameterType(Map.class, type, 1);
299299
if (owner != null) {
300300
MapType mapType = owner.getAnnotation(MapType.class);
301301
if (mapType != null) {
@@ -306,7 +306,7 @@ private Map<Object, Object> readMapByType(Field owner, ParameterizedType paramet
306306
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
307307
throw new SerializableReadException(e);
308308
}
309-
} else {
309+
} else if (Map.class.isAssignableFrom(owner.getType())) {
310310
try {
311311
//noinspection unchecked
312312
return this.readMap(owner,

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static int getParameterIndex(TypeVariable<?>[] variables, TypeVariable<?>
3434
}
3535

3636
public static Type getParameterTypeFromSuperclass(Class<?> parent, Type type, Type superclass, int searchIndex) {
37-
Type superclassType = GenericUtils.getParameterType(parent, superclass, searchIndex);
37+
Type superclassType = GenericUtils.getParameterType0(parent, superclass, searchIndex);
3838
if (superclassType instanceof TypeVariable<?> typeVariable && type instanceof ParameterizedType parameterizedType) {
3939
Class<?> rawSuperclass = (Class<?>) parameterizedType.getRawType();
4040
int index = GenericUtils.getParameterIndex(rawSuperclass.getTypeParameters(), typeVariable);
@@ -46,7 +46,7 @@ public static Type getParameterTypeFromSuperclass(Class<?> parent, Type type, Ty
4646
return superclassType;
4747
}
4848

49-
public static Type getParameterType(Class<?> parent, Type type, int index) {
49+
private static Type getParameterType0(Class<?> parent, Type type, int index) {
5050
if (type == null) {
5151
return null;
5252
}
@@ -81,4 +81,9 @@ public static Type getParameterType(Class<?> parent, Type type, int index) {
8181

8282
return null;
8383
}
84+
85+
public static Type getParameterType(Class<?> parent, Type type, int index) {
86+
Type parameterType = GenericUtils.getParameterType0(parent, type, index);
87+
return parameterType == null ? Object.class : parameterType;
88+
}
8489
}

0 commit comments

Comments
 (0)