Android事件总线框架Otto源码解析一(Bus的构造过程及在使用注解@Subscribe时的注册过程)

本文主要是介绍Android事件总线框架Otto源码解析一(Bus的构造过程及在使用注解@Subscribe时的注册过程),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 说明:本文的解析过程并不只是简单的讲解框架中各个方法的调用逻辑、程序的执行过程,而是依赖于示例代码,结合了具体的的示例程序的执行流程,有助于更好的理解。

有关Otto的使用方法参见另一篇文章: Android事件总线框架Otto使用介绍

package com.example.myotto;import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;import com.squareup.otto.Subscribe;public class MainActivity extends AppCompatActivity {private static final String TAG = MainActivity.class.getSimpleName();private Button mBtnJumpToPost, mBtnJumpToProduce, mBtnJumpToMainActivity;private TextView mTvMessage, mTvMessageUpdate;public static void start(Context context) {context.startActivity(new Intent(context, MainActivity.class));}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTvMessage = (TextView) this.findViewById(R.id.tv_message);mTvMessageUpdate = (TextView) this.findViewById(R.id.tv_message_update);mBtnJumpToPost = (Button) this.findViewById(R.id.btn_jump_to_post);mBtnJumpToMainActivity = (Button) this.findViewById(R.id.btn_jump_to_main_activity);mBtnJumpToProduce = (Button) this.findViewById(R.id.btn_jump_to_produce);mBtnJumpToPost.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {PostActivity.start(MainActivity.this);}});mBtnJumpToProduce.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {ProduceActivity.start(MainActivity.this);}});mBtnJumpToMainActivity.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {MainActivity.start(MainActivity.this);}});Log.i(TAG, "method:onCreate#this#hashCode=" + this.hashCode());OttoBus.getInstance().register(this);}@Subscribepublic void refreshMessage(EventData eventData) {Log.i(TAG, "method:Subscribe#refreshMessage#eventData#hashCode=" + eventData.hashCode());Log.i(TAG, "method:Subscribe#refreshMessage#eventData=" + eventData);mTvMessage.setText(eventData.getUserName() + ":\n\n" + eventData.getMessage());}@Subscribepublic void updateMessage(EventData eventData) {Log.i(TAG, "method:updateMessage#updateMessage#eventData#hashCode=" + eventData.hashCode());Log.i(TAG, "method:Subscribe#updateMessage#eventData=" + eventData);mTvMessageUpdate.setText(eventData.getUserName() + ":\n\n" + eventData.getMessage());}@Overrideprotected void onDestroy() {super.onDestroy();OttoBus.getInstance().unregister(this);}
}

 Bus的构造过程:

Otto源码中并没有提供Bus的单例实现,我们可以在使用前自行构造一个单例模式。

package com.example.myotto;import com.squareup.otto.Bus;public class OttoBus extends Bus {private volatile static OttoBus bus;private OttoBus() {}public static OttoBus getInstance() {if (bus == null) {synchronized (OttoBus.class) {if (bus == null) {bus = new OttoBus();}}}return bus;}
}

在源码中Bus类的无参构造方法,实际上最终还是调用了 Bus(ThreadEnforcer enforcer, String identifier, HandlerFinder handlerFinder)这个构造方法,并且在调用过程中注入了一些默认的参数 :DEFAULT_IDENTIFIER 、 ThreadEnforcer.MAIN 和HandlerFinder.ANNOTATED。

