Guava 的 EventBus - @Subscribe 的可见性
Guava's EventBus - visibility of @Subscribe
来自接口方法的注释不会继承到实现该接口的对象,afaik。 SO search results.
想和Guava的EventBus一起使用接口,需要对象有回调方法注解@Subscribe
。
我想知道我是否可以简单地将注释放入接口并让对象实现该侦听器接口。根据 above,这应该 不 有效。然而,它 确实有效 (见下面的代码)。
为什么?
我的机器是 Java 1.8.0_151(32 位)和 Windows 7.
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
/**
* This test should fail, but... it works!
*/
public class EventTests {
@Test
public void test_events_are_heard() {
MyListener listener = new MyListener();
DeafListener deafListener = new DeafListener();
EventBus bus = new EventBus();
bus.register(listener);
bus.register(deafListener);
bus.post(new MyEvent());
assertEquals(1, listener.eventCount); // ok
assertEquals(0, deafListener.eventCount); // ok
}
// this interface includes the @Subscribe annotation
private interface Listener {
@Subscribe
public void onEvent(MyEvent event);
}
// this interface does not include the @Subscribe annotation
private interface NoListener {
public void onEvent(MyEvent event);
}
// just something different from Object
private static class MyEvent {
}
// implementation of "Listener" (with @Subscribe in interface)
private static class MyListener implements Listener {
int eventCount = 0;
@Override
public void onEvent(MyEvent event) {
eventCount ++;
}
}
// IDENTICAL implementation as above, but of "NoListener"
// (without @Subscribe in interface)
private static class DeafListener implements NoListener {
int eventCount = 0;
@Override
public void onEvent(MyEvent event) {
eventCount ++;
}
}
}
你是对的……也是错的。
您说得对,@Subscribe
注释不会被继承。你可以通过这样的测试来检查它 MyListener.class.getMethod("onEvent", MyEvent.class).getAnnotation(Subscribe.class) != null
.
但是,感觉如果在接口中定义了订阅方法就注册是对的。
所以 this was discussed at length within the Guava team 在过去和最小惊奇原则规定,如果任何声明方法被注释,那么它们的注册实现就会被订阅。
我不得不承认我检查了 the documentation and the Javadoc 并没有看到任何关于您的问题的具体内容,尽管感觉有必要提及。想起几年前的讨论,不得不在封闭的issues中寻找答案。
来自接口方法的注释不会继承到实现该接口的对象,afaik。 SO search results.
想和Guava的EventBus一起使用接口,需要对象有回调方法注解@Subscribe
。
我想知道我是否可以简单地将注释放入接口并让对象实现该侦听器接口。根据 above,这应该 不 有效。然而,它 确实有效 (见下面的代码)。
为什么?
我的机器是 Java 1.8.0_151(32 位)和 Windows 7.
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
/**
* This test should fail, but... it works!
*/
public class EventTests {
@Test
public void test_events_are_heard() {
MyListener listener = new MyListener();
DeafListener deafListener = new DeafListener();
EventBus bus = new EventBus();
bus.register(listener);
bus.register(deafListener);
bus.post(new MyEvent());
assertEquals(1, listener.eventCount); // ok
assertEquals(0, deafListener.eventCount); // ok
}
// this interface includes the @Subscribe annotation
private interface Listener {
@Subscribe
public void onEvent(MyEvent event);
}
// this interface does not include the @Subscribe annotation
private interface NoListener {
public void onEvent(MyEvent event);
}
// just something different from Object
private static class MyEvent {
}
// implementation of "Listener" (with @Subscribe in interface)
private static class MyListener implements Listener {
int eventCount = 0;
@Override
public void onEvent(MyEvent event) {
eventCount ++;
}
}
// IDENTICAL implementation as above, but of "NoListener"
// (without @Subscribe in interface)
private static class DeafListener implements NoListener {
int eventCount = 0;
@Override
public void onEvent(MyEvent event) {
eventCount ++;
}
}
}
你是对的……也是错的。
您说得对,@Subscribe
注释不会被继承。你可以通过这样的测试来检查它 MyListener.class.getMethod("onEvent", MyEvent.class).getAnnotation(Subscribe.class) != null
.
但是,感觉如果在接口中定义了订阅方法就注册是对的。
所以 this was discussed at length within the Guava team 在过去和最小惊奇原则规定,如果任何声明方法被注释,那么它们的注册实现就会被订阅。
我不得不承认我检查了 the documentation and the Javadoc 并没有看到任何关于您的问题的具体内容,尽管感觉有必要提及。想起几年前的讨论,不得不在封闭的issues中寻找答案。