分析下边代码
// Activity中注册反注册
EventBus.getDefault().register(this);
EventBus.getDefault().unregister(this);
// Activity中用于接收数据写的方法
public void onEventMainThread()
// 发送数据的页面
EventBus.getDefault().post(param);
1. register
EventBus.getDefault()是一个单例
// 双重校验锁,防止并发,提高效率
public static EventBus getDefault() {
if (defaultInstance == null) {
synchronized (EventBus.class) {
if (defaultInstance == null) {
defaultInstance = new EventBus();
}
}
}
return defaultInstance;
}
register方法:
private synchronized void register(Object subscriber, String methodName, boolean sticky, int priority) {
// 调用 SubscriberMethodFinder类中的 findSubscriberMethods方法,
// 传入 this,methodName,返回一个 List<SubscriberMethod> subscriberMethods
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriber.getClass(),
methodName);
// for循环扫描到的 方法,然后调用 subscribe,SubscriberMethod 中保存了
// method、threadMode、eventType,通过上边方法已经分析
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod, sticky, priority);
}
}
subscriber:扫描的类,就是register(this)中的this;
methodName:写死的 onEvent,扫描该类中以 onEvent开头的方法;
sticky:
priority:优先级
调用 SubscriberMethodFinder 类中的 findSubscriberMethods方法,传入this,methodName,返回一个 List<SubscriberMethod> subscriberMethods,
// 遍历该类所有方法,根据 methodName 去匹配,匹配成功的 封装成 SubscriberMethod,然后返回 一个 List
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass, String eventMethodName) {
String key = subscriberClass.getName() + '.' + eventMethodName;
List<SubscriberMethod> subscriberMethods;
synchronized (methodCache) {
subscriberMethods = methodCache.get(key);
}
if (subscriberMethods != null) {
return subscriberMethods;
}
subscriberMethods = new ArrayList<SubscriberMethod>();
Class<?> clazz = subscriberClass;
HashSet<String> eventTypesFound = new HashSet<String>();
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;
}
// 获取该类所有的方法
Method[] methods = clazz.getMethods();
// 遍历所有方法
for (Method method : methods) {
String methodName = method.getName();
// 判断是否以 onEvent开头、是否是public、且不是static和abstract,是否是1个参数
// 如果都符合,就封装
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 = 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);
methodKeyBuilder.append('>').append(eventType.getName());
String methodKey = methodKeyBuilder.toString();
if (eventTypesFound.add(methodKey)) {
// 这里将 method、threadMode、eventType传入构造方法,
// 封装成 SubscriberMethod,添加到list集合,然后返回
subscriberMethods.add(new SubscriberMethod(method, threadMode, eventType));
}
}
} else if (!skipMethodVerificationForClasses.containsKey(clazz)) {
Log.d(EventBus.TAG, "Skipping method (not public, static or abstract): " + clazz + "."
+ methodName);
}
}
}
// 扫描所有父类,并不是当前类
clazz = clazz.getSuperclass();
}
if (subscriberMethods.isEmpty()) {
throw new EventBusException("Subscriber " + subscriberClass + " has no public methods called "
+ eventMethodName);
} else {
synchronized (methodCache) {
methodCache.put(key, subscriberMethods);
}
return subscriberMethods;
}
} |