通过ParameterizedType获取泛型参数
可以通过获取类的超类的GenericSuperclass属性,并判断其是否为ParameterizedType,从中提取实际的类型参数。例如:
Type type = getClass().getGenericSuperclass();
if (type instanceof ParameterizedType) {
Type[] typeArgs = ((ParameterizedType) type).getActualTypeArguments();
}
这种方式适用于类继承时保留类型信息的情况。