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

com.google.inject.internal.SingleFieldInjector Maven / Gradle / Ivy

package com.google.inject.internal;

import com.google.inject.internal.InjectorImpl.JitLimitation;
import com.google.inject.spi.Dependency;
import com.google.inject.spi.InjectionPoint;

import java.lang.reflect.Field;

/**
 * Sets an injectable field.
 */
final class SingleFieldInjector implements SingleMemberInjector {

    private final Field field;

    private final InjectionPoint injectionPoint;

    private final Dependency dependency;

    private final BindingImpl binding;

    SingleFieldInjector(InjectorImpl injector, InjectionPoint injectionPoint, Errors errors)
            throws ErrorsException {
        this.injectionPoint = injectionPoint;
        this.field = (Field) injectionPoint.getMember();
        this.dependency = injectionPoint.getDependencies().get(0);
        // Ewwwww...
        field.setAccessible(true);
        binding = injector.getBindingOrThrow(dependency.getKey(), errors, JitLimitation.NO_JIT);
    }

    @Override
    public InjectionPoint getInjectionPoint() {
        return injectionPoint;
    }

    @Override
    public void inject(InternalContext context, Object o) throws InternalProvisionException {
        Dependency previous = context.pushDependency(dependency, binding.getSource());
        try {
            Object value = binding.getInternalFactory().get(context, dependency, false);
            field.set(o, value);
        } catch (InternalProvisionException e) {
            throw e.addSource(dependency);
        } catch (IllegalAccessException e) {
            throw new AssertionError(e); // a security manager is blocking us, we're hosed
        } finally {
            context.popStateAndSetDependency(previous);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy