Mybatis源码分析篇------Reflector

栏目: 数据库 · 发布时间: 6年前

内容简介:Mybatis在进行参数处理、结果映射等操作时,会涉及很多反射的操作。Mybatis源码中的对应反射模块的部分叫做Reflector.首先我们先分清属性和字段这两个概念:下面我们就开始分析这个反射模块(注意:下面分析的源码的Mybatis的版本是3.4.6,如果有什么错误,欢迎大家在评论指出):源码图示

Mybatis在进行参数处理、结果映射等操作时,会涉及很多反射的操作。Mybatis源码中的对应反射模块的部分叫做Reflector.首先我们先分清属性和字段这两个概念:

  • 字段----类中定义的成员变量
  • 属性----属性则是通过Getter和Setter方法获得的,跟有没有这个成员变量没有关系。

下面我们就开始分析这个反射模块(注意:下面分析的源码的Mybatis的版本是3.4.6,如果有什么错误,欢迎大家在评论指出):

字段

Mybatis源码分析篇------Reflector

构造函数

Mybatis源码分析篇------Reflector
  • type :初始化type字段
  • addDefaultConstructor ():获取默认构造函数。
  • addGetMethods():处理目标class中的Getter方法,填充getMethods和getTypes字段。
  • addSetMethods():处理目标class中的Setter方法,填充setMethods和setTypes字段。
  • addFields():处理目标class中没有Getter和Setter方法的字段。
  • caseInsensitivePropertyMap:初始化caseinsensitivePropertyMap集合,其中记录了所有大写格式的属性名称。

addGetMethods()

源码图示

Mybatis源码分析篇------Reflector

大致流程:

  1. 首先获取当前目标类及其父类的定义的所有方法的唯一签名及其Method对象,对应的方法为getClassMethods()
    Mybatis源码分析篇------Reflector
Mybatis源码分析篇------Reflector
Mybatis源码分析篇------Reflector
  1. 当子类覆盖了父类的Getter方法且返回值发生了变化,在第1步就会出现两个不同的签名,这显然不符合我们的预期要求。所有便会调用resolveGetterConflicts()方法解决冲突
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());
        }
    }

以上所述就是小编给大家介绍的《Mybatis源码分析篇------Reflector》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

创业者手册

创业者手册

[美] 史蒂夫·布兰克、[美] 鲍勃·多夫 / 新华都商学院 / 机械工业出版社 / 2013-1 / 89.00元

我们发现,企业的成功程度和创始人使用本书的频繁程度成正比。书中折角越多,书被翻得越破,企业取得的成功就越显著。阅读本书切忌囫囵吞枣。 所有创业者都坚信自己的道路与众不同,他们在踏上创业之路时从不设计路线图,认为其他模式或模板并不适合自己。同样是初创企业,有些能够取得成功而有些只能沦落到廉价清库的下场,看起来这似乎是运气使然,然而事实并非如此。英雄成功的故事都是一样的。初创企业实现成功之路肯定......一起来看看 《创业者手册》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具