    001    package com.hammurapi.eventbus;
    002
    003    import java.util.HashSet;
    004    import java.util.Set;
    005
    006    import com.hammurapi.common.TokenExpander;
    007
    008    /**
    009     * This class introspects instances passed to it and creates event handlers from
    010     * classes annotated with Handler annotation.
    011     * @author Pavel Vlasov
    012     *
    013     * @param <E> Event type.
    014     * @param <P> Priority type.
    015     * @param <C> Context type.
    016     * @param <K> Registration key type.
    017     */
    018    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> {
    019
    020            public Introspector(ClassLoader classLoader, TokenExpander tokenExpander) {
    021                    super(classLoader, tokenExpander);
    022            }
    023
    024            /**
    025             * Registers instance methods annotated with Handler annotation with the bus.
    026             * @param instance Instance with handler methods.
    027             * @param bus Event bus.
    028             * @return Set of registration keys.
    029             */
    030            @Override
    031            public Set<K> bind(final C instance, B bus) {
    032                    Set<K> ret = new HashSet<K>();
    033                    for (ReflectiveEventHandler<E,C,H,S> handler: createHandlers(instance, bus.getEventType())) {
    034                            ret.add(bus.addHandler(handler));
    035                    }
    036                    return ret;
    037            }
    038    }