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. springmvc配置aop

    直接看代码 springmvc中的配置aop对 controller和它的子包进行拦截 springmvc中的配置 <!--xml头部配置需要有这几行代码--> xmlns:aop=&qu ...

  2. tabBar 选中默认蓝色 ,取消选中(自定义)

    - (void)viewDidLoad { [super viewDidLoad]; //    [self _initSubViewControllers]; //    [self _custom ...

  3. 特殊函数(__all__)

    python里__all__ 属性分别于模块和包之中的用法 一. 在模块(*.py)中使用意为导出__all__列表里的类.函数.变量等成员,否则将导出modualA中所有不以下划线开头(私有)的成员 ...

  4. linux开发

    linux开发资料 01 02 03 04 05 06 07 08 09 10 11 1 2 3 4 5 21 22 23 24 25

  5. VPS服务器利用WINSCP软件进行SFTP管理服务器文件

    虽然我使用VPS时间也不是很久,但是我善于进行统计和分析,从我在的一些VPS交流QQ群中,可以看到基本上使用搬瓦工VPS的站长群中新手较多,甚至很多人都搞不明白VPS与V-P-N的区别都直接选择VPS ...

  6. JSON及与XML比较

      JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - Decem ...

  7. CodeForces 702B Powers of Two

    简单题. 开一个$map$记录一下每个数字出现了几次,那么读入的时候$f[a[i]]+1$. 计算$a[i]$做出的贡献的时候,先把$f[a[i]]-1$,然后再枚举$x$,答案加上$f[{2^x} ...

  8. HDU 5857 Median

    因为原序列是排列好了的,那么只要看一下给出的两个区间相交的情况,然后分类讨论一下,O(1)输出. #pragma comment(linker, "/STACK:1024000000,102 ...

  9. 我的linux云服务器配置记录

    配置vps的时候,随手记录一下~~ 添加一些源: vi /etc/apt/sources.list deb http://mirrors.aliyun.com/ubuntu/ xenial main ...

  10. 使用Toad创建存储过程出现错误并解决

    存储过程中遇到ora-00942表或视图不存在 CREATE OR REPLACE PROCEDURE p IS CURSOR c IS SELECT * FROM scott.emp FOR UPD ...