创建Map key可以重复的内容:
//参数为普通监听器
私密多重,subscriberfindallsubscribers{
创建//key可以重复的mapkey:class信息订阅者value是订阅者
多时间图,subscribermethodsinlistener=hash多时间图. create (
Class clazz=listener.getClass (;
//getAnnotatedMethods构建缓存从初始化信息中获取所有已注释方法的集合
方法方法:千兆以太网方法(clazz ) )。
//获取参数类型
class [ ] parameter types=method.getparameter types (
获取事件类型的类型时间类型
class event type=parameter types [0];
确定了事件类与时间响应者的关系
//也就是说,EventBus注册的监听器参数解决了响应指定事件的原因
methodsinlistener.put (事件类型,subscriber.create ) bus,listener,method );
}
返回方法监听器;
}
私有静态最终加载缓存,immutablelistsubscribermethodscache=
CacheBuilder.newBuilder (
. weakKeys () )。
. build(newcacheloader,ImmutableList () )。
@Override
publicimmutablelistload (classconcreteclass ) throws Exception {
returngetannotatedmethodsnotcached (混凝土类;
}
);
//最终返回的是带有指定注释的所有方法集合方法名称参数,如果撤消的最终所有方法对象都有方法,则该参数可以反射
//需要注意的是,为什么要直接注册而不扫描所有方法,必须过滤继承方法
//去除沉重的条件到底是为了什么?
//父子类继承只需要注册一个子类(需要验证是否正确) ) ) )。
//获取缓存初始化父类的接口名称获取当前类自身当前类父类货物接口的所有类,并放入一个集
setextendsclasssupertypes=type token.of (clazz ).getTypes ).rawTypes );
Map identifiers=Maps.newHashMap (;
for (class supertype : supertypes ) {
for (method method : supertype.getdeclaredmethods () ) )
Synthetic (合成)方法是由编译器生成的,是源代码中没有的方法
if (方法. isannotationpresent (subscribe.class )! method.isSynthetic (
//todo(cgdecker ) : shouldcheckforagenericparametertypeanderrorout
//方法的参数必须判断
class [ ] parameter types=method.getparameter types (
检查规则(parameter types.length==1,
' method % shas @ subscribeannotationbuthas % s parameters.'
' subscribermethodsmusthaveexactly1parameter.',
method,parameterTypes.length;
//*
名称参数类型
*/
构建//方法的标识符对象由方法名称和方法对象参数组成
方法标识符=新方法标识符;
if (! identifiers.containskey(ident ) ) }
identifiers.put(ident,method;
}
}
}
}
returnimmutablelist.copy of (identifiers.values );