Search in sources :

Example 1 with MembersInjector

use of com.google.inject.MembersInjector in project roboguice by roboguice.

the class ElementsTest method testElementInitialization.

public void testElementInitialization() {
    final AtomicReference<Provider<String>> providerFromBinder = new AtomicReference<Provider<String>>();
    final AtomicReference<MembersInjector<String>> membersInjectorFromBinder = new AtomicReference<MembersInjector<String>>();
    final AtomicReference<String> lastInjected = new AtomicReference<String>();
    final MembersInjector<String> stringInjector = new MembersInjector<String>() {

        public void injectMembers(String instance) {
            lastInjected.set(instance);
        }
    };
    checkModule(new AbstractModule() {

        protected void configure() {
            providerFromBinder.set(getProvider(String.class));
            membersInjectorFromBinder.set(getMembersInjector(String.class));
        }
    }, new FailingElementVisitor() {

        public <T> Void visit(ProviderLookup<T> providerLookup) {
            // we know that T is a String here
            @SuppressWarnings("unchecked") ProviderLookup<String> stringLookup = (ProviderLookup<String>) providerLookup;
            stringLookup.initializeDelegate(Providers.of("out"));
            assertEquals("out", providerFromBinder.get().get());
            return null;
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(MembersInjectorLookup<T> lookup) {
            // we know that T is a String here
            @SuppressWarnings("unchecked") MembersInjectorLookup<String> stringLookup = (MembersInjectorLookup<String>) lookup;
            stringLookup.initializeDelegate(stringInjector);
            membersInjectorFromBinder.get().injectMembers("in");
            assertEquals("in", lastInjected.get());
            return null;
        }
    });
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Provider(com.google.inject.Provider) AbstractModule(com.google.inject.AbstractModule) MembersInjector(com.google.inject.MembersInjector)

Example 2 with MembersInjector

use of com.google.inject.MembersInjector in project guice by google.

the class ElementsTest method testElementInitialization.

public void testElementInitialization() {
    final AtomicReference<Provider<String>> providerFromBinder = new AtomicReference<Provider<String>>();
    final AtomicReference<MembersInjector<String>> membersInjectorFromBinder = new AtomicReference<MembersInjector<String>>();
    final AtomicReference<String> lastInjected = new AtomicReference<String>();
    final MembersInjector<String> stringInjector = new MembersInjector<String>() {

        @Override
        public void injectMembers(String instance) {
            lastInjected.set(instance);
        }
    };
    checkModule(new AbstractModule() {

        @Override
        protected void configure() {
            providerFromBinder.set(getProvider(String.class));
            membersInjectorFromBinder.set(getMembersInjector(String.class));
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(ProviderLookup<T> providerLookup) {
            // we know that T is a String here
            @SuppressWarnings("unchecked") ProviderLookup<String> stringLookup = (ProviderLookup<String>) providerLookup;
            stringLookup.initializeDelegate(Providers.of("out"));
            assertEquals("out", providerFromBinder.get().get());
            return null;
        }
    }, new FailingElementVisitor() {

        @Override
        public <T> Void visit(MembersInjectorLookup<T> lookup) {
            // we know that T is a String here
            @SuppressWarnings("unchecked") MembersInjectorLookup<String> stringLookup = (MembersInjectorLookup<String>) lookup;
            stringLookup.initializeDelegate(stringInjector);
            membersInjectorFromBinder.get().injectMembers("in");
            assertEquals("in", lastInjected.get());
            return null;
        }
    });
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Provider(com.google.inject.Provider) AbstractModule(com.google.inject.AbstractModule) MembersInjector(com.google.inject.MembersInjector)

Example 3 with MembersInjector

use of com.google.inject.MembersInjector in project guice by google.

the class InjectorImpl method injectMembers.

@Override
// the members injector type is consistent with instance's type
@SuppressWarnings("unchecked")
public void injectMembers(Object instance) {
    MembersInjector membersInjector = getMembersInjector(instance.getClass());
    membersInjector.injectMembers(instance);
}
Also used : MembersInjector(com.google.inject.MembersInjector)

Example 4 with MembersInjector

use of com.google.inject.MembersInjector in project guice by google.

the class InjectorImpl method createMembersInjectorBinding.

private <T> BindingImpl<MembersInjector<T>> createMembersInjectorBinding(Key<MembersInjector<T>> key, Errors errors) throws ErrorsException {
    Type membersInjectorType = key.getTypeLiteral().getType();
    if (!(membersInjectorType instanceof ParameterizedType)) {
        throw errors.cannotInjectRawMembersInjector().toException();
    }
    // safe because T came from Key<MembersInjector<T>>
    @SuppressWarnings("unchecked") TypeLiteral<T> instanceType = (TypeLiteral<T>) TypeLiteral.get(((ParameterizedType) membersInjectorType).getActualTypeArguments()[0]);
    MembersInjector<T> membersInjector = membersInjectorStore.get(instanceType, errors);
    InternalFactory<MembersInjector<T>> factory = new ConstantFactory<MembersInjector<T>>(Initializables.of(membersInjector));
    return new InstanceBindingImpl<MembersInjector<T>>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, ImmutableSet.<InjectionPoint>of(), membersInjector);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeLiteral(com.google.inject.TypeLiteral) MembersInjector(com.google.inject.MembersInjector)

Example 5 with MembersInjector

use of com.google.inject.MembersInjector in project camel by apache.

the class GuiceyFruitModule method configure.

protected void configure() {
    // lets find all of the configures methods
    List<Method> configureMethods = getConfiguresMethods();
    if (!configureMethods.isEmpty()) {
        final GuiceyFruitModule moduleInstance = this;
        final Class<? extends GuiceyFruitModule> moduleType = getClass();
        TypeLiteral<? extends GuiceyFruitModule> type = TypeLiteral.get(moduleType);
        for (final Method method : configureMethods) {
            int size = method.getParameterTypes().length;
            if (size == 0) {
                throw new ProvisionException("No arguments on @Configures method " + method);
            } else if (size > 1) {
                throw new ProvisionException("Too many arguments " + size + " on @Configures method " + method);
            }
            final Class<?> paramType = getParameterType(type, method, 0);
            bindListener(new AbstractMatcher<TypeLiteral<?>>() {

                public boolean matches(TypeLiteral<?> typeLiteral) {
                    return typeLiteral.getRawType().equals(paramType);
                }
            }, new TypeListener() {

                public <I> void hear(TypeLiteral<I> injectableType, TypeEncounter<I> encounter) {
                    encounter.register(new MembersInjector<I>() {

                        public void injectMembers(I injectee) {
                            // lets invoke the configures method
                            try {
                                method.setAccessible(true);
                                method.invoke(moduleInstance, injectee);
                            } catch (IllegalAccessException e) {
                                throw new ProvisionException("Failed to invoke @Configures method " + method + ". Reason: " + e, e);
                            } catch (InvocationTargetException ie) {
                                Throwable e = ie.getTargetException();
                                throw new ProvisionException("Failed to invoke @Configures method " + method + ". Reason: " + e, e);
                            }
                        }
                    });
                }
            });
        }
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProvisionException(com.google.inject.ProvisionException) TypeLiteral(com.google.inject.TypeLiteral) TypeListener(com.google.inject.spi.TypeListener) MembersInjector(com.google.inject.MembersInjector)

Aggregations

MembersInjector (com.google.inject.MembersInjector)8 TypeLiteral (com.google.inject.TypeLiteral)4 AbstractModule (com.google.inject.AbstractModule)2 Provider (com.google.inject.Provider)2 ProvisionException (com.google.inject.ProvisionException)2 TypeListener (com.google.inject.spi.TypeListener)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 InjectionListener (com.google.inject.spi.InjectionListener)1 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