EventBus阅读源码(2)-SubscriberMethod
来源:互联网 发布:配眼镜需要的数据 编辑:程序博客网 时间:2024/05/22 02:27
如果说为什么选择源码的阅读顺序,这个其实没有什么跟据,我写在这里的顺序绝对不是我阅读的顺序,阅读的时候都是交叉的,很难说选的顺序正好是循序渐进的。而且一般都是找到一个入口来进行的,例如从EventBus类开始。
这适合我们开始的时候通览代码,当对代码有了一个大致的了解之后,如果要细细的分析,则要从根部着手。
这篇文章看
SubscriberMethod
该类只是基于了ThreadMode,没有基于其它的类,所以我们可以放心阅读。
Subscriber的意思是订阅者。顾名思义,就是只订阅者方法。猜想一下是不是像我们注册的方法呢(例如onMainThread(Event event))。
源码:
/* * Copyright (C) 2012-2016 Markus Junginger, greenrobot (http://greenrobot.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.greenrobot.eventbus;import java.lang.reflect.Method;/** Used internally by EventBus and generated subscriber indexes. */public class SubscriberMethod { final Method method;//方法 final ThreadMode threadMode;//该方法的模式,即在什么线程中执行 final Class<?> eventType;//事件类型,就是我们注册方式的时候的参数类型 final int priority;//优先级 final boolean sticky;//是否粘性的 /** Used for efficient comparison */ String methodString;//方法的字符串描述,主要用于比较 public SubscriberMethod(Method method, Class<?> eventType, ThreadMode threadMode, int priority, boolean sticky) { this.method = method; this.threadMode = threadMode; this.eventType = eventType; this.priority = priority; this.sticky = sticky; } @Override public boolean equals(Object other) {//比较两个方法是否相等,即是否是一个方法 if (other == this) { return true; } else if (other instanceof SubscriberMethod) { checkMethodString(); SubscriberMethod otherSubscriberMethod = (SubscriberMethod)other; otherSubscriberMethod.checkMethodString(); // Don't use method.equals because of http://code.google.com/p/android/issues/detail?id=7811#c6 return methodString.equals(otherSubscriberMethod.methodString); } else { return false; } } private synchronized void checkMethodString() {//计算方法的字符串描述 if (methodString == null) { // Method.toString has more overhead, just take relevant parts of the method StringBuilder builder = new StringBuilder(64); builder.append(method.getDeclaringClass().getName()); builder.append('#').append(method.getName()); builder.append('(').append(eventType.getName()); methodString = builder.toString(); } } @Override public int hashCode() { return method.hashCode(); }}
容易理解。
不做过多啰嗦。
0 0
- EventBus阅读源码(2)-SubscriberMethod
- EventBus源码阅读笔记(2)
- EventBus源码阅读(18)-EventBus-总括
- EventBus源码阅读(19)-EventBus初始化
- EventBus源码阅读(20)-EventBus注册
- EventBus源码阅读(21)-EventBus触发
- EventBus源码阅读(22)-EventBus注销
- EventBus源码阅读(23)-EventBus其它
- EventBus源码阅读(24)-EventBus-Sticky
- EventBus源码阅读(开篇)
- EventBus源码阅读(1)-ThreadMode
- EventBus源码阅读(3)-Subscribe
- EventBus源码阅读(4)-Subscription
- EventBus源码阅读(5)-SubscriberInfo
- EventBus源码阅读(6)-AbstractSubscriberInfo
- EventBus源码阅读(7)-SimpleSubscriberInfo
- EventBus源码阅读(8)-SubscriberMethodInfo
- EventBus源码阅读(9)-SubscriberInfoIndex
- js学习笔记-函数调用
- php+jquery+ajax简单Ajax调用示例
- hiberfil.sys的问题及解决方法之微软官方版
- 前端模块化开发的价值
- SNAT、DNAT、MASQUERADE的区别
- EventBus阅读源码(2)-SubscriberMethod
- UVa 1330 LA 3029
- HDU 1730 Northcott Game(Nim博弈变形)
- toj 4609 Internal Rate of Return
- 提取assets.car中的图片 .car文件的解压
- 【安卓开发】JAVA代码设置selector不同状态下的背景颜色
- navicat for mysql注册码,各种版本都有
- poj1745 Divisibility(动态规划经典题)
- 一步步学会使用SeaJS 2.0