|
30 | 30 | package org.scijava.context; |
31 | 31 |
|
32 | 32 | import java.lang.reflect.Field; |
| 33 | +import java.lang.reflect.InvocationTargetException; |
| 34 | +import java.lang.reflect.Method; |
33 | 35 | import java.util.ArrayList; |
34 | 36 | import java.util.Collections; |
35 | 37 | import java.util.Comparator; |
|
45 | 47 |
|
46 | 48 | import org.scijava.discovery.Discovery; |
47 | 49 | import org.scijava.events.EventBus; |
| 50 | +import org.scijava.events.Subscription; |
48 | 51 | import org.scijava.index.IndexDiscoverer; |
49 | 52 | import org.scijava.spi.Disposable; |
50 | 53 |
|
@@ -156,6 +159,9 @@ public <S extends Service> Optional<S> optionalService(final Class<S> type) { |
156 | 159 | // on each other resolve instead of recursing. |
157 | 160 | byRequestedType.put(type, s); |
158 | 161 | inject(s); |
| 162 | + // NB: a service's lifetime is the context's, so its handlers need |
| 163 | + // no cleanup by the author: disposing the context closes the bus. |
| 164 | + subscribe(s); |
159 | 165 | initialize(s); |
160 | 166 | // NB: record for disposal only once initialization has finished. |
161 | 167 | // A service's dependencies finish first, so they land earlier in |
@@ -219,6 +225,34 @@ public void inject(final Object target) { |
219 | 225 | } |
220 | 226 | } |
221 | 227 |
|
| 228 | + /** |
| 229 | + * Subscribes the {@link EventHandler}-annotated methods of the given object |
| 230 | + * to this context's {@link EventBus}. |
| 231 | + * <p> |
| 232 | + * Services are subscribed automatically when the context creates them, and |
| 233 | + * unsubscribed when it is disposed. This is for everything else: a plugin |
| 234 | + * may be created and discarded many times in a session, so its handlers are |
| 235 | + * the caller's to manage. |
| 236 | + * </p> |
| 237 | + * |
| 238 | + * @param target the object whose handlers to subscribe |
| 239 | + * @return a subscription per handler; close them when the object is done |
| 240 | + * @throws ServiceException if a handler is malformed, or the target's |
| 241 | + * package is not open to this module |
| 242 | + */ |
| 243 | + public List<Subscription> subscribe(final Object target) { |
| 244 | + if (target == null) throw new NullPointerException("target"); |
| 245 | + final List<Subscription> subscriptions = new ArrayList<>(); |
| 246 | + for (Class<?> c = target.getClass(); c != null; c = c.getSuperclass()) { |
| 247 | + for (final Method method : c.getDeclaredMethods()) { |
| 248 | + final EventHandler handler = method.getAnnotation(EventHandler.class); |
| 249 | + if (handler == null) continue; |
| 250 | + subscriptions.add(subscribeHandler(target, method, handler)); |
| 251 | + } |
| 252 | + } |
| 253 | + return subscriptions; |
| 254 | + } |
| 255 | + |
222 | 256 | /** Gets this context's event bus. */ |
223 | 257 | public EventBus events() { |
224 | 258 | return events; |
@@ -310,6 +344,54 @@ private void injectField(final Object target, final Field field, |
310 | 344 | } |
311 | 345 | } |
312 | 346 |
|
| 347 | + /** Subscribes one annotated method. */ |
| 348 | + private Subscription subscribeHandler(final Object target, |
| 349 | + final Method method, final EventHandler handler) |
| 350 | + { |
| 351 | + final Class<?>[] params = method.getParameterTypes(); |
| 352 | + if (params.length != 1) { |
| 353 | + throw new ServiceException("An @EventHandler takes exactly one" + // |
| 354 | + " parameter, the event: " + method.getDeclaringClass().getName() + // |
| 355 | + "." + method.getName() + " takes " + params.length); |
| 356 | + } |
| 357 | + try { |
| 358 | + // NB: the same deep reflection, and the same `opens`, that |
| 359 | + // @Dependency needs. |
| 360 | + method.setAccessible(true); |
| 361 | + } |
| 362 | + catch (final RuntimeException exc) { |
| 363 | + throw new ServiceException("Cannot subscribe " + method |
| 364 | + .getDeclaringClass().getName() + "." + method.getName() + // |
| 365 | + ". Does its module declare `opens " + method.getDeclaringClass() |
| 366 | + .getPackageName() + " to org.scijava.context;`?", exc); |
| 367 | + } |
| 368 | + return subscribeTyped(params[0], handler.priority(), target, method); |
| 369 | + } |
| 370 | + |
| 371 | + /** Captures the event type, so that the bus sees a typed subscription. */ |
| 372 | + private <E> Subscription subscribeTyped(final Class<E> eventType, |
| 373 | + final double priority, final Object target, final Method method) |
| 374 | + { |
| 375 | + return events.subscribe(eventType, priority, event -> { |
| 376 | + try { |
| 377 | + method.invoke(target, event); |
| 378 | + } |
| 379 | + catch (final IllegalAccessException exc) { |
| 380 | + throw new ServiceException("Cannot invoke handler " + method |
| 381 | + .getDeclaringClass().getName() + "." + method.getName(), exc); |
| 382 | + } |
| 383 | + catch (final InvocationTargetException exc) { |
| 384 | + // NB: unwrap, so subscribers see what the handler actually threw |
| 385 | + // rather than a reflection wrapper. |
| 386 | + final Throwable cause = exc.getCause(); |
| 387 | + if (cause instanceof RuntimeException) throw (RuntimeException) cause; |
| 388 | + if (cause instanceof Error) throw (Error) cause; |
| 389 | + throw new ServiceException("Handler failed: " + method |
| 390 | + .getDeclaringClass().getName() + "." + method.getName(), cause); |
| 391 | + } |
| 392 | + }); |
| 393 | + } |
| 394 | + |
313 | 395 | /** Resolves what to inject for a field of the given type. */ |
314 | 396 | private Object resolve(final Class<?> type) { |
315 | 397 | if (type == Context.class) return this; |
|
0 commit comments