尝试注销时,我在我的一个课程中遇到以下错误。
java.lang.IllegalArgumentException:缺少注释方法的事件处理程序。[DerivedClass] 是否已注册?”在 com.google.common.eventbus.EventBus.unregister(EventBus.java:227)
类调用unregister(this)
具有公共@Subscribe
注释方法。
问题是错误消息误导我认为注释有问题。事实上,由于某些意外的控制流,该类正在注销自己两次。
源码说明:
/**
* Unregisters all handler methods on a registered {@code object}.
*
* @param object object whose handler methods should be unregistered.
* @throws IllegalArgumentException if the object was not previously registered.
*/
public void unregister(Object object) {
Multimap<Class<?>, EventHandler> methodsInListener = finder.findAllHandlers(object);
for (Entry<Class<?>, Collection<EventHandler>> entry : methodsInListener.asMap().entrySet()) {
Class<?> eventType = entry.getKey();
Collection<EventHandler> eventMethodsInListener = entry.getValue();
handlersByTypeLock.writeLock().lock();
try {
Set<EventHandler> currentHandlers = handlersByType.get(eventType);
if (!currentHandlers.containsAll(eventMethodsInListener)) {
throw new IllegalArgumentException(
"missing event handler for an annotated method. Is " + object + " registered?");
}
currentHandlers.removeAll(eventMethodsInListener);
} finally {
handlersByTypeLock.writeLock().unlock();
}
}
}