MyBatis源码分析-2-基础支持层-反射模块-TypeParameterResolver/ObjectFactory
2019獨角獸企業重金招聘Python工程師標準>>>
TypeParameterResolver:
TypeParameterResolver的功能是:當存在復雜的繼承關系以及泛型定義時, TypeParameterResolver 可以幫助我們解析字段、方法參數或方法返回值的類型。TypeParameterResolver 是在Refelctor中的addGetMethod方法中調用的,目的是獲取方法的返回值類型。在Refelctor中的addSetMethod方法中調用的,目的是獲取方法的參數類型。
private void addGetMethod(String name, Method method) {//檢查屬性名是否合法,檢查條件方法名不以$開頭,不等于serialVersionUID 不等于class if (isValidPropertyName(name)) {getMethods.put(name, new MethodInvoker(method));Type returnType = TypeParameterResolver.resolveReturnType(method, type);getTypes.put(name, typeToClass(returnType));} } private void addSetMethod(String name, Method method) {if (isValidPropertyName(name)) {setMethods.put(name, new MethodInvoker(method));Type[] paramTypes = TypeParameterResolver.resolveParamTypes(method, type);setTypes.put(name, typeToClass(paramTypes[0]));} }下面來看TypeParameterResolver具體實現:首先看resolveFieldType(),resolveReturnType(),resolveParamTypes()
此文已resolveFieldType為例講解。
resolveFileType 第一步獲取字段的聲明類型,第二步 獲取字段定義所在的類的Class對象。第三步resolveType是獲取字段的類型
public static Type resolveFieldType(Field field, Type srcType) {//獲取字段的聲明類型 Type fieldType = field.getGenericType();//獲取字段定義所在的類的Class 對象 Class<?> declaringClass = field.getDeclaringClass();return resolveType(fieldType, srcType, declaringClass); } public static Type resolveReturnType(Method method, Type srcType) {Type returnType = method.getGenericReturnType();Class<?> declaringClass = method.getDeclaringClass();return resolveType(returnType, srcType, declaringClass); } public static Type[] resolveParamTypes(Method method, Type srcType) {Type[] paramTypes = method.getGenericParameterTypes();Class<?> declaringClass = method.getDeclaringClass();Type[] result = new Type[paramTypes.length];for (int i = 0; i < paramTypes.length; i++) {result[i] = resolveType(paramTypes[i], srcType, declaringClass);}return result; }?
此處需要先介紹一下Type接口,Type是所有類型的父接口,它有四個子類和一個實現類。
Class 比較常見,它表示的是原始類型。Class 類的對象表示NM 中的一個類或接口,每個Java 類在NM 里都表現為一個Class 對象。在程序中可以通過“類名.class ”、“對象.getC!ass()”或是Class 對象,所有元素類型相同且維數相同的數組都共享同一個Class 對象。
ParameterizedType 表示的是參數化類型,例如List<String> 、Map<Integer,String>、Service<U ser>這種帶有泛型的類型。
Type Variable 表示的是類型變量,它用來反映在NM 編譯該泛型前的信息。例如List<T>中的T 就是類型變量,它在編譯時需被轉換為一個具體的類型后才能正常使用。
GenericArrayType 表示的是數組類型且組成元素是ParameterizedType 或Type Variable .例如List<String>[]或T [] 。該接口只有Type getGenericComponentType () 一個方法,它返回數組的組成元素。
WildcardType 表示的是通配符泛型,例如? extends Number 和? super Integer 。
現在看 resolveType(Type type, Type srcType, Class<?> declaringClass) 源碼:主要是根據字段類型來匹配是屬于哪個類型的,然后返回
private static Type resolveType(Type type, Type srcType, Class<?> declaringClass) {if (type instanceof TypeVariable) {return resolveTypeVar((TypeVariable<?>) type, srcType, declaringClass);} else if (type instanceof ParameterizedType) {return resolveParameterizedType((ParameterizedType) type, srcType, declaringClass);} else if (type instanceof GenericArrayType) {return resolveGenericArrayType((GenericArrayType) type, srcType, declaringClass);} else {return type;} }?
ObjectFactory??
?? ObjectFactory 主要功能是:根據指定的參數列表查找構造函數,并實例化對象。
?? My Batis 中有很多模塊會使用到ObjectFactory 接口,該接口提供了多個create()方法的重載,通過這些create()方法可以創建指定類型的對象。
public interface ObjectFactory {/** * Sets configuration properties. 設置配置信息 * @param properties configuration properties */ void setProperties(Properties properties);/** * Creates a new object with default constructor. 通過無參構造器創建對象 * @param type Object type * @return */ <T> T create(Class<T> type);/** * Creates a new object with the specified constructor and params.根據參數列表,從指定類型中選擇合適的構造器創建對象 * @param type Object type * @param constructorArgTypes Constructor argument types * @param constructorArgs Constructor argument values * @return */ <T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs);/**檢測指定類型是否為集合類型,主要處理java.util.Collection及其子類。 * Returns true if this object can have a set of other objects. * It's main purpose is to support non-java.util.Collection objects like Scala collections. * * @param type Object type * @return whether it is a collection or not * @since 3.1.0 */ <T> boolean isCollection(Class<T> type);}ObjectFactory只有一個默認實現DafaultObjectFactory,主要看instantiateClass()方法,實現的功能就是 根據指定的參數列表查找構造函數,并實例化對象。
public class DefaultObjectFactory implements ObjectFactory, Serializable {private static final long serialVersionUID = -8855120656740914948L;@Override public <T> T create(Class<T> type) {return create(type, null, null);}@SuppressWarnings("unchecked")@Override public <T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {Class<?> classToCreate = resolveInterface(type);// we know types are assignable return (T) instantiateClass(classToCreate, constructorArgTypes, constructorArgs);}@Override public void setProperties(Properties properties) {// no props for default }private <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {try {//聲明構造方法 Constructor<T> constructor;//如果參數列表為null,通過無參構造函數創建對象。 if (constructorArgTypes == null || constructorArgs == null) {constructor = type.getDeclaredConstructor();if (!constructor.isAccessible()) {constructor.setAccessible(true);}return constructor.newInstance();}//根據指定的參數列表查找構造函數,并實例化對象。 constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[constructorArgTypes.size()]));if (!constructor.isAccessible()) {constructor.setAccessible(true);}return constructor.newInstance(constructorArgs.toArray(new Object[constructorArgs.size()]));} catch (Exception e) {StringBuilder argTypes = new StringBuilder();if (constructorArgTypes != null && !constructorArgTypes.isEmpty()) {for (Class<?> argType : constructorArgTypes) {argTypes.append(argType.getSimpleName());argTypes.append(",");}argTypes.deleteCharAt(argTypes.length() - 1); // remove trailing , }StringBuilder argValues = new StringBuilder();if (constructorArgs != null && !constructorArgs.isEmpty()) {for (Object argValue : constructorArgs) {argValues.append(String.valueOf(argValue));argValues.append(",");}argValues.deleteCharAt(argValues.length() - 1); // remove trailing , }throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values (" + argValues + "). Cause: " + e, e);}}protected Class<?> resolveInterface(Class<?> type) {Class<?> classToCreate;if (type == List.class || type == Collection.class || type == Iterable.class) {classToCreate = ArrayList.class;} else if (type == Map.class) {classToCreate = HashMap.class;} else if (type == SortedSet.class) { // issue #510 Collections Support classToCreate = TreeSet.class;} else if (type == Set.class) {classToCreate = HashSet.class;} else {classToCreate = type;}return classToCreate;}@Override public <T> boolean isCollection(Class<T> type) {return Collection.class.isAssignableFrom(type);}}轉載于:https://my.oschina.net/u/3905482/blog/2249537
總結
以上是生活随笔為你收集整理的MyBatis源码分析-2-基础支持层-反射模块-TypeParameterResolver/ObjectFactory的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蒙特卡洛定积分(一)
- 下一篇: Spring Bean生命周期的各阶段介