10

尝试注销时,我在我的一个课程中遇到以下错误。

java.lang.IllegalArgumentException:缺少注释方法的事件处理程序。[DerivedClass] 是否已注册?”在 com.google.common.eventbus.EventBus.unregister(EventBus.java:227)

类调用unregister(this)具有公共@Subscribe注释方法。

4

2 回答 2

18

问题是错误消息误导我认为注释有问题。事实上,由于某些意外的控制流,该类正在注销自己两次。

于 2012-08-08T03:54:42.490 回答
4

源码说明:

/**
   * 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();
      }
    }
  }
于 2013-07-27T09:59:01.470 回答