Spring 中优雅的获取泛型信息

桃扇骨 2022-10-02 05:41 178阅读 0赞

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

简介

Spring 源码是个大宝库,我们能遇到的大部分工具在源码里都能找到,所以笔者开源的 mica 完全基于 Spring 进行基础增强,不重复造轮子。今天我要分享的是在 Spring 中优雅的获取泛型。

获取泛型

自己解析

我们之前的处理方式,代码来源 vjtools(江南白衣)。

  1. /**
  2. * 通过反射, 获得Class定义中声明的父类的泛型参数的类型.
  3. *
  4. * 注意泛型必须定义在父类处. 这是唯一可以通过反射从泛型获得Class实例的地方.
  5. *
  6. * 如无法找到, 返回Object.class.
  7. *
  8. * 如public UserDao extends HibernateDao<User,Long>
  9. *
  10. * @param clazz clazz The class to introspect
  11. * @param index the Index of the generic declaration, start from 0.
  12. * @return the index generic declaration, or Object.class if cannot be determined
  13. */
  14. public static Class getClassGenericType(final Class clazz, final int index) {
  15. Type genType = clazz.getGenericSuperclass();
  16. if (!(genType instanceof ParameterizedType)) {
  17. logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
  18. return Object.class;
  19. }
  20. Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
  21. if ((index >= params.length) || (index < 0)) {
  22. logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
  23. + params.length);
  24. return Object.class;
  25. }
  26. if (!(params[index] instanceof Class)) {
  27. logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
  28. return Object.class;
  29. }
  30. return (Class) params[index];
  31. }

ResolvableType 工具

从 Spring 4.0 开始 Spring 中添加了 ResolvableType 工具,这个类可以更加方便的用来回去泛型信息。 首先我们来看看官方示例:

  1. private HashMap<Integer, List<String>> myMap;
  2. public void example() {
  3. ResolvableType t = ResolvableType.forField(getClass().getDeclaredField("myMap"));
  4. t.getSuperType(); // AbstractMap<Integer, List<String>>
  5. t.asMap(); // Map<Integer, List<String>>
  6. t.getGeneric(0).resolve(); // Integer
  7. t.getGeneric(1).resolve(); // List
  8. t.getGeneric(1); // List<String>
  9. t.resolveGeneric(1, 0); // String
  10. }

详细说明

构造获取 Field 的泛型信息

  1. ResolvableType.forField(Field)

构造获取 Method 的泛型信息

  1. ResolvableType.forMethodParameter(Method, int)

构造获取方法返回参数的泛型信息

  1. ResolvableType.forMethodReturnType(Method)

构造获取构造参数的泛型信息

  1. ResolvableType.forConstructorParameter(Constructor, int)

构造获取类的泛型信息

  1. ResolvableType.forClass(Class)

构造获取类型的泛型信息

  1. ResolvableType.forType(Type)

构造获取实例的泛型信息

  1. ResolvableType.forInstance(Object)

更多使用 Api 请查看,ResolvableType java doc: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/ResolvableType.html

开源推荐

  • Spring boot 微服务高效开发 mica 工具集:https://gitee.com/596392912/mica
  • Avue 一款基于vue可配置化的神奇框架:https://gitee.com/smallweigit/avue
  • pig 宇宙最强微服务(架构师必备):https://gitee.com/log4j/pig
  • SpringBlade 完整的线上解决方案(企业开发必备):https://gitee.com/smallc/SpringBlade
  • IJPay 支付SDK让支付触手可及:https://gitee.com/javen205/IJPay
  • 加入【如梦技术】Spring QQ群:479710041,了解更多。

关注我们

如梦技术-公众号.jpg

扫描上面二维码,更多精彩内容每天推荐!

转载于:https://my.oschina.net/qq596392912/blog/3028409

发表评论

表情:
评论列表 (有 0 条评论,178人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java获取类型信息

    根据使用泛型位置的不同可以分为:声明侧泛型、使用侧泛型。 声明侧的泛型信息被记录在Class文件的Constant pool中以Signature的形式保存。而使用侧的泛型信