Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Guava issuing the error "missing event handler for an > annotated method"?

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.

like image 920
Jeff Axelrod Avatar asked Aug 07 '12 21:08

Jeff Axelrod


2 Answers

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.

like image 101
Jeff Axelrod Avatar answered Nov 19 '22 14:11

Jeff Axelrod


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();
      }
    }
  }
like image 23
Blundell Avatar answered Nov 19 '22 14:11

Blundell