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

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

The newest version!
package io.smallrye.beanbag;

import java.lang.reflect.Field;

/**
 * A field injection.
 */
final class FieldInjector implements Injector {
    private final Field field;
    private final BeanSupplier supplier;

    FieldInjector(final Field field, final BeanSupplier supplier) {
        this.field = field;
        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 field "
                    + field.getDeclaringClass().getSimpleName() + "#" + field.getName() + " of object " + instance, t);
        }
        try {
            field.set(instance, value);
        } catch (IllegalAccessException e) {
            throw new InjectionException("Failed to inject value " + value + " into field "
                    + field.getDeclaringClass().getSimpleName() + "#" + field.getName() + " of object " + instance, e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy