Search in sources :

Example 1 with InjectionListener

use of com.google.inject.spi.InjectionListener 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 InjectionListener

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

the class MembersInjectorImpl method notifyListeners.

void notifyListeners(T instance, Errors errors) throws ErrorsException {
    ImmutableList<InjectionListener<? super T>> localInjectionListeners = injectionListeners;
    if (localInjectionListeners == null) {
        // no listeners
        return;
    }
    int numErrorsBefore = errors.size();
    // optimization: use manual for/each to save allocating an iterator here
    for (int i = 0; i < localInjectionListeners.size(); i++) {
        InjectionListener<? super T> injectionListener = localInjectionListeners.get(i);
        try {
            injectionListener.afterInjection(instance);
        } catch (RuntimeException e) {
            errors.errorNotifyingInjectionListener(injectionListener, typeLiteral, e);
        }
    }
    errors.throwIfNewErrors(numErrorsBefore);
}
Also used : InjectionListener(com.google.inject.spi.InjectionListener) InjectionPoint(com.google.inject.spi.InjectionPoint)

Example 3 with InjectionListener

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

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

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

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

                @Override
                @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 4 with InjectionListener

use of com.google.inject.spi.InjectionListener in project camel by apache.

the class GuiceyFruitModule method bindAnnotationInjector.

private <A extends Annotation> void bindAnnotationInjector(final Class<A> annotationType, final EncounterProvider<AnnotationMemberProvider> memberProviderProvider) {
    bindListener(any(), new TypeListener() {

        Provider<? extends AnnotationMemberProvider> providerProvider;

        public <I> void hear(TypeLiteral<I> injectableType, TypeEncounter<I> encounter) {
            Set<Field> boundFields = Sets.newHashSet();
            Map<MethodKey, Method> boundMethods = Maps.newHashMap();
            TypeLiteral<?> startType = injectableType;
            while (true) {
                Class<?> type = startType.getRawType();
                if (type == Object.class) {
                    break;
                }
                Field[] fields = type.getDeclaredFields();
                for (Field field : fields) {
                    if (boundFields.add(field)) {
                        bindAnnotationInjectorToField(encounter, startType, field);
                    }
                }
                Method[] methods = type.getDeclaredMethods();
                for (final Method method : methods) {
                    MethodKey key = new MethodKey(method);
                    if (boundMethods.get(key) == null) {
                        boundMethods.put(key, method);
                        bindAnnotationInjectionToMember(encounter, startType, method);
                    }
                }
                Class<?> supertype = type.getSuperclass();
                if (supertype == Object.class) {
                    break;
                }
                startType = startType.getSupertype(supertype);
            }
        }

        protected <I> void bindAnnotationInjectionToMember(final TypeEncounter<I> encounter, final TypeLiteral<?> type, final Method method) {
            // TODO lets exclude methods with @Inject?
            final A annotation = method.getAnnotation(annotationType);
            if (annotation != null) {
                if (providerProvider == null) {
                    providerProvider = memberProviderProvider.get(encounter);
                }
                encounter.register(new MembersInjector<I>() {

                    public void injectMembers(I injectee) {
                        AnnotationMemberProvider provider = providerProvider.get();
                        int size = method.getParameterTypes().length;
                        Object[] values = new Object[size];
                        for (int i = 0; i < size; i++) {
                            Class<?> paramType = getParameterType(type, method, i);
                            Object value = provider.provide(annotation, type, method, paramType, i);
                            checkInjectedValueType(value, paramType, encounter);
                            // things
                            if (value == null && !provider.isNullParameterAllowed(annotation, method, paramType, i)) {
                                return;
                            }
                            values[i] = value;
                        }
                        try {
                            method.setAccessible(true);
                            method.invoke(injectee, values);
                        } catch (IllegalAccessException e) {
                            throw new ProvisionException("Failed to inject method " + method + ". Reason: " + e, e);
                        } catch (InvocationTargetException ie) {
                            Throwable e = ie.getTargetException();
                            throw new ProvisionException("Failed to inject method " + method + ". Reason: " + e, e);
                        }
                    }
                });
            }
        }

        protected <I> void bindAnnotationInjectorToField(final TypeEncounter<I> encounter, final TypeLiteral<?> type, final Field field) {
            // TODO lets exclude fields with @Inject?
            final A annotation = field.getAnnotation(annotationType);
            if (annotation != null) {
                if (providerProvider == null) {
                    providerProvider = memberProviderProvider.get(encounter);
                }
                encounter.register(new InjectionListener<I>() {

                    public void afterInjection(I injectee) {
                        AnnotationMemberProvider provider = providerProvider.get();
                        Object value = provider.provide(annotation, type, field);
                        checkInjectedValueType(value, field.getType(), encounter);
                        try {
                            field.setAccessible(true);
                            field.set(injectee, value);
                        } catch (IllegalAccessException e) {
                            throw new ProvisionException("Failed to inject field " + field + ". Reason: " + e, e);
                        }
                    }
                });
            }
        }
    });
}
Also used : Set(java.util.Set) InjectionListener(com.google.inject.spi.InjectionListener) Field(java.lang.reflect.Field) ProvisionException(com.google.inject.ProvisionException) TypeLiteral(com.google.inject.TypeLiteral) MethodKey(org.apache.camel.guice.support.internal.MethodKey) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) TypeListener(com.google.inject.spi.TypeListener) MembersInjector(com.google.inject.MembersInjector) Map(java.util.Map)

Example 5 with InjectionListener

use of com.google.inject.spi.InjectionListener 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)

Aggregations

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