Android EventBus源码解析 带你深入理解EventBus

}

public void registerSticky(Object subscriber, int priority) {

register(subscriber, DEFAULT_METHOD_NAME, true, priority);

}

本质上就调用了同一个:

private synchronized void register(Object subscriber, String methodName, boolean sticky, int priority) {

List subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriber.getClass(),

methodName);

for (SubscriberMethod subscriberMethod : subscriberMethods) {

subscribe(subscriber, subscriberMethod, sticky, priority);

}

}

四个参数

subscriber 是我们扫描类的对象,也就是我们代码中常见的this;

methodName 这个是写死的:“onEvent”,用于确定扫描什么开头的方法,可见我们的类中都是以这个开头。

sticky 这个参数,解释源码的时候解释,暂时不用管

priority 优先级,优先级越高,在调用的时候会越先调用。

下面开始看代码:

List subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriber.getClass(),

methodName);

调用内部类SubscriberMethodFinder的findSubscriberMethods方法,传入了subscriber 的class,以及methodName,返回一个List。

那么不用说,肯定是去遍历该类内部所有方法,然后根据methodName去匹配,匹配成功的封装成SubscriberMethod,最后返回一个List。下面看代码:

List findSubscriberMethods(Class<?> subscriberClass, String eventMethodName) {

String key = subscriberClass.getName() + ‘.’ + eventMethodName;

List subscriberMethods;

synchronized (methodCache) {

subscriberMethods = methodCache.get(key);

}

if (subscriberMethods != null) {

return subscriberMethods;

}

subscriberMethods = new ArrayList();

Class<?> clazz = subscriberClass;

HashSet eventTypesFound = new HashSet();

StringBuilder methodKeyBuilder = new StringBuilder();

while (clazz != null) {

String name = clazz.getName();

if (name.startsWith(“java.”) || name.startsWith(“javax.”) || name.startsWith(“android.”)) {

// Skip system classes, this just degrades performance

break;

}

// Starting with EventBus 2.2 we enforced methods to be public (might change with annotations again)

Method[] methods = clazz.getMethods();

for (Method method : methods) {

String methodName = method.getName();

if (methodName.startsWith(eventMethodName)) {

int modifiers = method.getModifiers();

if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {

Class<?>[] parameterTypes = method.getParameterTypes();

if (parameterTypes.length == 1) {

String modifierString = methodName.substring(eventMethodName.length());

ThreadMode threadMode;

if (modifierString.length() == 0) {

threadMode = ThreadMode.PostThread;

} else if (modifierString.equals(“MainThread”)) {

threadMode = ThreadMode.MainThread;

} else if (modifierString.equals(“BackgroundThread”)) {

threadMode = ThreadMode.BackgroundThread;

} else if (modifierString.equals(“Async”)) {

threadMode = ThreadMode.Async;

} else {

if (skipMethodVerificationForClasses.containsKey(clazz)) {

continue;

} else {

throw new EventBusException("Illegal onEvent method, check for typos: " + method);

}

}

Class<?> eventType = parameterTypes[0];

methodKeyBuilder.setLength(0);

methodKeyBuilder.append(methodName);

methodKey

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值