View Javadoc

1   package com.atlassian.event.internal;
2   
3   import com.atlassian.event.spi.ListenerInvoker;
4   import com.google.common.collect.Sets;
5   
6   import java.lang.reflect.InvocationTargetException;
7   import java.lang.reflect.Method;
8   import java.util.Optional;
9   import java.util.Set;
10  
11  import static com.google.common.base.Preconditions.checkNotNull;
12  
13  /**
14   * A listener invoker that knows how to call a given single parameter method on a given object.
15   *
16   * @since 2.0
17   */
18  final class SingleParameterMethodListenerInvoker implements ListenerInvoker {
19      private final Method method;
20      private final Object listener;
21      private final Optional<String> scope;
22  
23  
24      public SingleParameterMethodListenerInvoker(Object listener, Method method) {
25          this(listener, method, Optional.empty());
26      }
27  
28      public SingleParameterMethodListenerInvoker(Object listener, Method method, Optional<String> scope) {
29          this.listener = checkNotNull(listener);
30          this.method = checkNotNull(method);
31          this.scope = checkNotNull(scope);
32      }
33  
34      public Set<Class<?>> getSupportedEventTypes() {
35          return Sets.newHashSet(method.getParameterTypes());
36      }
37  
38      public void invoke(Object event) {
39          try {
40              method.invoke(listener, event);
41          } catch (IllegalAccessException e) {
42              throw new RuntimeException("Listener: " + listener.getClass().getName()
43                      + " event: " + event.getClass().getName(), e);
44          } catch (InvocationTargetException e) {
45              if (e.getCause() == null) {
46                  throw new RuntimeException("Listener: " + listener.getClass().getName()
47                          + " event: " + event.getClass().getName(), e);
48              } else if (e.getCause().getMessage() == null) {
49                  throw new RuntimeException("Listener: " + listener.getClass().getName()
50                          + " event: " + event.getClass().getName(), e.getCause());
51              } else {
52                  throw new RuntimeException(e.getCause().getMessage()
53                          + ". Listener: " + listener.getClass().getName()
54                          + " event: " + event.getClass().getName(),
55                          e.getCause());
56              }
57          }
58      }
59  
60      public boolean supportAsynchronousEvents() {
61          return true;
62      }
63  
64      @Override
65      public Optional<String> getScope()
66      {
67          return scope;
68      }
69  
70      @Override
71      public String toString() {
72          return "SingleParameterMethodListenerInvoker{method=" + method + ", listener=" + paranoidToString(listener) + '}';
73      }
74  
75      /**
76       * Calls the object's toString() method. If an exception is thrown from the object's toString() method then this
77       * method falls back to the "identity" toString() (as per JVM defaults).
78       *
79       * @param object an Object
80       * @return a human-readable String representation of the object
81       */
82      private String paranoidToString(Object object) {
83          try {
84              return String.valueOf(object);
85          } catch (RuntimeException e) {
86              return object.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(object));
87          }
88      }
89  }