package com.google.inject.internal;
import com.google.common.collect.ImmutableSet;
import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.Binding;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.MembersInjector;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Scope;
import com.google.inject.Stage;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.DefaultBindingTargetVisitor;
abstract class AbstractBindingProcessor extends AbstractProcessor {
private static final ImmutableSet<Class<?>> FORBIDDEN_TYPES =
ImmutableSet.<Class<?>>of(
AbstractModule.class,
Binder.class,
Binding.class,
Injector.class,
Key.class,
MembersInjector.class,
Module.class,
Provider.class,
Scope.class,
Stage.class,
TypeLiteral.class);
protected final ProcessedBindingData bindingData;
AbstractBindingProcessor(Errors errors, ProcessedBindingData bindingData) {
super(errors);
this.bindingData = bindingData;
}
protected <T> UntargettedBindingImpl<T> invalidBinding(
InjectorImpl injector, Key<T> key, Object source) {
return new UntargettedBindingImpl<T>(injector, key, source);
}
protected void putBinding(BindingImpl<?> binding) {
Key<?> key = binding.getKey();
Class<?> rawType = key.getTypeLiteral().getRawType();
if (FORBIDDEN_TYPES.contains(rawType)) {
errors.cannotBindToGuiceType(rawType.getSimpleName());
return;
}
BindingImpl<?> original = injector.getExistingBinding(key);
if (original != null) {
if (injector.state.getExplicitBinding(key) != null) {
try {
if (!isOkayDuplicate(original, binding, injector.state)) {
errors.bindingAlreadySet(key, original.getSource());
return;
}
} catch (Throwable t) {
errors.errorCheckingDuplicateBinding(key, original.getSource(), t);
return;
}
} else {
errors.jitBindingAlreadySet(key);
return;
}
}
injector.state.parent().blacklist(key, injector.state, binding.getSource());
injector.state.putBinding(key, binding);
}
private boolean isOkayDuplicate(BindingImpl<?> original, BindingImpl<?> binding, State state) {
if (original instanceof ExposedBindingImpl) {
ExposedBindingImpl exposed = (ExposedBindingImpl) original;
InjectorImpl exposedFrom = (InjectorImpl) exposed.getPrivateElements().getInjector();
return (exposedFrom == binding.getInjector());
} else {
original = (BindingImpl<?>) state.getExplicitBindingsThisLevel().get(binding.getKey());
if (original == null) {
return false;
} else {
return original.equals(binding);
}
}
}
private <T> void validateKey(Object source, Key<T> key) {
Annotations.checkForMisplacedScopeAnnotations(
key.getTypeLiteral().getRawType(), source, errors);
}
abstract class Processor<T, V> extends DefaultBindingTargetVisitor<T, V> {
final Object source;
final Key<T> key;
final Class<? super T> rawType;
Scoping scoping;
Processor(BindingImpl<T> binding) {
source = binding.getSource();
key = binding.getKey();
rawType = key.getTypeLiteral().getRawType();
scoping = binding.getScoping();
}
protected void prepareBinding() {
validateKey(source, key);
scoping = Scoping.makeInjectable(scoping, injector, errors);
}
protected void scheduleInitialization(BindingImpl<?> binding) {
bindingData.addUninitializedBinding(() -> initializeBinding(binding));
}
protected void scheduleDelayedInitialization(BindingImpl<?> binding) {
bindingData.addDelayedUninitializedBinding(() -> initializeBinding(binding));
}
private void initializeBinding(BindingImpl<?> binding) {
try {
binding.getInjector().initializeBinding(binding, errors.withSource(source));
} catch (ErrorsException e) {
errors.merge(e.getErrors());
}
}
}
}