This question already has an answer here:
I have a question about Java reflection and generic types.
If I do these instructions:
private static void test() {
List<String> list = new ArrayList<String>();
inspect(list.getClass());
}
private static void inspect(Class<?> clazz){
System.out.println(clazz.getGenericSuperclass());
Type genericSuperclass = clazz.getGenericSuperclass();
ParameterizedType pt = (ParameterizedType)genericSuperclass;
Type rawType = pt.getRawType();
Type type = pt.getActualTypeArguments()[0];
System.out.println(rawType + " "+type);
List<TypeVariable> tps = new ArrayList<TypeVariable>(Arrays.asList(clazz.getTypeParameters()));
tps.forEach(tp -> {
Type[] bounds = tp.getBounds();
System.out.println(bounds[0] + " " + tp.getTypeName());
});
}
the output is:
java.util.AbstractList<E>
class java.util.AbstractList E
class java.lang.Object E
I'd like to know if there is a way to obtain the original String.class type instead of a generic E element.
It is possible?
Thanks.
Aucun commentaire:
Enregistrer un commentaire