EventBus原理解析
1. 注册EventBus
将一个类注册为事件的订阅者分两步
- EventBus.getDefault().register(this)注册类为事件的订阅类
- 注解@Subscribe来定义真正的消费事件的订阅方法
下面展示了如何注册EventBus以及订阅事件,MyEvent为我们自定义事件类,类似与Handler类中的消息载体Message对象
public class EventActivity extends Activity(){
onCreate(){
EventBus.getDefault().register(this);
}
@Subscribe
public void onChangeEvent(MyEvent event){
}
}
public class MyEvent extends Event{
}
EventBus使用默认的EventBusBuilder进行了初始化,创建了跨进程级别的单例对象EventBus。其中构造方法中,我们需要关注的,是创建了SubscriberMethodFinder对象,对成员变量subscriberInfoIndexes与ignoreGeneratedIndex进行了赋值操作。后面解析注册类(EventActivity)中的订阅方法时,会用到这些变量
public class EventBus {
private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();
public static EventBus getDefault() {
if (defaultInstance == null) {
synchronized (EventBus.class) {
if (defaultInstance == null) {
defaultInstance = new EventBus();
}
}
}
return defaultInstance;
}
public EventBus() {
this(DEFAULT_BUILDER);
}
EventBus(EventBusBuilder builder) {
...
subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
builder.strictMethodVerification, builder.ignoreGeneratedIndex);
}
}
register()真正将一个类注册为事件的订阅类,首先获取到当前类的class文件,然后调用findSubscriberMethods解析出类中所有的订阅方法,然后调用subscribe()进行订阅操作
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
具体看一下SubscriberMethodFinder类,是如何通过class字节码解析出类中订阅方法的。首先会通过findSubscriberMethods方法,去缓存中查看是否已经解析过当前class,若已经解析过则直接返回,否则会调用findUsingInfo(subscriberClass)去进行解析操作,然后更新订阅方法缓存METHOD_CACHE。
注:因为默认DEFAULT_BUILDER中ignoreGeneratedIndex为false,所以不会执行findUsingReflection(subscriberClass);只有自定义EventBusBuilder创建EventBus,并主动设定ignoreGeneratedIndex = true,才会执行此方法。
private static final Map<Class<?>, List<SubscriberMethod>> METHOD_CACHE = new ConcurrentHashMap<>();
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) {
return subscriberMethods;
}
if (ignoreGeneratedIndex) {
subscriberMethods = findUsingReflection(subscriberClass);
} else {
subscriberMethods = findUsingInfo(subscriberClass);
}
if (subscriberMethods.isEmpty()) {
throw new EventBusException("Subscriber " + subscriberClass
+ " and its super classes have no public methods with the @Subscribe annotation");
} else {
METHOD_CACHE.put(subscriberClass, subscriberMethods);
return subscriberMethods;
}
}
这里我们先看一下FindState类, FindState对象内部以map形式存储了订阅类,订阅方法的各种信息,后续解析时都是以FindState为媒介进行的
static class FindState {
final List<SubscriberMethod> subscriberMethods = new ArrayList<>();
final Map<Class, Object> anyMethodByEventType = new HashMap<>();
final Map<String, Class> subscriberClassByMethodKey = new HashMap<>();
void initForSubscriber(Class<?> subscriberClass) {
this.subscriberClass = clazz = subscriberClass;
skipSuperClasses = false;
subscriberInfo = null;
}
}
findUsingInfo(subscriberClass)方法会先将创建FindState对象,并将订阅类存入成员变量,然后通过getSubscriberInfo(findState)方法,获取subscriberInfo,第一次调用时会返回null(详见下一步),会继续findUsingReflectionInSingleClass(findState);
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
findState.subscriberInfo = getSubscriberInfo(findState);
if (findState.subscriberInfo != null) {
SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
for (SubscriberMethod subscriberMethod : array) {
if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
findState.subscriberMethods.add(subscriberMethod);
}
}
} else {
findUsingReflectionInSingleClass(findState);
}
findState.moveToSuperclass();
}
return getMethodsAndRelease(findState);
}
此处findState.subscriberInfo为null,subscriberInfoIndexes在使用默认构造器DEFAULT_BUILDER进行初始化时进行了赋值,故会执行for循环中的遍历操作,EventBusBuilder中SubscriberInfoIndexs初始化时并无内容,所以最终返回null
private SubscriberInfo getSubscriberInfo(FindState findState) {
if (findState.subscriberInfo != null && findState.subscriberInfo.getSuperSubscriberInfo() != null) {
SubscriberInfo superclassInfo = findState.subscriberInfo.getSuperSubscriberInfo();
if (findState.clazz == superclassInfo.getSubscriberClass()) {
return superclassInfo;
}
}
if (subscriberInfoIndexes != null) {
for (SubscriberInfoIndex index : subscriberInfoIndexes) {
SubscriberInfo info = index.getSubscriberInfo(findState.clazz);
if (info != null) {
return info;
}
}
}
return null;
}
所以,最终解析类中所有的订阅方法,最终还是通过findUsingReflectionInSingleClass(findState)进行,具体的说明会穿插在代码中。其核心就是通过类的字节码文件,通过反射方法,获取方法的注解和参数,然后将符合条件的方法存储到findState中
private void findUsingReflectionInSingleClass(FindState findState) {
Method[] methods;
// 1. 通过反射查找所有的方法,异常后查找所有的public方法
try {
// This is faster than getMethods, especially when subscribers are fat classes like Activities
methods = findState.clazz.getDeclaredMethods();
} catch (Throwable th) {
// Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149
methods = findState.clazz.getMethods();
findState.skipSuperClasses = true;
}
// 2. 遍历所有的方法
for (Method method : methods) {
// 3. 通过getModifiers()获取方法的修饰符,获取公开且非abstract、static的方法
int modifiers = method.getModifiers();
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
// 4. 筛选出只有一个参数,包含有Subscribe的注解方法
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length == 1) {
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
if (subscribeAnnotation != null) {
// 5. 解析出方法的第一个参数,即我们定义的事件Event类,然后添加到findState中
Class<?> eventType = parameterTypes[0];
if (findState.checkAdd(method, eventType)) {
ThreadMode threadMode = subscribeAnnotation.threadMode();
findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
}
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException("@Subscribe method " + methodName +
"must have exactly 1 parameter but has " + parameterTypes.length);
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException(methodName +
" is a illegal @Subscribe method: must be public, non-static, and non-abstract");
}
}
}
如何判断FindState内部是否包含了当前事件,主要是通过checkAdd方法的两级检查
- 如果添加到anyMethodByEventType这个map返回null,代表未包含过该方法和event,添加
- 如果返回不为空,检查方法的签名
boolean checkAdd(Method method, Class<?> eventType) {
// 2 level check: 1st level with event type only (fast), 2nd level with complete signature when required.
// Usually a subscriber doesn't have methods listening to the same event type.
Object existing = anyMethodByEventType.put(eventType, method);
if (existing == null) {
return true;
} else {
if (existing instanceof Method) {
if (!checkAddWithMethodSignature((Method) existing, eventType)) {
// Paranoia check
throw new IllegalStateException();
}
// Put any non-Method object to "consume" the existing Method
anyMethodByEventType.put(eventType, this);
}
return checkAddWithMethodSignature(method, eventType);
}
}
Java检查一个类中方法签名,主要是靠方法名和参数名,满足方法名相同、参数类型相同且被@Subscribe修饰的函数,在一个类中不可能存在两个;考虑类继承体系,若这样的两个函数分别来自父类和子类,则最终被加入的是子类的函数。
private boolean checkAddWithMethodSignature(Method method, Class<?> eventType) {
methodKeyBuilder.setLength(0);
methodKeyBuilder.append(method.getName());
methodKeyBuilder.append('>').append(eventType.getName());
// 1. 拼接新方法的key, 方法名+参数名
String methodKey = methodKeyBuilder.toString();
// 2. 获取老方法的类字节码value
Class<?> methodClass = method.getDeclaringClass();
// 3. 将key和value放入map集合,并返回key对应的老的方法值
Class<?> methodClassOld = subscriberClassByMethodKey.put(methodKey, methodClass);
// 4. 如果不存在key对应的原始方法,或者原始的方法是当前方法(或当前方法的超类),加入到Map中,否认将原始方法放入map
if (methodClassOld == null || methodClassOld.isAssignableFrom(methodClass)) {
// Only add if not already found in a sub class
return true;
} else {
// Revert the put, old class is further down the class hierarchy
subscriberClassByMethodKey.put(methodKey, methodClassOld);
return false;
}
}
完成所有的check操作后,我们会将所有的订阅方法封装到FindState方法中,并通过getMethodsAndRelease()方法,将所有的订阅方法组合成一个list集合并返回到register()方法
private List<SubscriberMethod> getMethodsAndRelease(FindState findState) {
List<SubscriberMethod> subscriberMethods = new ArrayList<>(findState.subscriberMethods);
findState.recycle();
synchronized (FIND_STATE_POOL) {
for (int i = 0; i < POOL_SIZE; i++) {
if (FIND_STATE_POOL[i] == null) {
FIND_STATE_POOL[i] = findState;
break;
}
}
}
return subscriberMethods;
}
我们回到register()方法,然后遍历订阅方法数组,然后将订阅事件类型和订阅者关系,封装到subscriptionsByEventType中
// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
// 1.获取当前订阅方法所关联的事件类
Class<?> eventType = subscriberMethod.eventType;
// 2.创建订阅对象,包含订阅类与订阅方法等成员变量
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
// 3.从map中获取事件相关联的订阅对象集合
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions == null) {
// 4.创建subscriptions的集合,并将事件类和订阅对象集合放入subscriptionsByEventType中
subscriptions = new CopyOnWriteArrayList<>();
subscriptionsByEventType.put(eventType, subscriptions);
} else {
if (subscriptions.contains(newSubscription)) {
throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
+ eventType);
}
}
// 5.为subscriptions集合添加新的订阅对象newSubscription
int size = subscriptions.size();
for (int i = 0; i <= size; i++) {
if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
subscriptions.add(i, newSubscription);
break;
}
}
// 6.subscribedEvents存入新的事件
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
subscribedEvents.add(eventType);
if (subscriberMethod.sticky) {
if (eventInheritance) {
// Existing sticky events of all subclasses of eventType have to be considered.
// Note: Iterating over all events may be inefficient with lots of sticky events,
// thus data structure should be changed to allow a more efficient lookup
// (e.g. an additional map storing sub classes of super classes: Class -> List<Class>).
Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
for (Map.Entry<Class<?>, Object> entry : entries) {
Class<?> candidateEventType = entry.getKey();
if (eventType.isAssignableFrom(candidateEventType)) {
Object stickyEvent = entry.getValue();
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
} else {
Object stickyEvent = stickyEvents.get(eventType);
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
}
2. 发送事件
我们发送事件时无需初始化,直接调用EventBus.getDefault().post(event)即可
/** Posts the given event to the event bus. */
public void post(Object event) {
// 1. 获取当前线程绑定的PostingThreadState对象
PostingThreadState postingState = currentPostingThreadState.get();
// 2. 获取PostingThreadState中的event的list集合,并将当前需要发送的事件加入集合
List<Object> eventQueue = postingState.eventQueue;
eventQueue.add(event);
// 3. 若当前的发送线程状态非正在发送
if (!postingState.isPosting) {
// 4. 更新成员变量isMainThread与isPosting值
postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();
postingState.isPosting = true;
if (postingState.canceled) {
throw new EventBusException("Internal error. Abort state was not reset");
}
// 5.依次发送事件,并从队列中移除移除
try {
while (!eventQueue.isEmpty()) {
postSingleEvent(eventQueue.remove(0), postingState);
}
} finally {
// 6.所有时间发送完毕后更新状态
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
currentPostingThreadState是ThreadLocal的实例,每次调用时会返回当前线程绑定的PostingThreadState对象。如果为空,则会创建一个新的PostingThreadState对象,并将对象与当前线程进行绑定,Thread.threadLocals = new PostingThreadState(),具体的原理可以参考Handler中的ThreadLocal详解
private final ThreadLocal<PostingThreadState> currentPostingThreadState = new ThreadLocal<PostingThreadState>() {
@Override
protected PostingThreadState initialValue() {
return new PostingThreadState();
}
};
PostingThreadState类内部包含一个存储有事件的list集合,以及向前线程事件的一些状态
/** For ThreadLocal, much faster to set (and get multiple values). */
final static class PostingThreadState {
final List<Object> eventQueue = new ArrayList<Object>();
boolean isPosting;
boolean isMainThread;
Subscription subscription;
Object event;
boolean canceled;
}
postSingleEvent()发送单个事件
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
// 1.获取当前发送对象字节码
Class<?> eventClass = event.getClass();
boolean subscriptionFound = false;
// 2.默认eventInheritance为ture,执行if中的代码
if (eventInheritance) {
List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
int countTypes = eventTypes.size();
for (int h = 0; h < countTypes; h++) {
Class<?> clazz = eventTypes.get(h);
// 3.发送指定事件
subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);
}
} else {
subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);
}
if (!subscriptionFound) {
if (logNoSubscriberMessages) {
Log.d(TAG, "No subscribers registered for event " + eventClass);
}
if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
eventClass != SubscriberExceptionEvent.class) {
post(new NoSubscriberEvent(this, event));
}
}
}
通过当前的事件类,查找到所有包含这个类的接口和类对象
/** Looks up all Class objects including super classes and interfaces. Should also work for interfaces. */
private static List<Class<?>> lookupAllEventTypes(Class<?> eventClass) {
synchronized (eventTypesCache) {
// 1.默认eventTypesCache内部为空
List<Class<?>> eventTypes = eventTypesCache.get(eventClass);
if (eventTypes == null) {
eventTypes = new ArrayList<>();
Class<?> clazz = eventClass;
while (clazz != null) {
eventTypes.add(clazz);
addInterfaces(eventTypes, clazz.getInterfaces());
clazz = clazz.getSuperclass();
}
eventTypesCache.put(eventClass, eventTypes);
}
return eventTypes;
}
}
发送当前事件,通知注册EventBus时解析出的订阅类,调用订阅该事件的方法
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
// 1.获取在我们register操作时在subscriptionsByEventType中放入的事件类与相关连的订阅集合
subscriptions = subscriptionsByEventType.get(eventClass);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
for (Subscription subscription : subscriptions) {
postingState.event = event;
postingState.subscription = subscription;
boolean aborted = false;
try {
postToSubscription(subscription, event, postingState.isMainThread);
aborted = postingState.canceled;
} finally {
postingState.event = null;
postingState.subscription = null;
postingState.canceled = false;
}
if (aborted) {
break;
}
}
return true;
}
return false;
}
private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
switch (subscription.subscriberMethod.threadMode) {
// 1.默认所有的@Subscribe注解方法线程为当前发送线程:POSTING
case POSTING:
invokeSubscriber(subscription, event);
break;
case MAIN:
if (isMainThread) {
invokeSubscriber(subscription, event);
} else {
mainThreadPoster.enqueue(subscription, event);
}
break;
case BACKGROUND:
if (isMainThread) {
backgroundPoster.enqueue(subscription, event);
} else {
invokeSubscriber(subscription, event);
}
break;
case ASYNC:
asyncPoster.enqueue(subscription, event);
break;
default:
throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
}
}
通过反射调用订阅该事件的类方法
void invokeSubscriber(Subscription subscription, Object event) {
try {
subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
} catch (InvocationTargetException e) {
handleSubscriberException(subscription, event, e.getCause());
} catch (IllegalAccessException e) {
throw new IllegalStateException("Unexpected exception", e);
}
}
网友评论