EventBus.getDefault().register(this);
/**
* Registers the given subscriber to receive events. Subscribers must call {@link #unregister(Object)} once they
* are no longer interested in receiving events.
* <p/>
* Subscribers have event handling methods that must be annotated by {@link Subscribe}.
* The {@link Subscribe} annotation also allows configuration like {@link
* ThreadMode} and priority.
*/
public void register(Object subscriber) {
// 首先调用反射的这个getClass(),获取到Class的订阅对象
Class<?> subscriberClass = subscriber.getClass();
// 通过上面获取到的Class对象,找到订阅方法的集合,他是通过 findSubscriberMethods去找到的。最终会返回一个订阅者的集合。
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
// 从方法的缓存池当中去查找是否已经有了这个订阅者方法,如果有的话就把这个订阅者集合返回给我们
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) { // 判断有没有缓存
return subscriberMethods;
}
// 索引的判断,默认情况下是false,
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;
}
}
subscribe(subscriber, subscriberMethod); } }}
接下来看下findSubscriberMethods这个方法的源码:
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
// 从方法的缓存池当中去查找是否已经有了这个订阅者方法,如果有的话就把这个订阅者集合返回给我们
List<SubscriberMethod> subscriberMetho