image.png

2.1 解析器模块

2.1.1 XPath简介

image.png

2.1.2 XPathParser

image.png
XPathParser属性字段

  1. private Document document;
  2. private boolean validation;
  3. private EntityResolver entityResolver; // 用于加载本地DTD文件
  4. private Properties variables; // mybatis-config.xml中properties标签定义的键值对集合
  5. private XPath xpath;

image.png

Reflector

  1. public class Reflector {
  2. private static final String[] EMPTY_STRING_ARRAY = new String[0];
  3. private Class<?> type;
  4. private String[] readablePropertyNames = EMPTY_STRING_ARRAY;
  5. private String[] writeablePropertyNames = EMPTY_STRING_ARRAY;
  6. private Map<String, Invoker> setMethods = new HashMap<String, Invoker>();
  7. private Map<String, Invoker> getMethods = new HashMap<String, Invoker>();
  8. private Map<String, Class<?>> setTypes = new HashMap<String, Class<?>>();
  9. private Map<String, Class<?>> getTypes = new HashMap<String, Class<?>>();
  10. private Constructor<?> defaultConstructor;
  11. private Map<String, String> caseInsensitivePropertyMap = new HashMap<String, String>();
  12. public Reflector(Class<?> clazz) {
  13. type = clazz;
  14. addDefaultConstructor(clazz);
  15. addGetMethods(clazz);
  16. addSetMethods(clazz);
  17. addFields(clazz);
  18. readablePropertyNames = getMethods.keySet().toArray(new String[getMethods.keySet().size()]);
  19. writeablePropertyNames = setMethods.keySet().toArray(new String[setMethods.keySet().size()]);
  20. for (String propName : readablePropertyNames) {
  21. caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH), propName);
  22. }
  23. for (String propName : writeablePropertyNames) {
  24. caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH), propName);
  25. }
  26. }
  27. //....
  28. }

addMethod()和addField()在向上述集合添加元素时,会将getter/setter方法对于的Method对象以及字段对应的Field对象封装成Invoker对象,Invoker接口的定义如下:

  1. public interface Invoker {
  2. // 调用获取指定字段的值或执行指定的方法
  3. Object invoke(Object target, Object[] args) throws IllegalAccessException, InvocationTargetException;
  4. Class<?> getType(); // 返回属性相应的类型
  5. }

image.png
ReflectorFactory主要实现了对Reflector对象的创建和缓存,定义:

  1. public interface ReflectorFactory {
  2. boolean isClassCacheEnabled(); // 检测该ReflectorFactory对象是否会缓存Reflector对象
  3. void setClassCacheEnabled(boolean classCacheEnabled); // 设置是否缓存Reflector对象
  4. Reflector findForClass(Class<?> type); // 寻找或创建指定class对于的Reflector对象
  5. }
  1. public native boolean isAssignableFrom(Class<?> cls);

确定此 Class 对象表示的类或接口是否与指定的 Class 参数表示的类或接口相同,或者是其超类或超接口。

2.2.2 TypeParameterResolver

image.png

  • Class
  • ParameterizedType 表示参数化类型,例如List<String>, Map<Interger, String>, Service<User>
    • Type getRawType():返回参数化类型中的原始类型,例如List<String>的原始类型为List
    • Type[] getActualTypeArguments()
    • Type getOwnerType():返回所属类型,例如存在A<T>类,其中定义了内部类InnerA<T>,则InnerA<T>所属的类型为A<T>,如果是顶层类型则返回null
  • TypeVariable表示的是类型变量,它用来反映在JVM编译该泛型前的信息。例如List<T>中的T就是类型变量
    • Type[] getBounds():获取类型变量的上边界,如未声明上边界则默认为Object例如class Test<K extends Person>中K的上界就是Person
    • D getGenericDeclaration(): 获取声明该类型变量的原始类型
    • String getName():获取在源码中定义时的名字
  • GenericArrayType表示的是数组类型且组成元素是ParameterizedTypeTypeVariable
  • WildcardType表示的是通配符类型

TypeParameterResolver.resolverFieldType()

  1. public static Type resolveFieldType(Field field, Type srcType) {
  2. Type fieldType = field.getGenericType(); // 获取字段的声明类型
  3. Class<?> declaringClass = field.getDeclaringClass(); // 获取字段定义所在类的class对象
  4. return resolveType(fieldType, srcType, declaringClass);
  5. }
  1. private static Type resolveType(Type type, Type srcType, Class<?> declaringClass) {
  2. if (type instanceof TypeVariable) {
  3. return resolveTypeVar((TypeVariable<?>) type, srcType, declaringClass);
  4. } else if (type instanceof ParameterizedType) {
  5. return resolveParameterizedType((ParameterizedType) type, srcType, declaringClass);
  6. } else if (type instanceof GenericArrayType) {
  7. return resolveGenericArrayType((GenericArrayType) type, srcType, declaringClass);
  8. } else {
  9. return type;
  10. }
  11. }
  1. private static ParameterizedType resolveParameterizedType(ParameterizedType parameterizedType, Type srcType, Class<?> declaringClass) {
  2. Class<?> rawType = (Class<?>) parameterizedType.getRawType();
  3. Type[] typeArgs = parameterizedType.getActualTypeArguments();
  4. Type[] args = new Type[typeArgs.length];
  5. for (int i = 0; i < typeArgs.length; i++) {
  6. if (typeArgs[i] instanceof TypeVariable) {
  7. args[i] = resolveTypeVar((TypeVariable<?>) typeArgs[i], srcType, declaringClass);
  8. } else if (typeArgs[i] instanceof ParameterizedType) {
  9. args[i] = resolveParameterizedType((ParameterizedType) typeArgs[i], srcType, declaringClass);
  10. } else if (typeArgs[i] instanceof WildcardType) {
  11. args[i] = resolveWildcardType((WildcardType) typeArgs[i], srcType, declaringClass);
  12. } else {
  13. args[i] = typeArgs[i];
  14. }
  15. }
  16. return new ParameterizedTypeImpl(rawType, null, args);
  17. }