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;
}
});
}
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;
}
});
}
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);
}
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);
}
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);
}
}
});
}
});
}
}
}
Aggregations