1/*
2 * Copyright (C) 2007 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.eventbus;
18
19import com.google.common.collect.HashMultimap;
20import com.google.common.collect.Multimap;
21import java.lang.reflect.Method;
22
23/**
24 * A {@link HandlerFindingStrategy} for collecting all event handler methods
25 * that are marked with the {@link Subscribe} annotation.
26 *
27 * @author Cliff Biffle
28 */
29class AnnotatedHandlerFinder implements HandlerFindingStrategy {
30
31  /**
32   * {@inheritDoc}
33   *
34   * This implementation finds all methods marked with a {@link Subscribe}
35   * annotation.
36   */
37  @Override
38  public Multimap<Class<?>, EventHandler> findAllHandlers(Object listener) {
39    Multimap<Class<?>, EventHandler> methodsInListener =
40        HashMultimap.create();
41    Class clazz = listener.getClass();
42    while (clazz != null) {
43      for (Method method : clazz.getMethods()) {
44        Subscribe annotation = method.getAnnotation(Subscribe.class);
45
46        if (annotation != null) {
47          Class<?>[] parameterTypes = method.getParameterTypes();
48          if (parameterTypes.length != 1) {
49            throw new IllegalArgumentException(
50                "Method " + method + " has @Subscribe annotation, but requires " +
51                parameterTypes.length + " arguments.  Event handler methods " +
52                "must require a single argument.");
53          }
54          Class<?> eventType = parameterTypes[0];
55          EventHandler handler = makeHandler(listener, method);
56
57          methodsInListener.put(eventType, handler);
58        }
59      }
60      clazz = clazz.getSuperclass();
61    }
62    return methodsInListener;
63  }
64
65  /**
66   * Creates an {@code EventHandler} for subsequently calling {@code method} on
67   * {@code listener}.
68   * Selects an EventHandler implementation based on the annotations on
69   * {@code method}.
70   *
71   * @param listener  object bearing the event handler method.
72   * @param method  the event handler method to wrap in an EventHandler.
73   * @return an EventHandler that will call {@code method} on {@code listener}
74   *         when invoked.
75   */
76  private static EventHandler makeHandler(Object listener, Method method) {
77    EventHandler wrapper;
78    if (methodIsDeclaredThreadSafe(method)) {
79      wrapper = new EventHandler(listener, method);
80    } else {
81      wrapper = new SynchronizedEventHandler(listener, method);
82    }
83    return wrapper;
84  }
85
86  /**
87   * Checks whether {@code method} is thread-safe, as indicated by the
88   * {@link AllowConcurrentEvents} annotation.
89   *
90   * @param method  handler method to check.
91   * @return {@code true} if {@code handler} is marked as thread-safe,
92   *         {@code false} otherwise.
93   */
94  private static boolean methodIsDeclaredThreadSafe(Method method) {
95    return method.getAnnotation(AllowConcurrentEvents.class) != null;
96  }
97}
98