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進行了初始化,創(chuàng)建了跨進程級別的單例對象EventBus。其中構(gòu)造方法中,我們需要關注的,是創(chuàng)建了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文件,然后調(diào)用findSubscriberMethods解析出類中所有的訂閱方法,然后調(diào)用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字節(jié)碼解析出類中訂閱方法的。首先會通過findSubscriberMethods方法,去緩存中查看是否已經(jīng)解析過當前class,若已經(jīng)解析過則直接返回,否則會調(diào)用findUsingInfo(subscriberClass)去進行解析操作,然后更新訂閱方法緩存METHOD_CACHE。
注:因為默認DEFAULT_BUILDER中ignoreGeneratedIndex為false,所以不會執(zhí)行findUsingReflection(subscriberClass);只有自定義EventBusBuilder創(chuàng)建EventBus,并主動設定ignoreGeneratedIndex = true,才會執(zhí)行此方法。
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對象內(nèi)部以map形式存儲了訂閱類,訂閱方法的各種信息,后續(xù)解析時都是以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)方法會先將創(chuàng)建FindState對象,并將訂閱類存入成員變量,然后通過getSubscriberInfo(findState)方法,獲取subscriberInfo,第一次調(diào)用時會返回null(詳見下一步),會繼續(xù)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在使用默認構(gòu)造器DEFAULT_BUILDER進行初始化時進行了賦值,故會執(zhí)行for循環(huán)中的遍歷操作,EventBusBuilder中SubscriberInfoIndexs初始化時并無內(nèi)容,所以最終返回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)進行,具體的說明會穿插在代碼中。其核心就是通過類的字節(jié)碼文件,通過反射方法,獲取方法的注解和參數(shù),然后將符合條件的方法存儲到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. 篩選出只有一個參數(shù),包含有Subscribe的注解方法
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length == 1) {
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
if (subscribeAnnotation != null) {
// 5. 解析出方法的第一個參數(shù),即我們定義的事件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內(nèi)部是否包含了當前事件,主要是通過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檢查一個類中方法簽名,主要是靠方法名和參數(shù)名,滿足方法名相同、參數(shù)類型相同且被@Subscribe修飾的函數(shù),在一個類中不可能存在兩個;考慮類繼承體系,若這樣的兩個函數(shù)分別來自父類和子類,則最終被加入的是子類的函數(shù)。
private boolean checkAddWithMethodSignature(Method method, Class<?> eventType) {
methodKeyBuilder.setLength(0);
methodKeyBuilder.append(method.getName());
methodKeyBuilder.append('>').append(eventType.getName());
// 1. 拼接新方法的key, 方法名+參數(shù)名
String methodKey = methodKeyBuilder.toString();
// 2. 獲取老方法的類字節(jié)碼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()方法,然后遍歷訂閱方法數(shù)組,然后將訂閱事件類型和訂閱者關系,封裝到subscriptionsByEventType中
// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
// 1.獲取當前訂閱方法所關聯(lián)的事件類
Class<?> eventType = subscriberMethod.eventType;
// 2.創(chuàng)建訂閱對象,包含訂閱類與訂閱方法等成員變量
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
// 3.從map中獲取事件相關聯(lián)的訂閱對象集合
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions == null) {
// 4.創(chuàng)建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. 發(fā)送事件
我們發(fā)送事件時無需初始化,直接調(diào)用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集合,并將當前需要發(fā)送的事件加入集合
List<Object> eventQueue = postingState.eventQueue;
eventQueue.add(event);
// 3. 若當前的發(fā)送線程狀態(tài)非正在發(fā)送
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.依次發(fā)送事件,并從隊列中移除移除
try {
while (!eventQueue.isEmpty()) {
postSingleEvent(eventQueue.remove(0), postingState);
}
} finally {
// 6.所有時間發(fā)送完畢后更新狀態(tài)
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
currentPostingThreadState是ThreadLocal的實例,每次調(diào)用時會返回當前線程綁定的PostingThreadState對象。如果為空,則會創(chuàng)建一個新的PostingThreadState對象,并將對象與當前線程進行綁定,Thread.threadLocals = new PostingThreadState(),具體的原理可以參考Handler中的ThreadLocal詳解
private final ThreadLocal<PostingThreadState> currentPostingThreadState = new ThreadLocal<PostingThreadState>() {
@Override
protected PostingThreadState initialValue() {
return new PostingThreadState();
}
};
PostingThreadState類內(nèi)部包含一個存儲有事件的list集合,以及向前線程事件的一些狀態(tài)
/** 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()發(fā)送單個事件
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
// 1.獲取當前發(fā)送對象字節(jié)碼
Class<?> eventClass = event.getClass();
boolean subscriptionFound = false;
// 2.默認eventInheritance為ture,執(zhí)行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.發(fā)送指定事件
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內(nèi)部為空
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;
}
}
發(fā)送當前事件,通知注冊EventBus時解析出的訂閱類,調(diào)用訂閱該事件的方法
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注解方法線程為當前發(fā)送線程: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);
}
}
通過反射調(diào)用訂閱該事件的類方法
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);
}
}