package com.google.inject.internal;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Key;
import com.google.inject.MembersInjector;
import com.google.inject.TypeLiteral;
import com.google.inject.internal.ProvisionListenerStackCallback.ProvisionCallback;
import com.google.inject.spi.InjectionListener;
import com.google.inject.spi.InjectionPoint;
final class MembersInjectorImpl<T> implements MembersInjector<T> {
private final TypeLiteral<T> typeLiteral;
private final InjectorImpl injector;
private final ImmutableList<SingleMemberInjector> memberInjectors;
private final ImmutableList<MembersInjector<? super T>> userMembersInjectors;
private final ImmutableList<InjectionListener<? super T>> injectionListeners;
private final ImmutableList<MethodAspect> addedAspects;
MembersInjectorImpl(
InjectorImpl injector,
TypeLiteral<T> typeLiteral,
EncounterImpl<T> encounter,
ImmutableList<SingleMemberInjector> memberInjectors) {
this.injector = injector;
this.typeLiteral = typeLiteral;
this.memberInjectors = memberInjectors.isEmpty() ? null : memberInjectors;
this.userMembersInjectors =
encounter.getMembersInjectors().isEmpty() ? null : encounter.getMembersInjectors().asList();
this.injectionListeners =
encounter.getInjectionListeners().isEmpty()
? null
: encounter.getInjectionListeners().asList();
this.addedAspects = encounter.getAspects().isEmpty() ? null : encounter.getAspects();
}
public ImmutableList<SingleMemberInjector> getMemberInjectors() {
return memberInjectors == null ? ImmutableList.<SingleMemberInjector>of() : memberInjectors;
}
@Override
public void injectMembers(T instance) {
TypeLiteral<T> localTypeLiteral = typeLiteral;
try {
injectAndNotify(instance, null, null, localTypeLiteral, false);
} catch (InternalProvisionException ipe) {
throw ipe.addSource(localTypeLiteral).toProvisionException();
}
}
void injectAndNotify(
final T instance,
final Key<T> key,
final ProvisionListenerStackCallback<T> provisionCallback,
final Object source,
final boolean toolableOnly)
throws InternalProvisionException {
if (instance == null) {
return;
}
final InternalContext context = injector.enterContext();
context.pushState(key, source);
try {
if (provisionCallback != null && provisionCallback.hasListeners()) {
provisionCallback.provision(
context,
new ProvisionCallback<T>() {
@Override
public T call() throws InternalProvisionException {
injectMembers(instance, context, toolableOnly);
return instance;
}
});
} else {
injectMembers(instance, context, toolableOnly);
}
} finally {
context.popState();
context.close();
}
if (!toolableOnly) {
notifyListeners(instance);
}
}
void notifyListeners(T instance) throws InternalProvisionException {
ImmutableList<InjectionListener<? super T>> localInjectionListeners = injectionListeners;
if (localInjectionListeners == null) {
return;
}
for (int i = 0; i < localInjectionListeners.size(); i++) {
InjectionListener<? super T> injectionListener = localInjectionListeners.get(i);
try {
injectionListener.afterInjection(instance);
} catch (RuntimeException e) {
throw InternalProvisionException.errorNotifyingInjectionListener(
injectionListener, typeLiteral, e);
}
}
}
void injectMembers(T t, InternalContext context, boolean toolableOnly)
throws InternalProvisionException {
ImmutableList<SingleMemberInjector> localMembersInjectors = memberInjectors;
if (localMembersInjectors != null) {
for (int i = 0, size = localMembersInjectors.size(); i < size; i++) {
SingleMemberInjector injector = localMembersInjectors.get(i);
if (!toolableOnly || injector.getInjectionPoint().isToolable()) {
injector.inject(context, t);
}
}
}
if (!toolableOnly) {
ImmutableList<MembersInjector<? super T>> localUsersMembersInjectors = userMembersInjectors;
if (localUsersMembersInjectors != null) {
for (int i = 0; i < localUsersMembersInjectors.size(); i++) {
MembersInjector<? super T> userMembersInjector = localUsersMembersInjectors.get(i);
try {
userMembersInjector.injectMembers(t);
} catch (RuntimeException e) {
throw InternalProvisionException.errorInUserInjector(
userMembersInjector, typeLiteral, e);
}
}
}
}
}
@Override
public String toString() {
return "MembersInjector<" + typeLiteral + ">";
}
public ImmutableSet<InjectionPoint> getInjectionPoints() {
ImmutableList<SingleMemberInjector> localMemberInjectors = memberInjectors;
if (localMemberInjectors != null) {
ImmutableSet.Builder<InjectionPoint> builder = ImmutableSet.builder();
for (SingleMemberInjector memberInjector : localMemberInjectors) {
builder.add(memberInjector.getInjectionPoint());
}
return builder.build();
}
return ImmutableSet.of();
}
public ImmutableList<MethodAspect> getAddedAspects() {
return addedAspects == null ? ImmutableList.<MethodAspect>of() : addedAspects;
}
}