spring 中获取泛型

作者: Ian | 2019-04-02 | 阅读
private HashMap<Integer, List<String>> myMap;

 public void example() {
     ResolvableType t = ResolvableType.forField(getClass().getDeclaredField("myMap"));
     t.getSuperType(); // AbstractMap<Integer, List<String>>
     t.asMap(); // Map<Integer, List<String>>
     t.getGeneric(0).resolve(); // Integer
     t.getGeneric(1).resolve(); // List
     t.getGeneric(1); // List<String>
     t.resolveGeneric(1, 0); // String
 }

java doc地址:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/ResolvableType.html

可参考:https://my.oschina.net/lixin91/blog/677109

文章中若有不足之处,还望指出。坚持是一种精神,分享是一种快乐!



  相关文章:


留言区:

TOP