Spring 源碼是個大寶庫,咱們能遇到的大部分工具在源碼裏都能找到,因此筆者開源的 mica 徹底基於 Spring 進行基礎加強,不重複造輪子。今天我要分享的是在 Spring 中優雅的獲取泛型。html
咱們以前的處理方式,代碼來源 vjtools(江南白衣)。vue
/** * 經過反射, 得到Class定義中聲明的父類的泛型參數的類型. * * 注意泛型必須定義在父類處. 這是惟一能夠經過反射從泛型得到Class實例的地方. * * 如沒法找到, 返回Object.class. * * 如public UserDao extends HibernateDao<User,Long> * * @param clazz clazz The class to introspect * @param index the Index of the generic declaration, start from 0. * @return the index generic declaration, or Object.class if cannot be determined */
public static Class getClassGenericType(final Class clazz, final int index) {
Type genType = clazz.getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) {
logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
return Object.class;
}
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
if ((index >= params.length) || (index < 0)) {
logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
+ params.length);
return Object.class;
}
if (!(params[index] instanceof Class)) {
logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
return Object.class;
}
return (Class) params[index];
}
複製代碼
從 Spring 4.0 開始 Spring 中添加了 ResolvableType 工具,這個類能夠更加方便的用來回去泛型信息。 首先咱們來看看官方示例:java
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
}
複製代碼
ResolvableType.forField(Field)
複製代碼
ResolvableType.forMethodParameter(Method, int)
複製代碼
ResolvableType.forMethodReturnType(Method)
複製代碼
ResolvableType.forConstructorParameter(Constructor, int)
複製代碼
ResolvableType.forClass(Class)
複製代碼
ResolvableType.forType(Type)
複製代碼
ResolvableType.forInstance(Object)
複製代碼
更多使用 Api 請查看,ResolvableType java doc: docs.spring.io/spring-fram…git
mica
工具集:gitee.com/596392912/m…Avue
一款基於vue可配置化的神奇框架:gitee.com/smallweigit…pig
宇宙最強微服務(架構師必備):gitee.com/log4j/pigSpringBlade
完整的線上解決方案(企業開發必備):gitee.com/smallc/Spri…IJPay
支付SDK讓支付觸手可及:gitee.com/javen205/IJ…掃描上面二維碼,更多精彩內容天天推薦!spring