com.coditory.quark.context.BeanInitializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quark-context Show documentation
Show all versions of quark-context Show documentation
Coditory Quark Configuration Library
package com.coditory.quark.context;
import com.coditory.quark.context.annotations.Init;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Method;
import static com.coditory.quark.context.DependencyResolver.resolveArguments;
import static com.coditory.quark.context.MethodBasedBeanCreator.simplifyMethodName;
final class BeanInitializer {
private static final Logger log = LoggerFactory.getLogger(BeanInitializer.class);
private BeanInitializer() {
throw new UnsupportedOperationException("Do not instantiate utility class");
}
static void initializeBean(Object bean, BeanDescriptor> descriptor, ResolutionContext context) {
if (bean instanceof Initializable) {
initializeBean((Initializable) bean, descriptor);
}
for (Method method : bean.getClass().getDeclaredMethods()) {
if (method.isAnnotationPresent(Init.class)) {
method.setAccessible(true);
initializeBean(bean, descriptor, method, context);
}
}
}
private static void initializeBean(Object bean, BeanDescriptor> descriptor, Method method, ResolutionContext context) {
try {
Object[] args = resolveArguments(method, context);
method.invoke(bean, args);
log.debug("Initialized bean: " + descriptor.toShortString() + " using method: " + simplifyMethodName(method));
} catch (Exception e) {
throw new BeanInitializationException("Could not initialize bean: " + descriptor.toShortString() + " using method: " + simplifyMethodName(method), e);
}
}
private static void initializeBean(Initializable bean, BeanDescriptor> descriptor) {
try {
bean.init();
log.debug("Initialized bean: " + descriptor.toShortString());
} catch (Exception e) {
throw new BeanInitializationException("Could not initialize bean: " + descriptor.toShortString(), e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy