1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > MyBatis源码分析-2-基础支持层-反射模块-TypeParameterResolver/ObjectFactory

MyBatis源码分析-2-基础支持层-反射模块-TypeParameterResolver/ObjectFactory

时间:2018-09-02 08:38:46

相关推荐

MyBatis源码分析-2-基础支持层-反射模块-TypeParameterResolver/ObjectFactory

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

TypeParameterResolver:

TypeParameterResolver的功能是:当存在复杂的继承关系以及泛型定义时, TypeParameterResolver 可以帮助我们解析字段、方法参数或方法返回值的类型。TypeParameterResolver 是在Refelctor中的addGetMethod方法中调用的,目的是获取方法的返回值类型。在Refelctor中的addSetMethod方法中调用的,目的是获取方法的参数类型。

private voidaddGetMethod(String name, Method method) {//检查属性名是否合法,检查条件方法名不以$开头,不等于serialVersionUID 不等于classif(isValidPropertyName(name)) {getMethods.put(name,newMethodInvoker(method));Type returnType = TypeParameterResolver.resolveReturnType(method,type);getTypes.put(name, typeToClass(returnType));}}

private voidaddSetMethod(String name, Method method) {if(isValidPropertyName(name)) {setMethods.put(name,newMethodInvoker(method));Type[] paramTypes = TypeParameterResolver.resolveParamTypes(method,type);setTypes.put(name, typeToClass(paramTypes[0]));}}

下面来看TypeParameterResolver具体实现:首先看resolveFieldType(),resolveReturnType(),resolveParamTypes()

此文已resolveFieldType为例讲解。

resolveFileType 第一步获取字段的声明类型,第二步 获取字段定义所在的类的Class对象。第三步resolveType是获取字段的类型

public staticType resolveFieldType(Field field, Type srcType) {//获取字段的声明类型Type fieldType = field.getGenericType();//获取字段定义所在的类的Class 对象Class<?> declaringClass = field.getDeclaringClass();returnresolveType(fieldType, srcType, declaringClass);}public staticType resolveReturnType(Method method, Type srcType) {Type returnType = method.getGenericReturnType();Class<?> declaringClass = method.getDeclaringClass();returnresolveType(returnType, srcType, declaringClass);}public staticType[] resolveParamTypes(Method method, Type srcType) {Type[] paramTypes = method.getGenericParameterTypes();Class<?> declaringClass = method.getDeclaringClass();Type[] result =newType[paramTypes.length];for(inti = 0; i < paramTypes.length; i++) {result[i] =resolveType(paramTypes[i], srcType, declaringClass);}returnresult;}

此处需要先介绍一下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 staticType resolveType(Type type, Type srcType, Class<?> declaringClass) {if(typeinstanceofTypeVariable) {returnresolveTypeVar((TypeVariable<?>) type, srcType, declaringClass);}else if(typeinstanceofParameterizedType) {returnresolveParameterizedType((ParameterizedType) type, srcType, declaringClass);}else if(typeinstanceofGenericArrayType) {returnresolveGenericArrayType((GenericArrayType) type, srcType, declaringClass);}else{returntype;}}

ObjectFactory

ObjectFactory 主要功能是:根据指定的参数列表查找构造函数,并实例化对象。

My Batis 中有很多模块会使用到ObjectFactory 接口,该接口提供了多个create()方法的重载,通过这些create()方法可以创建指定类型的对象。

public interfaceObjectFactory {/** * Sets configuration properties. 设置配置信息 *@parampropertiesconfiguration properties */voidsetProperties(Properties properties);/** * Creates a new object with default constructor. 通过无参构造器创建对象 *@paramtypeObject type *@return */<T> T create(Class<T> type);/** * Creates a new object with the specified constructor and params.根据参数列表,从指定类型中选择合适的构造器创建对象 *@paramtypeObject type *@paramconstructorArgTypesConstructor argument types *@paramconstructorArgsConstructor 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. * *@paramtypeObject type *@returnwhether it is a collection or not *@since3.1.0 */<T>booleanisCollection(Class<T> type);}

ObjectFactory只有一个默认实现DafaultObjectFactory,主要看instantiateClass()方法,实现的功能就是根据指定的参数列表查找构造函数,并实例化对象。

public classDefaultObjectFactoryimplementsObjectFactory, Serializable {private static final longserialVersionUID= -8855120656740914948L;@Overridepublic<T> T create(Class<T> type) {returncreate(type,null,null);}@SuppressWarnings("unchecked")@Overridepublic<T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {Class<?> classToCreate = resolveInterface(type);// we know types are assignablereturn(T) instantiateClass(classToCreate, constructorArgTypes, constructorArgs);}@Overridepublic voidsetProperties(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);}returnconstructor.newInstance();}//根据指定的参数列表查找构造函数,并实例化对象。 constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(newClass[constructorArgTypes.size()]));if(!constructor.isAccessible()) {constructor.setAccessible(true);}returnconstructor.newInstance(constructorArgs.toArray(newObject[constructorArgs.size()]));}catch(Exception e) {StringBuilder argTypes =newStringBuilder();if(constructorArgTypes !=null&& !constructorArgTypes.isEmpty()) {for(Class<?> argType : constructorArgTypes) {argTypes.append(argType.getSimpleName());argTypes.append(",");}argTypes.deleteCharAt(argTypes.length() - 1);// remove trailing , }StringBuilder argValues =newStringBuilder();if(constructorArgs !=null&& !constructorArgs.isEmpty()) {for(Object argValue : constructorArgs) {argValues.append(String.valueOf(argValue));argValues.append(",");}argValues.deleteCharAt(argValues.length() - 1);// remove trailing , }throw newReflectionException("Error instantiating "+ type +" with invalid types ("+ argTypes +") or values ("+ argValues +"). Cause: "+ e, e);}}protectedClass<?> 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;}returnclassToCreate;}@Overridepublic<T>booleanisCollection(Class<T> type) {returnCollection.class.isAssignableFrom(type);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。