io.smallrye.beanbag.MethodInjector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smallrye-beanbag Show documentation
Show all versions of smallrye-beanbag Show documentation
A trivial programmatic bean container implementation
The newest version!
package io.smallrye.beanbag;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* A method injection.
*/
final class MethodInjector implements Injector {
private final Method method;
private final BeanSupplier supplier;
MethodInjector(final Method method, final BeanSupplier supplier) {
this.method = method;
this.supplier = supplier;
}
public void injectInto(Scope scope, C instance) {
final T value;
try {
value = supplier.get(scope);
} catch (Throwable t) {
throw new InjectionException("Failed to acquire value from provider for method "
+ method.getDeclaringClass().getSimpleName() + "#" + method.getName() + " of object " + instance, t);
}
try {
method.invoke(instance, value);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new InjectionException("Failed to inject value " + value + " into method "
+ method.getDeclaringClass().getSimpleName() + "#" + method.getName() + " of object " + instance, e);
}
}
}