2.1 解析器模块
2.1.1 XPath简介
2.1.2 XPathParser
XPathParser属性字段
private Document document;
private boolean validation;
private EntityResolver entityResolver; // 用于加载本地DTD文件
private Properties variables; // mybatis-config.xml中properties标签定义的键值对集合
private XPath xpath;
Reflector
public class Reflector {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private Class<?> type;
private String[] readablePropertyNames = EMPTY_STRING_ARRAY;
private String[] writeablePropertyNames = EMPTY_STRING_ARRAY;
private Map<String, Invoker> setMethods = new HashMap<String, Invoker>();
private Map<String, Invoker> getMethods = new HashMap<String, Invoker>();
private Map<String, Class<?>> setTypes = new HashMap<String, Class<?>>();
private Map<String, Class<?>> getTypes = new HashMap<String, Class<?>>();
private Constructor<?> defaultConstructor;
private Map<String, String> caseInsensitivePropertyMap = new HashMap<String, String>();
public Reflector(Class<?> clazz) {
type = clazz;
addDefaultConstructor(clazz);
addGetMethods(clazz);
addSetMethods(clazz);
addFields(clazz);
readablePropertyNames = getMethods.keySet().toArray(new String[getMethods.keySet().size()]);
writeablePropertyNames = setMethods.keySet().toArray(new String[setMethods.keySet().size()]);
for (String propName : readablePropertyNames) {
caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH), propName);
}
for (String propName : writeablePropertyNames) {
caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH), propName);
}
}
//....
}
addMethod()和addField()在向上述集合添加元素时,会将getter/setter方法对于的Method对象以及字段对应的Field对象封装成Invoker对象,Invoker接口的定义如下:
public interface Invoker {
// 调用获取指定字段的值或执行指定的方法
Object invoke(Object target, Object[] args) throws IllegalAccessException, InvocationTargetException;
Class<?> getType(); // 返回属性相应的类型
}
ReflectorFactory
主要实现了对Reflector对象的创建和缓存,定义:
public interface ReflectorFactory {
boolean isClassCacheEnabled(); // 检测该ReflectorFactory对象是否会缓存Reflector对象
void setClassCacheEnabled(boolean classCacheEnabled); // 设置是否缓存Reflector对象
Reflector findForClass(Class<?> type); // 寻找或创建指定class对于的Reflector对象
}
public native boolean isAssignableFrom(Class<?> cls);
确定此 Class 对象表示的类或接口是否与指定的 Class 参数表示的类或接口相同,或者是其超类或超接口。
2.2.2 TypeParameterResolver
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的上界就是PersonD getGenericDeclaration()
: 获取声明该类型变量的原始类型String getName()
:获取在源码中定义时的名字
GenericArrayType
表示的是数组类型且组成元素是ParameterizedType
或TypeVariable
WildcardType
表示的是通配符类型
TypeParameterResolver.resolverFieldType()
public static Type resolveFieldType(Field field, Type srcType) {
Type fieldType = field.getGenericType(); // 获取字段的声明类型
Class<?> declaringClass = field.getDeclaringClass(); // 获取字段定义所在类的class对象
return resolveType(fieldType, srcType, 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;
}
}
private static ParameterizedType resolveParameterizedType(ParameterizedType parameterizedType, Type srcType, Class<?> declaringClass) {
Class<?> rawType = (Class<?>) parameterizedType.getRawType();
Type[] typeArgs = parameterizedType.getActualTypeArguments();
Type[] args = new Type[typeArgs.length];
for (int i = 0; i < typeArgs.length; i++) {
if (typeArgs[i] instanceof TypeVariable) {
args[i] = resolveTypeVar((TypeVariable<?>) typeArgs[i], srcType, declaringClass);
} else if (typeArgs[i] instanceof ParameterizedType) {
args[i] = resolveParameterizedType((ParameterizedType) typeArgs[i], srcType, declaringClass);
} else if (typeArgs[i] instanceof WildcardType) {
args[i] = resolveWildcardType((WildcardType) typeArgs[i], srcType, declaringClass);
} else {
args[i] = typeArgs[i];
}
}
return new ParameterizedTypeImpl(rawType, null, args);
}