|
35 | 35 | import java.lang.reflect.Method; |
36 | 36 | import java.util.ArrayList; |
37 | 37 | import java.util.Collection; |
| 38 | +import java.util.Collections; |
38 | 39 | import java.util.HashMap; |
39 | 40 | import java.util.HashSet; |
40 | 41 | import java.util.List; |
@@ -112,29 +113,32 @@ public <E extends SciJavaEvent> void publishLater(final E e) { |
112 | 113 |
|
113 | 114 | @Override |
114 | 115 | public List<EventSubscriber<?>> subscribe(final Object o) { |
115 | | - final List<EventSubscriber<?>> subscribers = |
116 | | - new ArrayList<EventSubscriber<?>>(); |
| 116 | + List<EventSubscriber<?>> subscribers = Collections.emptyList(); |
117 | 117 | final List<Method> eventHandlers = |
118 | 118 | ClassUtils.getAnnotatedMethods(o.getClass(), EventHandler.class); |
119 | | - for (final Method m : eventHandlers) { |
120 | | - // verify that the event handler method is valid |
121 | | - final Class<? extends SciJavaEvent> eventClass = getEventClass(m); |
122 | | - if (eventClass == null) { |
123 | | - log.warn("Invalid EventHandler method: " + m); |
124 | | - continue; |
125 | | - } |
126 | 119 |
|
127 | | - // verify that the event handler key isn't already claimed |
128 | | - final String key = m.getAnnotation(EventHandler.class).key(); |
129 | | - if (!key.isEmpty()) { |
130 | | - synchronized (keys) { |
131 | | - if (keys.contains(key)) continue; |
132 | | - keys.add(key); |
| 120 | + if (!eventHandlers.isEmpty()) { |
| 121 | + subscribers = new ArrayList<EventSubscriber<?>>(); |
| 122 | + for (final Method m : eventHandlers) { |
| 123 | + // verify that the event handler method is valid |
| 124 | + final Class<? extends SciJavaEvent> eventClass = getEventClass(m); |
| 125 | + if (eventClass == null) { |
| 126 | + log.warn("Invalid EventHandler method: " + m); |
| 127 | + continue; |
133 | 128 | } |
134 | | - } |
135 | 129 |
|
136 | | - // subscribe the event handler |
137 | | - subscribers.add(subscribe(eventClass, o, m)); |
| 130 | + // verify that the event handler key isn't already claimed |
| 131 | + final String key = m.getAnnotation(EventHandler.class).key(); |
| 132 | + if (!key.isEmpty()) { |
| 133 | + synchronized (keys) { |
| 134 | + if (keys.contains(key)) continue; |
| 135 | + keys.add(key); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // subscribe the event handler |
| 140 | + subscribers.add(subscribe(eventClass, o, m)); |
| 141 | + } |
138 | 142 | } |
139 | 143 | return subscribers; |
140 | 144 | } |
|
0 commit comments