【第四篇】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 static EventBus getDefault() {
- public void register(Object subscriber) {
- if (subscriber == null) {
- return;
- }
- synchronized (this) {
- mMethodHunter.findSubcribeMethods(subscriber);
- }
- }
- public void register(Object 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();
- }
- }
- public void findSubcribeMethods(Object subscriber) {
- /**
- * 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 ...
随机推荐
- WinForm 基础
今天,我开始学习了WinForm.WinForm是客户端程序制作 - C/S,它必须在.NET Framework框架上运行 . 开始,我先学习了一下WinForm的常用窗体属性. 布局:AutoSc ...
- 生成元(Digit Generator,ACM/ICPC Seoul 2005,UVa 1583)
#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;int t, n, a, ...
- iOS 解决文本(uitextfield/uitextView)在中间显示而不在顶部显示 问题
在对应的控制器中设置下面属性 self.automaticallyAdjustsScrollViewInsets = NO; 这样就好了.
- iOS 10 / Swift 3.0 / XCode 8 总结
1,iOS10 新增的privacy settings iOS10添加了新的权限控制范围 如果你尝试访问这些隐私数据时得到如下错误: > This app has crashed because ...
- Python--变量作用域
变量作用域: 一般在函数体外定义的变量成为全局变量,在函数内部定义的变量称为局部变量. 全局变量所有作用域都可读,局部变量只能在本函数可读 函数在读取变量时,优先读取函数本身自有的局部变量,再去读全局 ...
- CachedRowSet的用法
String sql="select item_code from xt_dictionary_item where type_id='32' and parent_itemid='0' o ...
- HTTP状态码含义
本文内容是在有道云笔记中找到的,已不知复制自何处,抱歉 一些常见的状态代码为:200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务器暂时不可用 1xx(临时响应) 用于表 ...
- gulp4个基础API
Gulp.src(globs[, options]) 此接口会匹配工作目录下指定规则的文件并返回提供给下一个插件管道使用.其中globs就是匹配格式,options是一些额外参数. gulp.src( ...
- NetPayClient for PHP使用说明
名 称 放置的路径 用 途 SecssUtil.class.php 根据项目工程的需要放置对应路径下 支持PHP5.4.8及以上版本 用于提供商户签名.验签.加密.解密.文件验签等方法调用 Mer.p ...
- Java中三种常见的注释(注解) Annotation
Java为我们提供了三种Annotation方便我们开发. 1 Override-函数覆写注解 如果我们想覆写Object的toString()方法,请看下面的代码: class Annotation ...