All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.smallrye.beanbag.MethodInjector Maven / Gradle / Ivy

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);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy