Search in sources :

Example 6 with TypeEncounter

use of com.google.inject.spi.TypeEncounter in project guice by google.

the class BindingTest method testToConstructorSpiData.

public void testToConstructorSpiData() throws NoSuchMethodException {
    final Set<TypeLiteral<?>> heardTypes = Sets.newHashSet();
    final Constructor<D> constructor = D.class.getConstructor(Stage.class);
    final TypeListener listener = new TypeListener() {

        @Override
        public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
            if (!heardTypes.add(type)) {
                fail("Heard " + type + " multiple times!");
            }
        }
    };
    Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Object.class).toConstructor(constructor);
            bind(D.class).toConstructor(constructor);
            bindListener(Matchers.any(), listener);
        }
    });
    assertEquals(ImmutableSet.of(TypeLiteral.get(D.class)), heardTypes);
}
Also used : TypeListener(com.google.inject.spi.TypeListener) TypeEncounter(com.google.inject.spi.TypeEncounter)

Example 7 with TypeEncounter

use of com.google.inject.spi.TypeEncounter in project roboguice by roboguice.

the class BindingTest method testToConstructorSpiData.

public void testToConstructorSpiData() throws NoSuchMethodException {
    final Set<TypeLiteral<?>> heardTypes = Sets.newHashSet();
    final Constructor<D> constructor = D.class.getConstructor(Stage.class);
    final TypeListener listener = new TypeListener() {

        public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
            if (!heardTypes.add(type)) {
                fail("Heard " + type + " multiple times!");
            }
        }
    };
    Guice.createInjector(new AbstractModule() {

        protected void configure() {
            bind(Object.class).toConstructor(constructor);
            bind(D.class).toConstructor(constructor);
            bindListener(Matchers.any(), listener);
        }
    });
    assertEquals(ImmutableSet.of(TypeLiteral.get(D.class)), heardTypes);
}
Also used : TypeListener(com.google.inject.spi.TypeListener) TypeEncounter(com.google.inject.spi.TypeEncounter)

Example 8 with TypeEncounter

use of com.google.inject.spi.TypeEncounter in project roboguice by roboguice.

the class TypeListenerTest method testTypeListenersAreFired.

public void testTypeListenersAreFired() {
    final AtomicInteger firedCount = new AtomicInteger();
    final TypeListener typeListener = new TypeListener() {

        public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
            assertEquals(new TypeLiteral<A>() {
            }, type);
            firedCount.incrementAndGet();
        }
    };
    Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bindListener(onlyAbcd, typeListener);
            bind(A.class);
        }
    });
    assertEquals(1, firedCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TypeListener(com.google.inject.spi.TypeListener) TypeEncounter(com.google.inject.spi.TypeEncounter)

Example 9 with TypeEncounter

use of com.google.inject.spi.TypeEncounter in project roboguice by roboguice.

the class TypeListenerTest method testConstructedTypeListenerIsTheSameAsMembersInjectorListener.

public void testConstructedTypeListenerIsTheSameAsMembersInjectorListener() {
    final AtomicInteger typeEncounters = new AtomicInteger();
    final AtomicInteger injections = new AtomicInteger();
    final InjectionListener<A> listener = new InjectionListener<A>() {

        public void afterInjection(A injectee) {
            injections.incrementAndGet();
            assertNotNull(injectee.injector);
        }
    };
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bindListener(onlyAbcd, new TypeListener() {

                @SuppressWarnings("unchecked")
                public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
                    typeEncounters.incrementAndGet();
                    encounter.register((InjectionListener) listener);
                }
            });
            bind(A.class);
            getMembersInjector(A.class);
        }
    });
    // creating the injector shouldn't trigger injections
    assertEquals(0, injections.getAndSet(0));
    // constructing an A should trigger an injection
    injector.getInstance(A.class);
    assertEquals(1, injections.getAndSet(0));
    // injecting an A should trigger an injection
    injector.injectMembers(new A());
    assertEquals(1, injections.getAndSet(0));
    // getting a provider shouldn't
    Provider<A> aProvider = injector.getProvider(A.class);
    MembersInjector<A> aMembersInjector = injector.getMembersInjector(A.class);
    assertEquals(0, injections.getAndSet(0));
    // exercise the provider
    aProvider.get();
    aProvider.get();
    assertEquals(2, injections.getAndSet(0));
    // exercise the members injector
    aMembersInjector.injectMembers(new A());
    aMembersInjector.injectMembers(new A());
    assertEquals(2, injections.getAndSet(0));
    // we should only have encountered one type
    assertEquals(1, typeEncounters.getAndSet(0));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TypeListener(com.google.inject.spi.TypeListener) InjectionListener(com.google.inject.spi.InjectionListener) TypeEncounter(com.google.inject.spi.TypeEncounter)

Example 10 with TypeEncounter

use of com.google.inject.spi.TypeEncounter in project roboguice by roboguice.

the class TypeListenerTest method testAddErrors.

public void testAddErrors() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                requestInjection(new Object());
                bindListener(Matchers.any(), new TypeListener() {

                    public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
                        encounter.addError("There was an error on %s", type);
                        encounter.addError(new IllegalArgumentException("whoops!"));
                        encounter.addError(new Message("And another problem"));
                        encounter.addError(new IllegalStateException());
                    }
                });
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "1) There was an error on java.lang.Object", "2) An exception was caught and reported. Message: whoops!", "3) And another problem", "4) An exception was caught and reported. Message: null", "4 errors");
    }
}
Also used : Message(com.google.inject.spi.Message) TypeListener(com.google.inject.spi.TypeListener) TypeEncounter(com.google.inject.spi.TypeEncounter)

Aggregations

TypeEncounter (com.google.inject.spi.TypeEncounter)10 TypeListener (com.google.inject.spi.TypeListener)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 InjectionListener (com.google.inject.spi.InjectionListener)2 Message (com.google.inject.spi.Message)2