public class Bus {public static final String DEFAULT_IDENTIFIER = "default";/** All registered event handlers, indexed by event type. */private final ConcurrentMap<Class<?>, Set<EventHandler>> handlersByType =new ConcurrentHashMap<Class<?>, Set<EventHandler>>();/** All registered event producers, index by event type. */private final ConcurrentMap<Class<?>, EventProducer> producersByType =new ConcurrentHashMap<Class<?>, EventProducer>();/** Identifier used to differentiate the event bus instance. */private final String identifier;/** Thread enforcer for register, unregister, and posting events. */private final ThreadEnforcer enforcer;/** Used to find handler methods in register and unregister. */private final HandlerFinder handlerFinder;/** Queues of events for the current thread to dispatch. */private final ThreadLocal<ConcurrentLinkedQueue<EventWithHandler>> eventsToDispatch =new ThreadLocal<ConcurrentLinkedQueue<EventWithHandler>>() {@Override protected ConcurrentLinkedQueue<EventWithHandler> initialValue() {return new ConcurrentLinkedQueue<EventWithHandler>();}};/** True if the current thread is currently dispatching an event. */private final ThreadLocal<Boolean> isDispatching = new ThreadLocal<Boolean>() {@Override protected Boolean initialValue() {return false;}};public Bus() {this(DEFAULT_IDENTIFIER);}public Bus(String identifier) {this(ThreadEnforcer.MAIN, identifier);}public Bus(ThreadEnforcer enforcer) {this(enforcer, DEFAULT_IDENTIFIER);}public Bus(ThreadEnforcer enforcer, String identifier) {this(enforcer, identifier, HandlerFinder.ANNOTATED);}Bus(ThreadEnforcer enforcer, String identifier, HandlerFinder handlerFinder) {this.enforcer =  enforcer;this.identifier = identifier;this.handlerFinder = handlerFinder;}.......省略.......}

根据具体的示例分析 Bus 的 register(Object object) 方法在MainActivity的执行过程:

public class Bus{.......省略.......public void register(Object object) {if (object == null) {throw new NullPointerException("Object to register must not be null.");}// 检查线程,如果不是对应的线程则抛异常;默认的构造方法里使用的是主线程;// enforcer 就是构造过程中注入的默认参数 ThreadEnforcer.MAIN;// 它是 ThreadEnforcer 的一个实例,实现的 enforce(Bus bus)方法里会进行线程检查enforcer.enforce(this);  // 1// handlerFinder 就是构造过程中注入的默认参数 HandlerFinder.ANNOTATED;// 它是 HandlerFinder 的一个实例,实现了两个方法:findAllProducers(Object listener) 和 findAllSubscribers(Object listener);// findAllProducers(Object listener) 内部调用了 AnnotatedHandlerFinder 的 findAllProducers(Object listener);// 因为 MainActivity 中并没有 @Produce 的注解方法,所以这里返回的map大小是0,相关的逻辑也不再执行,接下来程序就会执行 handlerFinder.findAllSubscribers(object)方法;Map<Class<?>, EventProducer> foundProducers = handlerFinder.findAllProducers(object); // 2.......省略.......// handlerFinder 就是构造过程中注入的默认参数 HandlerFinder.ANNOTATED;// findAllSubscribers(Object listener) 内部调用了 AnnotatedHandlerFinder 的 findAllSubscribers(Object listener);// 这里返回的map是;Map<Class<?>, Set<EventHandler>> foundHandlersMap = handlerFinder.findAllSubscribers(object);  // 3for (Class<?> type : foundHandlersMap.keySet()) {Set<EventHandler> handlers = handlersByType.get(type);if (handlers == null) {//concurrent put if absentSet<EventHandler> handlersCreation = new CopyOnWriteArraySet<EventHandler>();handlers = handlersByType.putIfAbsent(type, handlersCreation);if (handlers == null) {handlers = handlersCreation;}}final Set<EventHandler> foundHandlers = foundHandlersMap.get(type);if (!handlers.addAll(foundHandlers)) {throw new IllegalArgumentException("Object already registered.");}}for (Map.Entry<Class<?>, Set<EventHandler>> entry : foundHandlersMap.entrySet()) {Class<?> type = entry.getKey();EventProducer producer = producersByType.get(type);if (producer != null && producer.isValid()) {Set<EventHandler> foundHandlers = entry.getValue();for (EventHandler foundHandler : foundHandlers) {if (!producer.isValid()) {break;}if (foundHandler.isValid()) {dispatchProducerResultToHandler(foundHandler, producer);}}}}}.......省略.......}

标注1处,即ThreadEnforcer类的处理逻辑:

public interface ThreadEnforcer {void enforce(Bus bus);ThreadEnforcer ANY = new ThreadEnforcer() {@Override public void enforce(Bus bus) {// Allow any thread.}};ThreadEnforcer MAIN = new ThreadEnforcer() {@Override public void enforce(Bus bus) {if (Looper.myLooper() != Looper.getMainLooper()) {throw new IllegalStateException("Event bus " + bus + " accessed from non-main thread " + Looper.myLooper());}}};}

HandlerFinder类的具体处理逻辑:

interface HandlerFinder {Map<Class<?>, EventProducer> findAllProducers(Object listener);Map<Class<?>, Set<EventHandler>> findAllSubscribers(Object listener);HandlerFinder ANNOTATED = new HandlerFinder() {@Overridepublic Map<Class<?>, EventProducer> findAllProducers(Object listener) {return AnnotatedHandlerFinder.findAllProducers(listener);}@Overridepublic Map<Class<?>, Set<EventHandler>> findAllSubscribers(Object listener) {return AnnotatedHandlerFinder.findAllSubscribers(listener);}};
}

标注2处具体调用的方法:

final class AnnotatedHandlerFinder {/** Cache event bus producer methods for each class. */private static final ConcurrentMap<Class<?>, Map<Class<?>, Method>> PRODUCERS_CACHE =new ConcurrentHashMap<Class<?>, Map<Class<?>, Method>>();/** Cache event bus subscriber methods for each class. */private static final ConcurrentMap<Class<?>, Map<Class<?>, Set<Method>>> SUBSCRIBERS_CACHE =new ConcurrentHashMap<Class<?>, Map<Class<?>, Set<Method>>>();.......省略........../** This implementation finds all methods marked with a {@link Produce} annotation. */static Map<Class<?>, EventProducer> findAllProducers(Object listener) { // listener 就是示例代码中的 MainActivity 的实例对象// final Class<?> listenerClass = listener.getClass();Map<Class<?>, EventProducer> handlersInMethod = new HashMap<Class<?>, EventProducer>();// 此时缓存中还没有,methods集合为null;Map<Class<?>, Method> methods = PRODUCERS_CACHE.get(listenerClass);if (null == methods) {methods = new HashMap<Class<?>, Method>();loadAnnotatedProducerMethods(listenerClass, methods);}// 因为当前MainActivity里只有Subject注解的方法,所在这个集合大小是0;if (!methods.isEmpty()) {for (Map.Entry<Class<?>, Method> e : methods.entrySet()) {EventProducer producer = new EventProducer(listener, e.getValue());handlersInMethod.put(e.getKey(), producer);}}// 当前这个返回结果集合大小是0;return handlersInMethod;}.......省略..........}

