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

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

package com.google.inject.internal;

import com.google.inject.spi.Dependency;

/**
 * Resolves a single parameter, to be used in a constructor or method invocation.
 */
final class SingleParameterInjector {

    private static final Object[] NO_ARGUMENTS = {};

    private final Dependency dependency;

    private final Object source;

    private final InternalFactory factory;

    SingleParameterInjector(Dependency dependency, BindingImpl binding) {
        this.dependency = dependency;
        this.source = binding.getSource();
        this.factory = binding.getInternalFactory();
    }

    /**
     * Returns an array of parameter values.
     */
    static Object[] getAll(InternalContext context,
                           SingleParameterInjector[] parameterInjectors) throws InternalProvisionException {
        if (parameterInjectors == null) {
            return NO_ARGUMENTS;
        }
        int size = parameterInjectors.length;
        Object[] parameters = new Object[size];

        // optimization: use manual for/each to save allocating an iterator here
        for (int i = 0; i < size; i++) {
            parameters[i] = parameterInjectors[i].inject(context);
        }
        return parameters;
    }

    T inject(InternalContext context) throws InternalProvisionException {
        Dependency localDependency = dependency;
        Dependency previous = context.pushDependency(localDependency, source);

        try {
            return factory.get(context, localDependency, false);
        } catch (InternalProvisionException ipe) {
            throw ipe.addSource(localDependency);
        } finally {
            context.popStateAndSetDependency(previous);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy