MetaObject基本架构
MetaObject获取属性值流程
源码分析MetaObject获取属性值
public class MetaObjectTest {
@Test
public void test1(){
Object blog = new Blog();
Configuration configuration = new Configuration();
MetaObject metaObject = configuration.newMetaObject(blog);
User user = new User();
user.setName("xxppp");
ArrayList<Comment> comments = new ArrayList<>();
Comment comment = new Comment();
comment.setUser(user);
comments.add(comment);
metaObject.setValue("comments",comments);
metaObject.getValue("comments[0].user.name");
}
}
经过属性分词器解析
此时hasNext()为true表示存在子属性
调用getValue,此时参数和上一次已经不一样了。为comments[0],此时没有子属性,直接调用get方法获取comments[0]的值。
再将获取到的comments[0]值包装为MetaObject。
继续调用getValue(),此时参数为user.name。
此时是有子属性的,为name,会进入metaObjectForProperty()。
此时user没有子属性,调用get方法获取user值
继续调用getValue(),此时参数是name,没有子属性,直接调用get()获取name值
对BeanWrapper【get】方法单独分析
再次出现调用getValue()
总结:获取属性主要是利用MetaObject中的属性分词器,和反射原理
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/1264.html