com.google.inject.internal.InheritingState Maven / Gradle / Ivy
package com.google.inject.internal;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.inject.Binding;
import com.google.inject.Key;
import com.google.inject.Scope;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.InjectionRequest;
import com.google.inject.spi.MembersInjectorLookup;
import com.google.inject.spi.ModuleAnnotatedMethodScannerBinding;
import com.google.inject.spi.ProviderLookup;
import com.google.inject.spi.ProvisionListenerBinding;
import com.google.inject.spi.ScopeBinding;
import com.google.inject.spi.StaticInjectionRequest;
import com.google.inject.spi.TypeConverterBinding;
import com.google.inject.spi.TypeListenerBinding;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
final class InheritingState implements State {
private final State parent;
// Must be a linked hashmap in order to preserve order of bindings in Modules.
private final Map, Binding>> explicitBindingsMutable = Maps.newLinkedHashMap();
private final Map, Binding>> explicitBindings =
Collections.unmodifiableMap(explicitBindingsMutable);
private final Map, ScopeBinding> scopes = Maps.newHashMap();
private final Set> providerLookups = Sets.newLinkedHashSet();
private final Set staticInjectionRequests = Sets.newLinkedHashSet();
private final Set> membersInjectorLookups = Sets.newLinkedHashSet();
private final Set> injectionRequests = Sets.newLinkedHashSet();
private final List converters = Lists.newArrayList();
private final List typeListenerBindings = Lists.newArrayList();
private final List provisionListenerBindings = Lists.newArrayList();
private final List scannerBindings = Lists.newArrayList();
private final WeakKeySet blacklistedKeys;
private final Object lock;
InheritingState(State parent) {
this.parent = checkNotNull(parent, "parent");
this.lock = (parent == State.NONE) ? this : parent.lock();
this.blacklistedKeys = new WeakKeySet(lock);
}
@Override
public State parent() {
return parent;
}
@SuppressWarnings("unchecked") // we only put in BindingImpls that match their key types
@Override
public BindingImpl getExplicitBinding(Key key) {
Binding> binding = explicitBindings.get(key);
return binding != null ? (BindingImpl) binding : parent.getExplicitBinding(key);
}
@Override
public Map, Binding>> getExplicitBindingsThisLevel() {
return explicitBindings;
}
@Override
public void putBinding(Key> key, BindingImpl> binding) {
explicitBindingsMutable.put(key, binding);
}
@Override
public void putProviderLookup(ProviderLookup> lookup) {
providerLookups.add(lookup);
}
@Override
public Set> getProviderLookupsThisLevel() {
return providerLookups;
}
@Override
public void putStaticInjectionRequest(StaticInjectionRequest staticInjectionRequest) {
staticInjectionRequests.add(staticInjectionRequest);
}
@Override
public Set getStaticInjectionRequestsThisLevel() {
return staticInjectionRequests;
}
@Override
public void putInjectionRequest(InjectionRequest> injectionRequest) {
injectionRequests.add(injectionRequest);
}
@Override
public Set> getInjectionRequestsThisLevel() {
return injectionRequests;
}
@Override
public void putMembersInjectorLookup(MembersInjectorLookup> membersInjectorLookup) {
membersInjectorLookups.add(membersInjectorLookup);
}
@Override
public Set> getMembersInjectorLookupsThisLevel() {
return membersInjectorLookups;
}
@Override
public ScopeBinding getScopeBinding(Class extends Annotation> annotationType) {
ScopeBinding scopeBinding = scopes.get(annotationType);
return scopeBinding != null ? scopeBinding : parent.getScopeBinding(annotationType);
}
@Override
public void putScopeBinding(Class extends Annotation> annotationType, ScopeBinding scope) {
scopes.put(annotationType, scope);
}
@Override
public Collection getScopeBindingsThisLevel() {
return scopes.values();
}
@Override
public Iterable getConvertersThisLevel() {
return converters;
}
@Override
public void addConverter(TypeConverterBinding typeConverterBinding) {
converters.add(typeConverterBinding);
}
@Override
public TypeConverterBinding getConverter(
String stringValue, TypeLiteral> type, Errors errors, Object source) {
TypeConverterBinding matchingConverter = null;
for (State s = this; s != State.NONE; s = s.parent()) {
for (TypeConverterBinding converter : s.getConvertersThisLevel()) {
if (converter.getTypeMatcher().matches(type)) {
if (matchingConverter != null) {
errors.ambiguousTypeConversion(stringValue, source, type, matchingConverter, converter);
}
matchingConverter = converter;
}
}
}
return matchingConverter;
}
@Override
public void addTypeListener(TypeListenerBinding listenerBinding) {
typeListenerBindings.add(listenerBinding);
}
@Override
public List getTypeListenerBindings() {
List parentBindings = parent.getTypeListenerBindings();
List result =
Lists.newArrayListWithCapacity(parentBindings.size() + typeListenerBindings.size());
result.addAll(parentBindings);
result.addAll(typeListenerBindings);
return result;
}
@Override
public List getTypeListenerBindingsThisLevel() {
return typeListenerBindings;
}
@Override
public void addProvisionListener(ProvisionListenerBinding listenerBinding) {
provisionListenerBindings.add(listenerBinding);
}
@Override
public List getProvisionListenerBindings() {
List parentBindings = parent.getProvisionListenerBindings();
List result =
Lists.newArrayListWithCapacity(parentBindings.size() + provisionListenerBindings.size());
result.addAll(parentBindings);
result.addAll(provisionListenerBindings);
return result;
}
@Override
public List getProvisionListenerBindingsThisLevel() {
return provisionListenerBindings;
}
@Override
public void addScanner(ModuleAnnotatedMethodScannerBinding scanner) {
scannerBindings.add(scanner);
}
@Override
public List getScannerBindings() {
List parentBindings = parent.getScannerBindings();
List result =
Lists.newArrayListWithCapacity(parentBindings.size() + scannerBindings.size());
result.addAll(parentBindings);
result.addAll(scannerBindings);
return result;
}
@Override
public List getScannerBindingsThisLevel() {
return scannerBindings;
}
@Override
public void blacklist(Key> key, State state, Object source) {
parent.blacklist(key, state, source);
blacklistedKeys.add(key, state, source);
}
@Override
public boolean isBlacklisted(Key> key) {
return blacklistedKeys.contains(key);
}
@Override
public Set