TypeParameterResolver:java
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为例讲解。app
resolveFileType 第一步获取字段的声明类型,第二步 获取字段定义所在的类的Class对象。第三步resolveType是获取字段的类型ide
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 。ui
如今看 resolveType(Type type, Type srcType, Class<?> declaringClass) 源码:主要是根据字段类型来匹配是属于哪一个类型的,而后返回this
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 spa
ObjectFactory 主要功能是:根据指定的参数列表查找构造函数,并实例化对象。对象
My Batis 中有不少模块会使用到ObjectFactory 接口,该接口提供了多个create()方法的重载,经过这些create()方法能够建立指定类型的对象。blog
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); } }