1 | package com.hammurapi.eventbus; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collection; |
5 | |
6 | import com.hammurapi.common.TokenExpander; |
7 | import com.hammurapi.extract.Predicate; |
8 | |
9 | /** |
10 | * This class adds a single method to the base class - it creates predicate/handler pairs from |
11 | * @author Pavel Vlasov |
12 | * |
13 | * @param <E> |
14 | * @param <C> |
15 | */ |
16 | public class ReflectiveEventHandlerFactory<E, C, H extends EventBus.Handle<E,Integer,C>, S extends EventStore<E,Integer,C,H,S>> extends IntrospectorBase<E, C> { |
17 | |
18 | protected ReflectiveEventHandlerFactory(ClassLoader classLoader, TokenExpander tokenExpander) { |
19 | super(classLoader, tokenExpander); |
20 | } |
21 | |
22 | /** |
23 | * Introspects class and creates predicate/handler pairs. |
24 | * @param handlerClass |
25 | * @param eventType |
26 | * @return |
27 | */ |
28 | @SuppressWarnings("unchecked") |
29 | public Collection<ReflectiveEventHandler<E,C,H,S>> createHandlers(C instance, Class<E> eventType) { |
30 | Collection<ReflectiveEventHandler<E,C,H,S>> ret = new ArrayList<ReflectiveEventHandler<E,C,H,S>>(); |
31 | for (IntrospectionResult<E, C> ir: introspect((Class<C>) instance.getClass(), eventType)) { |
32 | ReflectiveEventHandler<E, C, H, S> handler = new ReflectiveEventHandler<E,C,H,S>(instance, ir.getOffset(), ir.getMethod(), ir.getParameterTypes(), ir.getHandlerAnnotation(), eventType, ir.getPredicates()); |
33 | ret.add(handler); |
34 | } |
35 | |
36 | return ret; |
37 | } |
38 | } |