GreenRobot 和 Guava 的 EventBus 是否使用反射?

Do GreenRobot's and Guava's EventBus use reflection?

我们的 Android 应用目前使用 Otto EventBus,它使用反射。我们希望避免反射的开销,但要保持灵活性。 Guava's event bus use reflection? What about GreenRobot的吗?

如果他们不这样做,他们会使用代码生成或类似的东西吗?

Otto 从来没有像 GreenRobot 的 EventBus 那样功能丰富——例如,没有线程模式,所以这是很好的摆脱。并且 Otto 被弃用,取而代之的是 RxJava——这对许多项目来说是大材小用(个人观点)。



但是为了减少反射的使用,GreenRobot EventBus 3.x is able to build an index in compilation time using APT而不是运行时反射。

http://greenrobot.org/eventbus/documentation/subscriber-index/


Index Preconditions: Note that only @Subscriber methods can be indexed for which the subscriber AND event class are public. Also, due to technical limitations of Java’s annotation processing itself, @Subscribe annotations are not recognized inside of anonymous classes.

buildscript {
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}



apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    compile 'org.greenrobot:eventbus:3.0.0'
    apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'
}

apt {
    arguments {
        eventBusIndex "com.example.myapp.MyEventBusIndex"
    }
}

And

EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
// Now the default instance uses the given index. Use it like this:
EventBus eventBus = EventBus.getDefault();