|
48 | 48 | import org.scijava.service.AbstractService; |
49 | 49 | import org.scijava.service.Service; |
50 | 50 | import org.scijava.thread.ThreadService; |
| 51 | +import org.scijava.util.ClassUtils; |
51 | 52 |
|
52 | 53 | /** |
53 | 54 | * Default service for publishing and subscribing to SciJava events. |
@@ -101,7 +102,16 @@ public <E extends SciJavaEvent> void publishLater(final E e) { |
101 | 102 | public List<EventSubscriber<?>> subscribe(final Object o) { |
102 | 103 | final List<EventSubscriber<?>> subscribers = |
103 | 104 | new ArrayList<EventSubscriber<?>>(); |
104 | | - subscribeRecursively(subscribers, o.getClass(), o); |
| 105 | + final List<Method> eventHandlers = |
| 106 | + ClassUtils.getAnnotatedMethods(o.getClass(), EventHandler.class); |
| 107 | + for (final Method m : eventHandlers) { |
| 108 | + final Class<? extends SciJavaEvent> eventClass = getEventClass(m); |
| 109 | + if (eventClass == null) { |
| 110 | + log.warn("Invalid EventHandler method: " + m); |
| 111 | + continue; |
| 112 | + } |
| 113 | + subscribers.add(subscribe(eventClass, o, m)); |
| 114 | + } |
105 | 115 | return subscribers; |
106 | 116 | } |
107 | 117 |
|
@@ -144,30 +154,6 @@ public void dispose() { |
144 | 154 |
|
145 | 155 | // -- Helper methods -- |
146 | 156 |
|
147 | | - /** |
148 | | - * Recursively scans for @{@link EventHandler} annotated methods, and |
149 | | - * subscribes them to the event service. |
150 | | - */ |
151 | | - private void subscribeRecursively( |
152 | | - final List<EventSubscriber<?>> subscribers, final Class<?> type, |
153 | | - final Object o) |
154 | | - { |
155 | | - if (type == null || type == Object.class) return; |
156 | | - for (final Method m : type.getDeclaredMethods()) { |
157 | | - final EventHandler ann = m.getAnnotation(EventHandler.class); |
158 | | - if (ann == null) continue; // not an event handler method |
159 | | - |
160 | | - final Class<? extends SciJavaEvent> eventClass = getEventClass(m); |
161 | | - if (eventClass == null) { |
162 | | - log.warn("Invalid EventHandler method: " + m); |
163 | | - continue; |
164 | | - } |
165 | | - |
166 | | - subscribers.add(subscribe(eventClass, o, m)); |
167 | | - } |
168 | | - subscribeRecursively(subscribers, type.getSuperclass(), o); |
169 | | - } |
170 | | - |
171 | 157 | private <E extends SciJavaEvent> void subscribe(final Class<E> c, |
172 | 158 | final EventSubscriber<E> subscriber) |
173 | 159 | { |
|
0 commit comments