 loadAnnotatedProducerMethods(Class<?> listenerClass, Map<Class<?>, Method> producerMethods) 方法,最终调用loadAnnotatedMethods(Class<?> listenerClass,
      Map<Class<?>, Method> producerMethods, Map<Class<?>, Set<Method>> subscriberMethods) 这个方法

.......省略.........private static void loadAnnotatedProducerMethods(Class<?> listenerClass,Map<Class<?>, Method> producerMethods) {Map<Class<?>, Set<Method>> subscriberMethods = new HashMap<Class<?>, Set<Method>>();loadAnnotatedMethods(listenerClass, producerMethods, subscriberMethods);}/*** Load all methods annotated with {@link Produce} or {@link Subscribe} into their respective caches for the* specified class.*/private static void loadAnnotatedMethods(Class<?> listenerClass,Map<Class<?>, Method> producerMethods, Map<Class<?>, Set<Method>> subscriberMethods) { // 传入的参数: 图1for (Method method : listenerClass.getDeclaredMethods()) {// The compiler sometimes creates synthetic bridge methods as part of the// type erasure process. As of JDK8 these methods now include the same// annotations as the original declarations. They should be ignored for// subscribe/produce.// 如果桥接方法,则不处理if (method.isBridge()) {continue;}// 判断方法是否以@Subscribe为注解if (method.isAnnotationPresent(Subscribe.class)) {Class<?>[] parameterTypes = method.getParameterTypes();// 方法的参数类型的数量只能有1个,否则抛出异常if (parameterTypes.length != 1) {throw new IllegalArgumentException("Method " + method + " has @Subscribe annotation but requires "+ parameterTypes.length + " arguments.  Methods must require a single argument.");}Class<?> eventType = parameterTypes[0];if (eventType.isInterface()) {throw new IllegalArgumentException("Method " + method + " has @Subscribe annotation on " + eventType+ " which is an interface.  Subscription must be on a concrete class type.");}// 方法的访问修饰符必须是public;if ((method.getModifiers() & Modifier.PUBLIC) == 0) {throw new IllegalArgumentException("Method " + method + " has @Subscribe annotation on " + eventType+ " but is not 'public'.");}Set<Method> methods = subscriberMethods.get(eventType);if (methods == null) {methods = new HashSet<Method>();subscriberMethods.put(eventType, methods);}methods.add(method);// 判断方法是否以@Produce为注解} else if (method.isAnnotationPresent(Produce.class)) {Class<?>[] parameterTypes = method.getParameterTypes();if (parameterTypes.length != 0) {throw new IllegalArgumentException("Method " + method + "has @Produce annotation but requires "+ parameterTypes.length + " arguments.  Methods must require zero arguments.");}// 方法的必须要有返回参数;if (method.getReturnType() == Void.class) {throw new IllegalArgumentException("Method " + method+ " has a return type of void.  Must declare a non-void type.");}Class<?> eventType = method.getReturnType();if (eventType.isInterface()) {throw new IllegalArgumentException("Method " + method + " has @Produce annotation on " + eventType+ " which is an interface.  Producers must return a concrete class type.");}if (eventType.equals(Void.TYPE)) {throw new IllegalArgumentException("Method " + method + " has @Produce annotation but has no return type.");}// 方法的访问修饰符必须是public;if ((method.getModifiers() & Modifier.PUBLIC) == 0) {throw new IllegalArgumentException("Method " + method + " has @Produce annotation on " + eventType+ " but is not 'public'.");}if (producerMethods.containsKey(eventType)) {throw new IllegalArgumentException("Producer for type " + eventType + " has already been registered.");}producerMethods.put(eventType, method);}}PRODUCERS_CACHE.put(listenerClass, producerMethods);SUBSCRIBERS_CACHE.put(listenerClass, subscriberMethods);}.......省略.........

图1:

当查找到第一个以 Subscribe 为注解的 refreshMessage(EventData eventData)方法时,数据的处理流程:

1、判断参数个数

2、判断参数类型是否是接口类型

3、保存到对应的集合中

当查找到第二个以 Subscribe 为注解的 updateMessage(EventData eventData)方法时,数据的处理流程:

查找完所有以 Subscribe 为注解的方法后,再缓存入这个集合中:

总结一下上述方法中出现的集合存储的数据类型:

Map<Class<?>, Set<Method>> subscriberMethods 集合存入的数据类型:

key 是 EventData 的Class对象

value 是Set<Method>集合,比如:refreshMessage(EventData eventData)方法 和 updateMessage(EventData eventData)方法;

ConcurrentMap<Class<?>, Map<Class<?>, Set<Method>>> SUBSCRIBERS_CACHE 集合存入的数据可以是:

key 是 MainActivity 的Class对象

value 是 subscriberMethods 集合;

 标注3处最终调用的方法:

final class AnnotatedHandlerFinder {/** Cache event bus producer methods for each class. */private static final ConcurrentMap<Class<?>, Map<Class<?>, Method>> PRODUCERS_CACHE =new ConcurrentHashMap<Class<?>, Map<Class<?>, Method>>();/** Cache event bus subscriber methods for each class. */private static final ConcurrentMap<Class<?>, Map<Class<?>, Set<Method>>> SUBSCRIBERS_CACHE =new ConcurrentHashMap<Class<?>, Map<Class<?>, Set<Method>>>();.......省略........../** This implementation finds all methods marked with a {@link Subscribe} annotation. */static Map<Class<?>, Set<EventHandler>> findAllSubscribers(Object listener) {Class<?> listenerClass = listener.getClass(); Map<Class<?>, Set<EventHandler>> handlersInMethod = new HashMap<Class<?>, Set<EventHandler>>();// 此时缓存中已经有数据,如果没有,最终还会调用上述的查找所有Subscribe注解的方法Map<Class<?>, Set<Method>> methods = SUBSCRIBERS_CACHE.get(listenerClass);if (null == methods) {methods = new HashMap<Class<?>, Set<Method>>();loadAnnotatedSubscriberMethods(listenerClass, methods);}if (!methods.isEmpty()) {// 遍历这个集合,将数据封装到EventHandler的对象中,然后存入Map<Class<?>, Set<EventHandler>>集合中for (Map.Entry<Class<?>, Set<Method>> e : methods.entrySet()) {Set<EventHandler> handlers = new HashSet<EventHandler>();for (Method m : e.getValue()) {handlers.add(new EventHandler(listener, m));}handlersInMethod.put(e.getKey(), handlers);}}return handlersInMethod;}.......省略..........}

总结一下这段代码执行完之后的数据情况:

循环遍历了集合 Map<Class<?>, Set<Method>> subscriberMethods 集合存入的数据可以是:

key 是 EventData 的Class对象

value 是Set<Method>集合,比如:refreshMessage(EventData eventData)方法 和 updateMessage(EventData eventData)方法

找到每一个key下对应的Set<Method>集合,然后遍历这个集合,将相应数据封装到 EventHandler 对象中,并且存入Set<EventHandler > 集合中,里层循环结束后,再将数据存入 Map<Class<?>, Set<EventHandler>> handlersInMethod 集合中;

EventHandler 对象创建时注入的参数 new EventHandler(listener, m);listener 是 MainActivity的Class对象,m 是 Subscribe注释方法 refreshMessage(EventData eventData)方法;

 Map<Class<?>, Set<EventHandler>> handlersInMethod 集合的 key 是 EventData 的Class对象,value 是 Set<EventHandler > 集合;

 下面我们回到Bus里标注3的返回结果:

public class Bus{.......省略.......public void register(Object object) {.......省略.......Map<Class<?>, Set<EventHandler>> foundHandlersMap = handlerFinder.findAllSubscribers(object); // 3for (Class<?> type : foundHandlersMap.keySet()) {// 此时handlersByType集合还是0,查找出的结果集合还是null;Set<EventHandler> handlers = handlersByType.get(type);if (handlers == null) {//concurrent put if absentSet<EventHandler> handlersCreation = new CopyOnWriteArraySet<EventHandler>();// 此时key对应的value还不存在,此方法会添加key和value,并且返回nullhandlers = handlersByType.putIfAbsent(type, handlersCreation);if (handlers == null) {// 将 CopyOnWriteArraySet类型的集合对象 handlersCreation 赋值给 handlershandlers = handlersCreation;}}final Set<EventHandler> foundHandlers = foundHandlersMap.get(type);// 如果Set集合发生更改,则返回true。if (!handlers.addAll(foundHandlers)) {throw new IllegalArgumentException("Object already registered.");}}.......省略.......}

总结一下数据情况:

ConcurrentMap<Class<?>, Set<EventHandler>> handlersByType 的 key 是 EventData 的Class对象,value 是 Set<EventHandler > 集合;

到此注册方法的执行流程已经完成。

示例代码GitHub地址

由于作者水平有限,语言描述及代码实现中难免有纰漏,望各位看官多提宝贵意见!

Hello , World !

感谢所有!

这篇关于Android事件总线框架Otto源码解析一(Bus的构造过程及在使用注解@Subscribe时的注册过程)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/404851

相关文章

网页解析 lxml 库--实战

lxml库使用流程 lxml 是 Python 的第三方解析库,完全使用 Python 语言编写,它对 XPath表达式提供了良好的支 持,因此能够了高效地解析 HTML/XML 文档。本节讲解如何通过 lxml 库解析 HTML 文档。 pip install lxml lxm| 库提供了一个 etree 模块,该模块专门用来解析 HTML/XML 文档,下面来介绍一下 lxml 库

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

作业提交过程之HDFSMapReduce

作业提交全过程详解 (1)作业提交 第1步:Client调用job.waitForCompletion方法,向整个集群提交MapReduce作业。 第2步:Client向RM申请一个作业id。 第3步:RM给Client返回该job资源的提交路径和作业id。 第4步:Client提交jar包、切片信息和配置文件到指定的资源提交路径。 第5步:Client提交完资源后,向RM申请运行MrAp

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

禁止平板,iPad长按弹出默认菜单事件

通过监控按下抬起时间差来禁止弹出事件,把以下代码写在要禁止的页面的页面加载事件里面即可     var date;document.addEventListener('touchstart', event => {date = new Date().getTime();});document.addEventListener('touchend', event => {if (new

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听