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