EventListenerGuiceTypeListener implements TypeListener { public <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) { Provider<EventCasterInternal> eventCasterProvider = null; for (final Class<?> interfaceClass : listInterfaces(type.getRawType(), new HashSet<Class<?>>())) { final TypeLiteral<?> interfaceType = type.getSupertype(interfaceClass); if (bindings.contains(interfaceType)) { if(eventCasterProvider == null) { eventCasterProvider = encounter.getProvider(EventCasterInternal.class); } encounter.register(new RegisterInjectedEventListeners<I>(interfaceType, eventCasterProvider)); } } } EventListenerGuiceTypeListener(final Set<TypeLiteral<?>> bindings); void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter);  }
@Test public void typesThatImplementARegisteredListenerInterfaceAreMonitoredForInjection() { final TypeEncounter<MyListenerImpl> encounter = context .mock(new TypeLiteral<TypeEncounter<MyListenerImpl>>() { }); bindings.add(TypeLiteral.get(MyListener.class)); context.checking(new Expectations() { { oneOf(encounter).getProvider(EventCasterInternal.class); will(returnValue(eventCasterProvider)); oneOf(encounter).register( new RegisterInjectedEventListeners<Object>(TypeLiteral .get(MyListener.class), eventCasterProvider)); } }); new EventListenerGuiceTypeListener(bindings).hear( TypeLiteral.get(MyListenerImpl.class), encounter); }
@Test public void typesThatHaveASuperClassWhichImplementARegisteredListenerInterfaceAreMonitoredForInjection() { final TypeEncounter<MyClassWhichExtendsMyListenerImpl> encounter = context .mock(new TypeLiteral<TypeEncounter<MyClassWhichExtendsMyListenerImpl>>() {}); bindings.add(TypeLiteral.get(MyListener.class)); context.checking(new Expectations() { { oneOf(encounter).getProvider(EventCasterInternal.class); will(returnValue(eventCasterProvider)); oneOf(encounter).register( new RegisterInjectedEventListeners<Object>(TypeLiteral .get(MyListener.class), eventCasterProvider)); } }); new EventListenerGuiceTypeListener(bindings).hear( TypeLiteral.get(MyClassWhichExtendsMyListenerImpl.class), encounter); }
@Test public void typesThatImplementInterfaceWithASupertypeThatIsAListenerAreMonitoredForInjection() { final TypeEncounter<MyClassWhichImplementsInterfaceExtendingMyListenerImpl> encounter = context .mock(new TypeLiteral<TypeEncounter<MyClassWhichImplementsInterfaceExtendingMyListenerImpl>>() {}); bindings.add(TypeLiteral.get(MyListener.class)); context.checking(new Expectations() { { oneOf(encounter).getProvider(EventCasterInternal.class); will(returnValue(eventCasterProvider)); oneOf(encounter).register( new RegisterInjectedEventListeners<Object>(TypeLiteral .get(MyListener.class), eventCasterProvider)); } }); new EventListenerGuiceTypeListener(bindings) .hear(TypeLiteral .get(MyClassWhichImplementsInterfaceExtendingMyListenerImpl.class), encounter); }