com.google.inject.internal.ProviderInternalFactory Maven / Gradle / Ivy
package com.google.inject.internal;
import com.google.inject.spi.Dependency;
import javax.inject.Provider;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Base class for InternalFactories that are used by Providers, to handle
* circular dependencies.
*
*/
abstract class ProviderInternalFactory implements InternalFactory {
protected final Object source;
ProviderInternalFactory(Object source) {
this.source = checkNotNull(source, "source");
}
protected T circularGet(final Provider extends T> provider,
InternalContext context,
final Dependency> dependency,
ProvisionListenerStackCallback provisionCallback)
throws InternalProvisionException {
final ConstructionContext constructionContext = context.getConstructionContext(this);
// We have a circular reference between constructors. Return a proxy.
if (constructionContext.isConstructing()) {
Class> expectedType = dependency.getKey().getTypeLiteral().getRawType();
// TODO: if we can't proxy this object, can we proxy the other object?
@SuppressWarnings("unchecked")
T proxyType = (T) constructionContext.createProxy(context.getInjectorOptions(), expectedType);
return proxyType;
}
// Optimization: Don't go through the callback stack if no one's listening.
constructionContext.startConstruction();
try {
if (!provisionCallback.hasListeners()) {
return provision(provider, dependency, constructionContext);
} else {
return provisionCallback.provision(context, () ->
provision(provider, dependency, constructionContext));
}
} finally {
constructionContext.removeCurrentReference();
constructionContext.finishConstruction();
}
}
/**
* Provisions a new instance. Subclasses should override this to catch
* exceptions & rethrow as ErrorsExceptions.
*/
protected T provision(Provider extends T> provider, Dependency> dependency,
ConstructionContext constructionContext) throws InternalProvisionException {
T t = provider.get();
if (t == null && !dependency.isNullable()) {
InternalProvisionException.onNullInjectedIntoNonNullableDependency(source, dependency);
}
constructionContext.setProxyDelegates(t);
return t;
}
}