com.google.inject.internal.BindingImpl Maven / Gradle / Ivy
package com.google.inject.internal;
import com.google.common.base.MoreObjects;
import com.google.inject.Binding;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.spi.BindingScopingVisitor;
import com.google.inject.spi.ElementVisitor;
import com.google.inject.spi.InstanceBinding;
public abstract class BindingImpl implements Binding {
private final InjectorImpl injector;
private final Key key;
private final Object source;
private final Scoping scoping;
private final InternalFactory extends T> internalFactory;
private volatile Provider provider;
public BindingImpl(InjectorImpl injector, Key key, Object source,
InternalFactory extends T> internalFactory, Scoping scoping) {
this.injector = injector;
this.key = key;
this.source = source;
this.internalFactory = internalFactory;
this.scoping = scoping;
}
protected BindingImpl(Object source, Key key, Scoping scoping) {
this.internalFactory = null;
this.injector = null;
this.source = source;
this.key = key;
this.scoping = scoping;
}
@Override
public Key getKey() {
return key;
}
@Override
public Object getSource() {
return source;
}
@Override
public Provider getProvider() {
if (provider == null) {
if (injector == null) {
throw new UnsupportedOperationException("getProvider() not supported for module bindings");
}
provider = injector.getProvider(key);
}
return provider;
}
public InternalFactory extends T> getInternalFactory() {
return internalFactory;
}
public Scoping getScoping() {
return scoping;
}
/**
* Is this a constant binding? This returns true for constant bindings as
* well as toInstance() bindings.
*/
public boolean isConstant() {
return this instanceof InstanceBinding;
}
@Override
public V acceptVisitor(ElementVisitor visitor) {
return visitor.visit(this);
}
@Override
public V acceptScopingVisitor(BindingScopingVisitor visitor) {
return scoping.acceptVisitor(visitor);
}
protected BindingImpl withScoping(Scoping scoping) {
throw new AssertionError();
}
protected BindingImpl withKey(Key key) {
throw new AssertionError();
}
@Override
public String toString() {
return MoreObjects.toStringHelper(Binding.class)
.add("key", key)
.add("scope", scoping)
.add("source", source)
.toString();
}
public InjectorImpl getInjector() {
return injector;
}
}