|
35 | 35 | import java.lang.reflect.Method; |
36 | 36 | import java.util.ArrayList; |
37 | 37 | import java.util.Collection; |
| 38 | +import java.util.HashMap; |
38 | 39 | import java.util.HashSet; |
39 | 40 | import java.util.List; |
| 41 | +import java.util.Map; |
40 | 42 | import java.util.WeakHashMap; |
41 | 43 |
|
42 | 44 | import org.bushe.swing.event.annotation.AbstractProxySubscriber; |
@@ -80,6 +82,12 @@ public class DefaultEventService extends AbstractService implements |
80 | 82 |
|
81 | 83 | private DefaultEventBus eventBus; |
82 | 84 |
|
| 85 | + /** |
| 86 | + * A cache for mapping {@link Method}s to the {@link SciJavaEvent} class taken |
| 87 | + * as parameters. Only methods with event parameters will cached here. |
| 88 | + */ |
| 89 | + private final Map<Method, Class<?>> eventClasses = new HashMap<Method, Class<?>>(); |
| 90 | + |
83 | 91 | /** |
84 | 92 | * Set of claimed {@link EventHandler#key()}s. Additional event handlers |
85 | 93 | * specifying the same key will be ignored rather than subscribed. |
@@ -198,14 +206,24 @@ private <E extends SciJavaEvent> EventSubscriber<E> subscribe( |
198 | 206 |
|
199 | 207 | /** Gets the event class parameter of the given method. */ |
200 | 208 | private Class<? extends SciJavaEvent> getEventClass(final Method m) { |
201 | | - final Class<?>[] c = m.getParameterTypes(); |
202 | | - if (c == null || c.length != 1) return null; // wrong number of args |
203 | | - if (!SciJavaEvent.class.isAssignableFrom(c[0])) return null; // wrong class |
| 209 | + // Check for a cached entry for the given method |
| 210 | + Class<?> eventClass = eventClasses.get(m); |
| 211 | + |
| 212 | + if (eventClass == null) { |
| 213 | + final Class<?>[] c = m.getParameterTypes(); |
| 214 | + if (c == null || c.length != 1) return null; // wrong number of args |
| 215 | + if (!SciJavaEvent.class.isAssignableFrom(c[0])) return null; // wrong class |
| 216 | + |
| 217 | + // Cache the eventClass |
| 218 | + eventClass = c[0]; |
| 219 | + eventClasses.put(m, eventClass); |
| 220 | + } |
204 | 221 |
|
205 | 222 | @SuppressWarnings("unchecked") |
206 | | - final Class<? extends SciJavaEvent> eventClass = |
207 | | - (Class<? extends SciJavaEvent>) c[0]; |
208 | | - return eventClass; |
| 223 | + final Class<? extends SciJavaEvent> typedClass = |
| 224 | + (Class<? extends SciJavaEvent>) eventClass; |
| 225 | + |
| 226 | + return typedClass; |
209 | 227 | } |
210 | 228 |
|
211 | 229 | // -- Event handlers garbage collection preventer -- |
|
0 commit comments