I am trying to get the type parameter of a List/Collection for my deserializer. I know I'm not the first asking and I saw in the answer this method to get the type parameter.
Class<?> persistentClass = (Class<?>) ((ParameterizedType) field.getType().getGenericSuperclass()).getActualTypeArguments()[0];
And it is not working form me. It is giving me a casting error saying that it cannot cast TypeVariableImpl to Class. And when I tried printing out the result it printed out just "E".
else if (Collection.class.isAssignableFrom(field.getType())) {
Collection collection = new ArrayList();
JSONArray array = toDeserialize.optJSONArray(fieldName);
if (array != null) {
for (int i = 0; i < array.length(); i++) {
Object val = array.get(i);
Class<E> persistentClass = (Class<E>) ((ParameterizedType) field.getType().getGenericSuperclass()).getActualTypeArguments()[0];
if (ISerializable.class.isAssignableFrom(persistentClass)) {
collection.add(deserialize(((Class<ISerializable>) persistentClass), (JSONObject) val));
} else collection.add(val);
}
}
try {
field.set(object, collection);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
This is the broken code.
I could I fix this?
Aucun commentaire:
Enregistrer un commentaire