I'm getting the following error in one of my classes on the when attempting to unregister it.
java.lang.IllegalArgumentException: missing event handler for an annotated method. Is [DerivedClass] registered?" at com.google.common.eventbus.EventBus.unregister(EventBus.java:227)
The class calling unregister(this)
has the public @Subscribe
annotated method.
The problem was that the error message misled me to think there was something wrong with the annotations. It was in fact that the class was unregistering itself twice due to some unexpected flow of control.
A source code explanation:
/**
* 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();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With