1,分析androidEventbus的注册源代码:

我们在使用androidEventbus的第一步是注册eventbus,如下代码:
  1. EventBus.getDefault().register(this);
首先获取eventbus对象,采用单利模式实现获取对象:
Eventbus.java里面
  1. public static EventBus getDefault() {
    if (sDefaultBus == null) {
    synchronized (EventBus.class) {
    if (sDefaultBus == null) {
    sDefaultBus = new EventBus();
    }
    }
    }
    return sDefaultBus;
    }


然后是:
  1.  public void register(Object subscriber) {
    if (subscriber == null) {
    return;
    }
    synchronized (this) {
    mMethodHunter.findSubcribeMethods(subscriber);
    }
    }


跟踪到mMethodHunter.findSubcribeMethods(subscriber);继续往下看:
mMethodHunter在代码头部注册:
  1.  /**
    * the subscriber method hunter, find all of the subscriber's methods
    * annotated with @Subcriber
    */
    SubsciberMethodHunter mMethodHunter =newSubsciberMethodHunter(mSubcriberMap);
用于查找所有使用@subcriber的注解方法
然后我们跟到findSubcribeMethods(subscriber)里面看看:
遍历
  1.  public void findSubcribeMethods(Object subscriber) {
    if (mSubcriberMap == null) {
    throw new NullPointerException("the mSubcriberMap is null. ");
    }
    Class<?> clazz = subscriber.getClass();
    // 查找类中符合要求的注册方法,直到Object类
    while (clazz != null && !isSystemCalss(clazz.getName())) {
    final Method[] allMethods = clazz.getDeclaredMethods();
    for (int i = 0; i < allMethods.length; i++) {
    Method method = allMethods[i];
    // 根据注解来解析函数
    Subscriber annotation = method.getAnnotation(Subscriber.class);
    if (annotation != null) {
    // 获取方法参数
    Class<?>[] paramsTypeClass = method.getParameterTypes();
    // 订阅函数只支持一个参数
    if (paramsTypeClass != null && paramsTypeClass.length == 1) {
    Class<?> paramType = convertType(paramsTypeClass[0]);
    EventType eventType = new EventType(paramType, annotation.tag());
    TargetMethod subscribeMethod = new TargetMethod(method, eventType,
    annotation.mode());
    subscibe(eventType, subscribeMethod, subscriber);
    }
    }
    } // end for
    // 获取父类,以继续查找父类中符合要求的方法
    clazz = clazz.getSuperclass();
    }
    }


然后再 subscibe(eventType, subscribeMethod, subscriber);方法里面的代码:
mSubcriberMap是个map集合
 
  1. /**
    * the event bus's subscriber's map
    */
    Map<EventType, CopyOnWriteArrayList<Subscription>> mSubcriberMap;

     /**
    * 按照EventType存储订阅者列表,这里的EventType就是事件类型,一个事件对应0到多个订阅者.
    *
    * @param event 事件
    * @param method 订阅方法对象
    * @param subscriber 订阅者
    */
    private void subscibe(EventType event, TargetMethod method, Object subscriber) {
    CopyOnWriteArrayList<Subscription> subscriptionLists = mSubcriberMap.get(event);
    if (subscriptionLists == null) {
    subscriptionLists = new CopyOnWriteArrayList<Subscription>();
    }
    Subscription newSubscription = new Subscription(subscriber, method);
    if (subscriptionLists.contains(newSubscription)) {
    return;
    }
    subscriptionLists.add(newSubscription);
    // 将事件类型key和订阅者信息存储到map中
    mSubcriberMap.put(event, subscriptionLists);
    }
 
到这里就可以看到register就是遍历所有注解@Subcriber的方法,并将事件类型key和订阅者信息存储在map中去。这点很类似eventbus代码中register,只不过eventbus是以以onEvent开头的方法去进行查找,而androideventbus是以@subcriber去进行遍历检索,但最终都是将事件类型key和订阅者信息存储在map中去。

