Java获取泛型实际类型

Published on 2023-01-12 14:36 in 分类: 随笔 with 狂盗一枝梅
分类: 随笔
@Service
public class ImformationModelEsServiceImpl extends BaseEsService<ImformationModuleEsEntity{
}

问:如何获取ImformationModelEsServiceImpl类中的泛型ImformationModuleEsEntity?

答:

Type genericSuperclass = this.getClass().getGenericSuperclass();
if (genericSuperclass instanceof ParameterizedType){
  ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
  Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
  for (Type actualTypeArgument : actualTypeArguments) {
    System.out.println(actualTypeArgument);
  }
}

#java
目录
复制 复制成功