Search in sources :

Example 1 with TypeListener

use of com.google.inject.spi.TypeListener in project che by eclipse.

the class DestroyModule method configure.

@Override
protected void configure() {
    final Destroyer destroyer = new Destroyer(errorHandler);
    bind(Destroyer.class).toInstance(destroyer);
    bindListener(Matchers.any(), new TypeListener() {

        @Override
        public <T> void hear(TypeLiteral<T> type, TypeEncounter<T> encounter) {
            encounter.register(new InjectionListener<T>() {

                @Override
                public void afterInjection(T injectee) {
                    final Method[] methods = get(injectee.getClass(), annotationType);
                    if (methods.length > 0) {
                        // copy array when pass it outside
                        final Method[] copy = new Method[methods.length];
                        System.arraycopy(methods, 0, copy, 0, methods.length);
                        destroyer.add(injectee, copy);
                    }
                }
            });
        }
    });
}
Also used : TypeListener(com.google.inject.spi.TypeListener) Method(java.lang.reflect.Method) InjectionListener(com.google.inject.spi.InjectionListener)

Example 2 with TypeListener

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

the class TypeListenerTest method testTypesWithNoInjectableMembersAreNotified.

/**
   * We had a bug where we weren't notifying of types encountered for member injection when those
   * types had no members to be injected. Constructed types are always injected because they always
   * have at least one injection point: the class constructor.
   */
public void testTypesWithNoInjectableMembersAreNotified() {
    final AtomicInteger notificationCount = new AtomicInteger();
    Guice.createInjector(new AbstractModule() {

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

                public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
                    notificationCount.incrementAndGet();
                }
            });
            bind(C.class).toInstance(new C());
        }
    });
    assertEquals(1, notificationCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TypeListener(com.google.inject.spi.TypeListener) TypeEncounter(com.google.inject.spi.TypeEncounter)

Example 3 with TypeListener

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

the class MembersInjectorStore method createWithListeners.

/** Creates a new members injector and attaches both injection listeners and method aspects. */
private <T> MembersInjectorImpl<T> createWithListeners(TypeLiteral<T> type, Errors errors) throws ErrorsException {
    int numErrorsBefore = errors.size();
    Set<InjectionPoint> injectionPoints;
    try {
        injectionPoints = InjectionPoint.forInstanceMethodsAndFields(type);
    } catch (ConfigurationException e) {
        errors.merge(e.getErrorMessages());
        injectionPoints = e.getPartialValue();
    }
    ImmutableList<SingleMemberInjector> injectors = getInjectors(injectionPoints, errors);
    errors.throwIfNewErrors(numErrorsBefore);
    EncounterImpl<T> encounter = new EncounterImpl<T>(errors, injector.lookups);
    Set<TypeListener> alreadySeenListeners = Sets.newHashSet();
    for (TypeListenerBinding binding : typeListenerBindings) {
        TypeListener typeListener = binding.getListener();
        if (!alreadySeenListeners.contains(typeListener) && binding.getTypeMatcher().matches(type)) {
            alreadySeenListeners.add(typeListener);
            try {
                typeListener.hear(type, encounter);
            } catch (RuntimeException e) {
                errors.errorNotifyingTypeListener(binding, type, e);
            }
        }
    }
    encounter.invalidate();
    errors.throwIfNewErrors(numErrorsBefore);
    return new MembersInjectorImpl<T>(injector, type, encounter, injectors);
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) InjectionPoint(com.google.inject.spi.InjectionPoint) TypeListenerBinding(com.google.inject.spi.TypeListenerBinding) ConfigurationException(com.google.inject.ConfigurationException) TypeListener(com.google.inject.spi.TypeListener)

Example 4 with TypeListener

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

the class TypeListenerTest method testTypesWithNoInjectableMembersAreNotified.

/**
   * We had a bug where we weren't notifying of types encountered for member injection when those
   * types had no members to be injected. Constructed types are always injected because they always
   * have at least one injection point: the class constructor.
   */
public void testTypesWithNoInjectableMembersAreNotified() {
    final AtomicInteger notificationCount = new AtomicInteger();
    Guice.createInjector(new AbstractModule() {

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

                @Override
                public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
                    notificationCount.incrementAndGet();
                }
            });
            bind(C.class).toInstance(new C());
        }
    });
    assertEquals(1, notificationCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TypeListener(com.google.inject.spi.TypeListener) TypeEncounter(com.google.inject.spi.TypeEncounter)

Example 5 with TypeListener

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

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() {

                    @Override
                    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

TypeListener (com.google.inject.spi.TypeListener)15 TypeEncounter (com.google.inject.spi.TypeEncounter)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 InjectionListener (com.google.inject.spi.InjectionListener)4 Method (java.lang.reflect.Method)3 ConfigurationException (com.google.inject.ConfigurationException)2 MembersInjector (com.google.inject.MembersInjector)2 ProvisionException (com.google.inject.ProvisionException)2 TypeLiteral (com.google.inject.TypeLiteral)2 InjectionPoint (com.google.inject.spi.InjectionPoint)2 Message (com.google.inject.spi.Message)2 TypeListenerBinding (com.google.inject.spi.TypeListenerBinding)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Field (java.lang.reflect.Field)1 Map (java.util.Map)1 Set (java.util.Set)1 MethodKey (org.apache.camel.guice.support.internal.MethodKey)1