Skip to content

Commit 0bc3575

Browse files
committed
DefaultEventService: use getAnnotatedMethods
The ClassUtils class now contains a getAnnotatedMethods method, which obtains all methods annotated with the given annotation class.
1 parent 11608e5 commit 0bc3575

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

src/main/java/org/scijava/event/DefaultEventService.java

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.scijava.service.AbstractService;
4949
import org.scijava.service.Service;
5050
import org.scijava.thread.ThreadService;
51+
import org.scijava.util.ClassUtils;
5152

5253
/**
5354
* Default service for publishing and subscribing to SciJava events.
@@ -101,7 +102,16 @@ public <E extends SciJavaEvent> void publishLater(final E e) {
101102
public List<EventSubscriber<?>> subscribe(final Object o) {
102103
final List<EventSubscriber<?>> subscribers =
103104
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+
}
105115
return subscribers;
106116
}
107117

@@ -144,30 +154,6 @@ public void dispose() {
144154

145155
// -- Helper methods --
146156

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-
171157
private <E extends SciJavaEvent> void subscribe(final Class<E> c,
172158
final EventSubscriber<E> subscriber)
173159
{

0 commit comments

Comments
 (0)