Mybatis在进行参数处理、结果映射等操作时,会涉及很多反射的操作。Mybatis源码中的对应反射模块的部分叫做Reflector.首先我们先分清属性和字段这两个概念:
下面我们就开始分析这个反射模块(注意:下面分析的源码的Mybatis的版本是3.4.6,如果有什么错误,欢迎大家在评论指出):
源码图示
大致流程:
private void resolveGetterConflicts(Map> conflictingGetters) { for (Entry> entry : conflictingGetters.entrySet()) { Method winner = null; String propName = entry.getKey(); for (Method candidate : entry.getValue()) { if (winner == null) { winner = candidate; continue; } Class winnerType = winner.getReturnType(); Class candidateType = candidate.getReturnType(); if (candidateType.equals(winnerType)) { if (!boolean.class.equals(candidateType)) { throw new ReflectionException( "Illegal overloaded getter method with ambiguous type for property " + propName + " in class " + winner.getDeclaringClass() + ". This breaks the JavaBeans specification and can cause unpredictable results."); } else if (candidate.getName().startsWith("is")) { winner = candidate; } } else if (candidateType.isAssignableFrom(winnerType)) { // OK getter type is descendant } else if (winnerType.isAssignableFrom(candidateType)) { winner = candidate; } else { throw new ReflectionException( "Illegal overloaded getter method with ambiguous type for property " + propName + " in class " + winner.getDeclaringClass() + ". This breaks the JavaBeans specification and can cause unpredictable results."); } } addGetMethod(propName, winner); } }
addSetMethods() 逻辑与 addGetMethods() 差不多,我就不在重复叙述了。
addFields()会将这些没有get set方法的字段添加到 setMethods() getMethods()的集合中,逻辑如下面:
private void addFields(Class clazz) { //获取目标class中的全部字段 Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (canAccessPrivateMethods()) { try { field.setAccessible(true); } catch (Exception e) { } } if (field.isAccessible()) { //判断在setmethod 中是否已经处在有这个属性了 if (!setMethods.containsKey(field.getName())) { int modifiers = field.getModifiers(); if (!(Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers))) { //如果这个属性不是Final或者静态,将其添加在setmethod和settype中 addSetField(field); } } if (!getMethods.containsKey(field.getName())) { //如果这个属性不是Final或者静态,将其添加在getmethod和gettype中 addGetField(field); } } } //检查是否有父类,继续递归执行该过程 if (clazz.getSuperclass() != null) { addFields(clazz.getSuperclass()); } }