}
public <I> void hear(TypeLiteral<I> literal, TypeEncounter<I> encounter) {
final List<Method> constructs = new ArrayList<Method>();
final List<Method> destroys = new ArrayList<Method>();
for (Method method : new Mirror().on(literal.getRawType()).reflectAll().methods()) {
if (method.isAnnotationPresent(PostConstruct.class)) {
constructs.add(method);
}
if (method.isAnnotationPresent(PreDestroy.class)) {
destroys.add(method);
}
}
logger.debug("Registering lifecycle listeners for {}", literal);
if (!constructs.isEmpty() || !destroys.isEmpty()) {
encounter.register(new InjectionListener() {
public void afterInjection(final Object instance) {
for (Method method : constructs) {
new Mirror().on(instance).invoke().method(method).withoutArgs();
}
scope.registerDestroyListener(new LifecycleListener() {
public void onEvent() {
for (Method method : destroys) {
new Mirror().on(instance).invoke().method(method).withoutArgs();
}
}
});
}