【第四篇】androidEventbus源代码阅读和分析的更多相关文章

  1. 【第五篇】androidEventbus源代码阅读和分析之发送粘性事件和接收粘性事件代码分析

    代码里面发送粘性事件代码如下: // 发送Sticky事件 EventBus.getDefault().postSticky(new User("soyoungboy", &quo ...

  2. 【第五篇】androidEventbus源代码阅读和分析之unregister代码分析

    代码里面注销eventbus一般我们会在onDestory里面这么写: EventBus.getDefault().unregister(this); 然后走到unregister里面去看看: /** ...

  3. Tools - 源代码阅读分析工具Source Insight

    简介 https://www.sourceinsight.com/ Source Insight是一个面向项目开发的程序编辑器和代码浏览器,可以分析C/C++.C#.Java.Python等语言源代码 ...

  4. 非常好!!!Linux源代码阅读——中断【转】

    Linux源代码阅读——中断 转自:http://home.ustc.edu.cn/~boj/courses/linux_kernel/2_int.html 目录 为什么要有中断 中断的作用 中断的处 ...

  5. 【转】Tomcat总体结构(Tomcat源代码阅读系列之二)

    本文是Tomcat源代码阅读系列的第二篇文章,我们在本系列的第一篇文章:在IntelliJ IDEA 和 Eclipse运行tomcat 7源代码一文中介绍了如何在intelliJ IDEA 和 Ec ...

  6. CI框架源代码阅读笔记5 基准測试 BenchMark.php

    上一篇博客(CI框架源代码阅读笔记4 引导文件CodeIgniter.php)中.我们已经看到:CI中核心流程的核心功能都是由不同的组件来完毕的.这些组件类似于一个一个单独的模块,不同的模块完毕不同的 ...

  7. [转]madwifi无线网卡源代码阅读

    转自:http://xiyong8260.blog.163.com/blog/static/66514621200892465922669/ 在我的Doctor课题研究中,基于ARF协议设计了一个改进 ...

  8. 非常好!!!Linux源代码阅读——内核引导【转】

    Linux源代码阅读——内核引导 转自:http://home.ustc.edu.cn/~boj/courses/linux_kernel/1_boot.html 目录 Linux 引导过程综述 BI ...

  9. 【block第四篇】实现

    -------------------------------------------欢迎查看block连载博客[专栏]--------------------------------------[b ...

随机推荐

  1. BAPI_GOODSMVT_CREATE 移动类型311 CODE = '04' 代码

    DATA: MAT_DOC LIKE BAPI2017_GM_HEAD_RET-MAT_DOC.      "物料凭证编号   DATA: GMHEAD LIKE BAPI2017_GM_H ...

  2. xfs文件系统磁盘配额

    引言 这篇文章简单介绍一下xfs文件系统的磁盘配额配置. 文章目录 0×1.开启分区磁盘配额 0×2.使用xfs_quota命令配置磁盘配额 0×1.开启分区磁盘配额 对于ext4文件以前的文件系统, ...

  3. stm32 串口乱码的解决

    版权声明:本文为博主原创文章. 前几天在中移物联网申请了一个迷你开发板,运行官方提供的程序,感觉板子是正常的.但是自己写的程序能够刷到板子上,但是串口却是乱码.官方和我的额程序都是用的库函数的方式写的 ...

  4. Multi-Objective Data Placement for Multi-Cloud Socially Aware Services---INFOCOM 2014

    [标题] [作者] [来源] [对本文评价] [why] 存在的问题 [how] [不足] assumption in future work [相关方法或论文] [重点提示] [其它]

  5. 微信小程序官方demo学习

    最近微信小程序很火,很喜欢那种轻应用,用完就走的理念.于是,下载好微信开发者工具,学习一下官方demo. 体验下来,有类似react和vue的感觉,dom类似react那种组件的,data-bindi ...

  6. Linux用户相关命令

    1.建用户: adduser snailz //新建用户 snailz passwd snailz //给用户 snailz 设置密码 2.建工作组 groupadd test //新建test工作组 ...

  7. JavaEE XML 基础知识

    JavaEE XML 基础知识 @author ixenos 1.    XML开头都需要一个声明 <?和?>表明这是一个处理指令 <?xml version=”1.0” encod ...

  8. asp.net 批量删除

    直接上代码: 1.页面部分 <script type="text/javascript" src="http://code.jquery.com/jquery-1. ...

  9. HTTP Request GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE Methods

    注:本文为个人学习摘录,原文地址为:http://javaeedevelop.iteye.com/blog/1725299 An HTTP request is a class consisting ...

  10. HDU 2614 Beat(DFS)

    题目链接 Problem Description Zty is a man that always full of enthusiasm. He wants to solve every kind o ...