假设有个对象里有一个list<Object> listProperty 属性,要获取Object的类型:

Class<?> typeClass = field.getType();
if (List.class.isAssignableFrom(typeClass)) {
      Type fieldType = field.getGenericType();
       if (fieldType instanceof ParameterizedType) {
               ParameterizedType paramType = (ParameterizedType) fieldType;
               Type[] genericTypes = paramType.getActualTypeArguments();
               if (genericTypes != null && genericTypes.length > 0) {
                        if (genericTypes[0] instanceof Class<?>) {
                              Class<?> subType = (Class<?>) genericTypes[0];
                        }
               }
       }
}

其中subType就是Object的对象类型。

相关文章:

  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-26
  • 2021-08-19
  • 2021-11-22
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
  • 2022-12-23
  • 2022-12-23
  • 2022-03-07
相关资源
相似解决方案