1 | package com.hammurapi.eventbus; |
2 | |
3 | import java.util.HashSet; |
4 | import java.util.Set; |
5 | |
6 | import com.hammurapi.common.TokenExpander; |
7 | |
8 | /** |
9 | * This class introspects instances passed to it and creates event handlers from |
10 | * classes annotated with Handler annotation. |
11 | * @author Pavel Vlasov |
12 | * |
13 | * @param <E> Event type. |
14 | * @param <P> Priority type. |
15 | * @param <C> Context type. |
16 | * @param <K> Registration key type. |
17 | */ |
18 | public class Introspector<E, C, K, H extends EventBus.Handle<E,Integer,C>, S extends EventStore<E,Integer,C,H,S>, B extends EventBus<E, Integer, C, K, H, S>> extends ReflectiveEventHandlerFactory<E, C, H, S> implements JavaBinder<E, C, K, H, S, B, C> { |
19 | |
20 | public Introspector(ClassLoader classLoader, TokenExpander tokenExpander) { |
21 | super(classLoader, tokenExpander); |
22 | } |
23 | |
24 | /** |
25 | * Registers instance methods annotated with Handler annotation with the bus. |
26 | * @param instance Instance with handler methods. |
27 | * @param bus Event bus. |
28 | * @return Set of registration keys. |
29 | */ |
30 | @Override |
31 | public Set<K> bind(final C instance, B bus) { |
32 | Set<K> ret = new HashSet<K>(); |
33 | for (ReflectiveEventHandler<E,C,H,S> handler: createHandlers(instance, bus.getEventType())) { |
34 | ret.add(bus.addHandler(handler)); |
35 | } |
36 | return ret; |
37 | } |
38 | } |