EventBus阅读源码(2)-SubscriberMethod

来源:互联网 发布:配眼镜需要的数据 编辑:程序博客网 时间:2024/05/22 02:27

如果说为什么选择源码的阅读顺序,这个其实没有什么跟据,我写在这里的顺序绝对不是我阅读的顺序,阅读的时候都是交叉的,很难说选的顺序正好是循序渐进的。而且一般都是找到一个入口来进行的,例如从EventBus类开始。

这适合我们开始的时候通览代码,当对代码有了一个大致的了解之后,如果要细细的分析,则要从根部着手。


这篇文章看

SubscriberMethod

该类只是基于了ThreadMode,没有基于其它的类,所以我们可以放心阅读。

Subscriber的意思是订阅者。顾名思义,就是只订阅者方法。猜想一下是不是像我们注册的方法呢(例如onMainThread(Event event))。


源码:

/* * Copyright (C) 2012-2016 Markus Junginger, greenrobot (http://greenrobot.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.greenrobot.eventbus;import java.lang.reflect.Method;/** Used internally by EventBus and generated subscriber indexes. */public class SubscriberMethod {    final Method method;//方法    final ThreadMode threadMode;//该方法的模式,即在什么线程中执行    final Class<?> eventType;//事件类型,就是我们注册方式的时候的参数类型    final int priority;//优先级    final boolean sticky;//是否粘性的    /** Used for efficient comparison */    String methodString;//方法的字符串描述,主要用于比较    public SubscriberMethod(Method method, Class<?> eventType, ThreadMode threadMode, int priority, boolean sticky) {        this.method = method;        this.threadMode = threadMode;        this.eventType = eventType;        this.priority = priority;        this.sticky = sticky;    }    @Override    public boolean equals(Object other) {//比较两个方法是否相等,即是否是一个方法        if (other == this) {            return true;        } else if (other instanceof SubscriberMethod) {            checkMethodString();            SubscriberMethod otherSubscriberMethod = (SubscriberMethod)other;            otherSubscriberMethod.checkMethodString();            // Don't use method.equals because of http://code.google.com/p/android/issues/detail?id=7811#c6            return methodString.equals(otherSubscriberMethod.methodString);        } else {            return false;        }    }    private synchronized void checkMethodString() {//计算方法的字符串描述        if (methodString == null) {            // Method.toString has more overhead, just take relevant parts of the method            StringBuilder builder = new StringBuilder(64);            builder.append(method.getDeclaringClass().getName());            builder.append('#').append(method.getName());            builder.append('(').append(eventType.getName());            methodString = builder.toString();        }    }    @Override    public int hashCode() {        return method.hashCode();    }}


代码不多。

容易理解。

不做过多啰嗦。

0 0
原创粉丝点击