com.coditory.quark.context.MethodBasedBeanCreator 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 java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Collectors;
import static com.coditory.quark.context.DependencyResolver.resolveArguments;
import static java.util.Objects.requireNonNull;
final class MethodBasedBeanCreator implements BeanCreator {
static String simplifyMethodName(Method method) {
String params = Arrays.stream(method.getParameterTypes())
.map(Class::getSimpleName)
.collect(Collectors.joining(", "));
return method.getName() + "(" + params + ")";
}
private final Method method;
private final BeanHolder> holder;
public MethodBasedBeanCreator(BeanHolder> holder, Method method) {
this.holder = requireNonNull(holder);
this.method = requireNonNull(method);
}
@SuppressWarnings("unchecked")
@Override
public T create(ResolutionContext context) {
Object object = holder.get(context);
Object[] args = resolveArguments(method, context);
try {
return (T) method.invoke(object, args);
} catch (Exception e) {
throw new ContextException("Could not create bean from method: " + simplifyMethodName(method), e);
}
}
@Override
public boolean isActive(ConditionContext context) {
return holder.isActive(context) && ConditionsResolver.isActive(context, method);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy