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

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);
  }

}

猜你喜欢

转载自my.oschina.net/u/3905482/blog/2249537