EventBus - 5 源码解析-3:register订阅(上)

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值