采用反射
的方式对方法进行调用
使用APT(Annotation Processor Tool)
技术增强Subscribe
对应的类
APT 学习可以参考 IceMimosa/apt
前面几步基本都跟Guava实现的类似, 假设有类如下:
// Event 是个简单类
public class TestSubscribe {
@Subscribe
public void handle(Event e) {
System.out.println("执行了 handle");
}
@Subscribe
public void handle2(Event e) {
System.out.println("执行了 handle2");
}
}
- 在
regist
阶段, 将方法参数
和对应的对象和方法(Subscriber对象)
, 形成一个map post
阶段, 找出Event
类型对应的所有Subscriber
, 然后利用反射
的方式调用每个方法
在编译阶段使用APT
对类进行增强
- 定义个通用接口
EventBusHandler
public interface EventBusHandler {
void $$__invoke__$$(String type, Object arg);
}
-
编译期间
TestSubscribe
实现EventBusHandler接口
- 为
TestSubscribe
添加接口方法, 如下
public void $$__invoke__$$(String type, Object arg) { if ("handle@io.patamon.eventbus.demo.Event".equals(type)) { this.handle((Event)arg); } if ("handle2@io.patamon.eventbus.demo.Event".equals(type)) { this.handle2((Event)arg); } }
-
这样在
post
阶段, 就可以传入type
和arg
进行方法的调用, 无需进行反射, 很大的提高的调用效率.
其他细节需要处理的, 只实现的demo级别. (抽了Guava部分代码)