【第四篇】androidEventbus源代码阅读和分析
1,分析androidEventbus的注册源代码:
EventBus.getDefault().register(this);
public static EventBus getDefault() {
if (sDefaultBus == null) {
synchronized (EventBus.class) {
if (sDefaultBus == null) {
sDefaultBus = new EventBus();
}
}
}
return sDefaultBus;
}
public void register(Object subscriber) {
if (subscriber == null) {
return;
}
synchronized (this) {
mMethodHunter.findSubcribeMethods(subscriber);
}
}
/**
* the subscriber method hunter, find all of the subscriber's methods
* annotated with @Subcriber
*/
SubsciberMethodHunter mMethodHunter =newSubsciberMethodHunter(mSubcriberMap);
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();
}
}
/**
* 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);
}
【第四篇】androidEventbus源代码阅读和分析的更多相关文章
- 【第五篇】androidEventbus源代码阅读和分析之发送粘性事件和接收粘性事件代码分析
代码里面发送粘性事件代码如下: // 发送Sticky事件 EventBus.getDefault().postSticky(new User("soyoungboy", &quo ...
- 【第五篇】androidEventbus源代码阅读和分析之unregister代码分析
代码里面注销eventbus一般我们会在onDestory里面这么写: EventBus.getDefault().unregister(this); 然后走到unregister里面去看看: /** ...
- Tools - 源代码阅读分析工具Source Insight
简介 https://www.sourceinsight.com/ Source Insight是一个面向项目开发的程序编辑器和代码浏览器,可以分析C/C++.C#.Java.Python等语言源代码 ...
- 非常好!!!Linux源代码阅读——中断【转】
Linux源代码阅读——中断 转自:http://home.ustc.edu.cn/~boj/courses/linux_kernel/2_int.html 目录 为什么要有中断 中断的作用 中断的处 ...
- 【转】Tomcat总体结构(Tomcat源代码阅读系列之二)
本文是Tomcat源代码阅读系列的第二篇文章,我们在本系列的第一篇文章:在IntelliJ IDEA 和 Eclipse运行tomcat 7源代码一文中介绍了如何在intelliJ IDEA 和 Ec ...
- CI框架源代码阅读笔记5 基准測试 BenchMark.php
上一篇博客(CI框架源代码阅读笔记4 引导文件CodeIgniter.php)中.我们已经看到:CI中核心流程的核心功能都是由不同的组件来完毕的.这些组件类似于一个一个单独的模块,不同的模块完毕不同的 ...
- [转]madwifi无线网卡源代码阅读
转自:http://xiyong8260.blog.163.com/blog/static/66514621200892465922669/ 在我的Doctor课题研究中,基于ARF协议设计了一个改进 ...
- 非常好!!!Linux源代码阅读——内核引导【转】
Linux源代码阅读——内核引导 转自:http://home.ustc.edu.cn/~boj/courses/linux_kernel/1_boot.html 目录 Linux 引导过程综述 BI ...
- 【block第四篇】实现
-------------------------------------------欢迎查看block连载博客[专栏]--------------------------------------[b ...
随机推荐
- android studio导入第三方源码模块
从网上得到的但三方源码模块,如果直接导入到自己的项目里的时候,可能需要比较长的时间,甚至不成功. 在导入之间,还是应该将模块里的 build.gradle 编辑一下,使其与自己的android stu ...
- use ContourPlot-使用ContourPlot
use ContourPlot to draw implicit function graphics 使用ContourPlot 画隐函数图像 for example $x^{3}+y^{3}-3xy ...
- vs2008编译wxWidgets 2.8.12
用vs2008编译wxWidgets 2.8.12 FileZilla客户端源码3.5.3及以上版本依赖wxWidgets 2.8.12或更高版本,因此,编译FileZilla客户端首先需要编译wxW ...
- lldb 命令
po [[UIWindow keyWindow] recursiveDescription]
- 变更mysql数据库文件目录 Linux
本次需要将mysql默认的数据库文件路径/var/lib/mysql 改为新挂载的目录/data/mysql上,需要做以下修改 1.停止mysql服务 service mysqld stop 2.复制 ...
- bootstrap复习:组件
一.下拉菜单 1.实例:将下拉菜单触发器和下拉菜单都包裹在 .dropdown 里,或者另一个声明了 position: relative; 的元素.然后加入组成菜单的 HTML 代码.为下拉菜单的父 ...
- hdu_5903_Square Distance(dp)
题目链接:hdu_5903_Square Distance 题意: 给你一个长度为n的a串,一个数m,现在让你构造一个长度也为n的b串,使这个串是由两个相同的串拼起来的,并且和a串对应的位不同的数量为 ...
- openwrt 汉化
make menuconfig 添加luci LuCI--->Collections----- <*> luci 添加luci的中文语言包 LuCI--->Translatio ...
- Visual Studio 2010 安装帮助文档问题
今天重装系统,装完VS2010后,如往常一样安装文档,却弹出如下错误"Could not create the local store in the specified folder.... ...
- 极光推送 PHP sdk
<?php defined('IN_WZ') or exit('No direct script access allowed'); /** * Created by PhpStorm. * U ...