package com.google.inject.internal;
import com.google.inject.spi.Dependency;
final class SingleParameterInjector<T> {
private static final Object[] NO_ARGUMENTS = {};
private final Dependency<T> dependency;
private final Object source;
private final InternalFactory<? extends T> factory;
SingleParameterInjector(Dependency<T> dependency, BindingImpl<? extends T> binding) {
this.dependency = dependency;
this.source = binding.getSource();
this.factory = binding.getInternalFactory();
}
T inject(InternalContext context) throws InternalProvisionException {
Dependency<T> 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);
}
}
static Object[] getAll(InternalContext context, SingleParameterInjector<?>[] parameterInjectors)
throws InternalProvisionException {
if (parameterInjectors == null) {
return NO_ARGUMENTS;
}
int size = parameterInjectors.length;
Object[] parameters = new Object[size];
for (int i = 0; i < size; i++) {
parameters[i] = parameterInjectors[i].inject(context);
}
return parameters;
}
}