Modifier and Type | Class and Description |
---|---|
private static class |
InjectorImpl.BindingsMultimap |
private static class |
InjectorImpl.ConvertedConstantBindingImpl<T> |
(package private) static class |
InjectorImpl.InjectorOptions
Options that control how the injector behaves.
|
(package private) static class |
InjectorImpl.JitLimitation
some limitations on what just in time bindings are allowed.
|
(package private) static interface |
InjectorImpl.MethodInvoker
Invokes a method.
|
private static class |
InjectorImpl.ProviderBindingImpl<T> |
Modifier and Type | Field and Description |
---|---|
(package private) InjectorImpl.BindingsMultimap |
bindingsMultimap |
(package private) ConstructorInjectorStore |
constructors
Cached constructor injectors for each type
|
(package private) java.util.Set<Key<?>> |
failedJitBindings
Cache of Keys that we were unable to create JIT bindings for, so we don't
keep trying.
|
(package private) java.util.Map<Key<?>,BindingImpl<?>> |
jitBindings
Just-in-time binding cache.
|
private java.lang.ThreadLocal<java.lang.Object[]> |
localContext
Holds Object[] as a mutable wrapper, rather than InternalContext, since array operations are
faster than ThreadLocal.set() / .get() operations.
|
(package private) Lookups |
lookups |
(package private) MembersInjectorStore |
membersInjectorStore
Cached field and method injectors for each type.
|
(package private) InjectorImpl.InjectorOptions |
options |
(package private) InjectorImpl |
parent |
(package private) ProvisionListenerCallbackStore |
provisionListenerStore
Cached provision listener callbacks for each key.
|
(package private) State |
state |
static TypeLiteral<java.lang.String> |
STRING_TYPE |
Constructor and Description |
---|
InjectorImpl(InjectorImpl parent,
State state,
InjectorImpl.InjectorOptions injectorOptions) |
Modifier and Type | Method and Description |
---|---|
(package private) <T> T |
callInContext(ContextualCallable<T> callable)
Looks up thread local context.
|
private boolean |
cleanup(BindingImpl<?> binding,
java.util.Set<Key> encountered)
Iterates through the binding's dependencies to clean up any stray bindings that were leftover
from a failed JIT binding.
|
private <T> BindingImpl<T> |
convertConstantStringBinding(Key<T> key,
Errors errors)
Converts a constant string binding to the required type.
|
Injector |
createChildInjector(java.lang.Iterable<? extends Module> modules)
Returns a new injector that inherits all state from this injector.
|
Injector |
createChildInjector(Module... modules)
Returns a new injector that inherits all state from this injector.
|
private <T> BindingImpl<T> |
createImplementedByBinding(Key<T> key,
Scoping scoping,
ImplementedBy implementedBy,
Errors errors)
Creates a binding for a type annotated with @ImplementedBy.
|
private <T> BindingImpl<T> |
createJustInTimeBinding(Key<T> key,
Errors errors,
boolean jitDisabled,
InjectorImpl.JitLimitation jitType)
Returns a new just-in-time binding created by resolving
key . |
private <T> BindingImpl<T> |
createJustInTimeBindingRecursive(Key<T> key,
Errors errors,
boolean jitDisabled,
InjectorImpl.JitLimitation jitType)
Attempts to create a just-in-time binding for
key in the root injector, falling back to
other ancestor injectors until this injector is tried. |
private <T> BindingImpl<MembersInjector<T>> |
createMembersInjectorBinding(Key<MembersInjector<T>> key,
Errors errors) |
(package private) <T> SingleParameterInjector<T> |
createParameterInjector(Dependency<T> dependency,
Errors errors) |
(package private) <T> BindingImpl<T> |
createProvidedByBinding(Key<T> key,
Scoping scoping,
ProvidedBy providedBy,
Errors errors)
Creates a binding for a type annotated with @ProvidedBy.
|
private <T> BindingImpl<Provider<T>> |
createProviderBinding(Key<Provider<T>> key,
Errors errors)
Creates a synthetic binding to
Provider<T> , i.e. |
private <T> BindingImpl<TypeLiteral<T>> |
createTypeLiteralBinding(Key<TypeLiteral<T>> key,
Errors errors)
Converts a binding for a
Key<TypeLiteral<T>> to the value TypeLiteral<T> . |
(package private) <T> BindingImpl<T> |
createUninitializedBinding(Key<T> key,
Scoping scoping,
java.lang.Object source,
Errors errors,
boolean jitBinding)
Creates a binding for an injectable type with the given scope.
|
<T> java.util.List<Binding<T>> |
findBindingsByType(TypeLiteral<T> type)
Returns all explicit bindings for
type . |
java.util.Map<Key<?>,Binding<?>> |
getAllBindings()
Returns a snapshot of this injector's bindings, both explicit and
just-in-time.
|
<T> Binding<T> |
getBinding(java.lang.Class<T> type)
Returns the binding for the given type.
|
<T> BindingImpl<T> |
getBinding(Key<T> key)
Returns the binding for
key |
(package private) <T> BindingImpl<T> |
getBindingOrThrow(Key<T> key,
Errors errors,
InjectorImpl.JitLimitation jitType)
Gets a binding implementation.
|
java.util.Map<Key<?>,Binding<?>> |
getBindings()
Returns this injector's explicit bindings.
|
<T> BindingImpl<T> |
getExistingBinding(Key<T> key)
Returns the binding if it already exists, or null if does not exist.
|
<T> T |
getInstance(java.lang.Class<T> type)
Returns the appropriate instance for the given injection type; equivalent to
getProvider(type).get() . |
<T> T |
getInstance(Key<T> key)
Returns the appropriate instance for the given injection key; equivalent to
getProvider(key).get() . |
private java.util.Set<Dependency<?>> |
getInternalDependencies(BindingImpl<?> binding)
Safely gets the dependencies of possibly not initialized bindings.
|
(package private) <T> InternalFactory<? extends T> |
getInternalFactory(Key<T> key,
Errors errors,
InjectorImpl.JitLimitation jitType) |
private <T> BindingImpl<T> |
getJustInTimeBinding(Key<T> key,
Errors errors,
InjectorImpl.JitLimitation jitType)
Returns a just-in-time binding for
key , creating it if necessary. |
(package private) InternalContext |
getLocalContext()
Only to be called by the
SingletonScope provider. |
<T> MembersInjector<T> |
getMembersInjector(java.lang.Class<T> type)
Returns the members injector used to inject dependencies into methods and fields on instances
of the given type
T . |
<T> MembersInjector<T> |
getMembersInjector(TypeLiteral<T> typeLiteral)
Returns the members injector used to inject dependencies into methods and fields on instances
of the given type
T . |
(package private) SingleParameterInjector<?>[] |
getParametersInjectors(java.util.List<Dependency<?>> parameters,
Errors errors)
Returns parameter injectors, or
null if there are no parameters. |
Injector |
getParent()
Returns this injector's parent, or
null if this is a top-level injector. |
private static <T> Key<T> |
getProvidedKey(Key<Provider<T>> key,
Errors errors) |
<T> Provider<T> |
getProvider(java.lang.Class<T> type)
Returns the provider used to obtain instances for the given type.
|
<T> Provider<T> |
getProvider(Key<T> key)
Returns the provider used to obtain instances for the given injection key.
|
(package private) <T> Provider<T> |
getProviderOrThrow(Dependency<T> dependency,
Errors errors) |
java.util.Map<java.lang.Class<? extends java.lang.annotation.Annotation>,Scope> |
getScopeBindings()
Returns a map containing all scopes in the injector.
|
java.util.Set<TypeConverterBinding> |
getTypeConverterBindings()
Returns a set containing all type converter bindings in the injector.
|
(package private) void |
index()
Indexes bindings by type.
|
(package private) <T> void |
index(Binding<T> binding) |
(package private) <T> void |
initializeBinding(BindingImpl<T> binding,
Errors errors) |
(package private) <T> void |
initializeJitBinding(BindingImpl<T> binding,
Errors errors) |
void |
injectMembers(java.lang.Object instance)
Injects dependencies into the fields and methods of
instance . |
private static boolean |
isMembersInjector(Key<?> key)
Returns true if the key type is MembersInjector (but not a subclass of MembersInjector).
|
private static boolean |
isProvider(Key<?> key)
Returns true if the key type is Provider (but not a subclass of Provider).
|
private static boolean |
isTypeLiteral(Key<?> key) |
private void |
removeFailedJitBinding(Binding<?> binding,
InjectionPoint ip)
Cleans up any state that may have been cached when constructing the JIT binding.
|
java.lang.String |
toString() |
public static final TypeLiteral<java.lang.String> STRING_TYPE
final State state
final InjectorImpl parent
final InjectorImpl.BindingsMultimap bindingsMultimap
final InjectorImpl.InjectorOptions options
final java.util.Map<Key<?>,BindingImpl<?>> jitBindings
final java.util.Set<Key<?>> failedJitBindings
Lookups lookups
final ConstructorInjectorStore constructors
MembersInjectorStore membersInjectorStore
ProvisionListenerCallbackStore provisionListenerStore
private final java.lang.ThreadLocal<java.lang.Object[]> localContext
InjectorImpl(InjectorImpl parent, State state, InjectorImpl.InjectorOptions injectorOptions)
void index()
<T> void index(Binding<T> binding)
public <T> java.util.List<Binding<T>> findBindingsByType(TypeLiteral<T> type)
Injector
type
.
This method is part of the Guice SPI and is intended for use by tools and extensions.
findBindingsByType
in interface Injector
public <T> BindingImpl<T> getBinding(Key<T> key)
key
getBinding
in interface Injector
public <T> BindingImpl<T> getExistingBinding(Key<T> key)
Injector
Injector.getBinding(Key)
, this does not attempt to create just-in-time bindings
for keys that aren't bound.
This method is part of the Guice SPI and is intended for use by tools and extensions.
getExistingBinding
in interface Injector
<T> BindingImpl<T> getBindingOrThrow(Key<T> key, Errors errors, InjectorImpl.JitLimitation jitType) throws ErrorsException
ErrorsException
public <T> Binding<T> getBinding(java.lang.Class<T> type)
Injector
This method is part of the Guice SPI and is intended for use by tools and extensions.
getBinding
in interface Injector
public Injector getParent()
Injector
null
if this is a top-level injector.public Injector createChildInjector(java.lang.Iterable<? extends Module> modules)
Injector
Just-in-time bindings created for child injectors will be created in an ancestor injector whenever possible. This allows for scoped instances to be shared between injectors. Use explicit bindings to prevent bindings from being shared with the parent injector. Optional injections in just-in-time bindings (created in the parent injector) may be silently ignored if the optional dependencies are from the child injector.
No key may be bound by both an injector and one of its ancestors. This includes just-in-time
bindings. The lone exception is the key for Injector.class
, which is bound by each
injector to itself.
createChildInjector
in interface Injector
public Injector createChildInjector(Module... modules)
Injector
Just-in-time bindings created for child injectors will be created in an ancestor injector whenever possible. This allows for scoped instances to be shared between injectors. Use explicit bindings to prevent bindings from being shared with the parent injector.
No key may be bound by both an injector and one of its ancestors. This includes just-in-time
bindings. The lone exception is the key for Injector.class
, which is bound by each
injector to itself.
createChildInjector
in interface Injector
private <T> BindingImpl<T> getJustInTimeBinding(Key<T> key, Errors errors, InjectorImpl.JitLimitation jitType) throws ErrorsException
key
, creating it if necessary.ErrorsException
- if the binding could not be created.private static boolean isProvider(Key<?> key)
private static boolean isTypeLiteral(Key<?> key)
private static <T> Key<T> getProvidedKey(Key<Provider<T>> key, Errors errors) throws ErrorsException
ErrorsException
private static boolean isMembersInjector(Key<?> key)
private <T> BindingImpl<MembersInjector<T>> createMembersInjectorBinding(Key<MembersInjector<T>> key, Errors errors) throws ErrorsException
ErrorsException
private <T> BindingImpl<Provider<T>> createProviderBinding(Key<Provider<T>> key, Errors errors) throws ErrorsException
Provider<T>
, i.e. a binding to the provider from
Binding<T>
.ErrorsException
private <T> BindingImpl<T> convertConstantStringBinding(Key<T> key, Errors errors) throws ErrorsException
ErrorsException
- if there was an error resolving the binding<T> void initializeBinding(BindingImpl<T> binding, Errors errors) throws ErrorsException
ErrorsException
<T> void initializeJitBinding(BindingImpl<T> binding, Errors errors) throws ErrorsException
ErrorsException
private boolean cleanup(BindingImpl<?> binding, java.util.Set<Key> encountered)
private void removeFailedJitBinding(Binding<?> binding, InjectionPoint ip)
private java.util.Set<Dependency<?>> getInternalDependencies(BindingImpl<?> binding)
<T> BindingImpl<T> createUninitializedBinding(Key<T> key, Scoping scoping, java.lang.Object source, Errors errors, boolean jitBinding) throws ErrorsException
ErrorsException
private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding(Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException
Key<TypeLiteral<T>>
to the value TypeLiteral<T>
. It's
a bit awkward because we have to pull out the inner type in the type literal.ErrorsException
<T> BindingImpl<T> createProvidedByBinding(Key<T> key, Scoping scoping, ProvidedBy providedBy, Errors errors) throws ErrorsException
ErrorsException
private <T> BindingImpl<T> createImplementedByBinding(Key<T> key, Scoping scoping, ImplementedBy implementedBy, Errors errors) throws ErrorsException
ErrorsException
private <T> BindingImpl<T> createJustInTimeBindingRecursive(Key<T> key, Errors errors, boolean jitDisabled, InjectorImpl.JitLimitation jitType) throws ErrorsException
key
in the root injector, falling back to
other ancestor injectors until this injector is tried.ErrorsException
private <T> BindingImpl<T> createJustInTimeBinding(Key<T> key, Errors errors, boolean jitDisabled, InjectorImpl.JitLimitation jitType) throws ErrorsException
key
. The strategies used to
create just-in-time bindings are:
Provider<T>
, we delegate
to the binding for T
.
ErrorsException
- if the binding cannot be created.<T> InternalFactory<? extends T> getInternalFactory(Key<T> key, Errors errors, InjectorImpl.JitLimitation jitType) throws ErrorsException
ErrorsException
public java.util.Map<Key<?>,Binding<?>> getBindings()
Injector
The returned map does not include bindings inherited from a parent
injector
, should one exist. The returned map is guaranteed to iterate (for example, with
its Map.entrySet()
iterator) in the order of insertion. In other words, the order in
which bindings appear in user Modules.
This method is part of the Guice SPI and is intended for use by tools and extensions.
getBindings
in interface Injector
public java.util.Map<Key<?>,Binding<?>> getAllBindings()
Injector
getAllBindings()
was invoked. Subsequent calls may return a map with
additional just-in-time bindings.
The returned map does not include bindings inherited from a parent
injector
, should one exist.
This method is part of the Guice SPI and is intended for use by tools and extensions.
getAllBindings
in interface Injector
public java.util.Map<java.lang.Class<? extends java.lang.annotation.Annotation>,Scope> getScopeBindings()
Injector
Singleton.class
, and the values are scope instances, such as Scopes.SINGLETON
. The returned map is immutable.
This method is part of the Guice SPI and is intended for use by tools and extensions.
getScopeBindings
in interface Injector
public java.util.Set<TypeConverterBinding> getTypeConverterBindings()
Injector
This method is part of the Guice SPI and is intended for use by tools and extensions.
getTypeConverterBindings
in interface Injector
SingleParameterInjector<?>[] getParametersInjectors(java.util.List<Dependency<?>> parameters, Errors errors) throws ErrorsException
null
if there are no parameters.ErrorsException
<T> SingleParameterInjector<T> createParameterInjector(Dependency<T> dependency, Errors errors) throws ErrorsException
ErrorsException
public void injectMembers(java.lang.Object instance)
Injector
instance
. Ignores the presence or
absence of an injectable constructor.
Whenever Guice creates an instance, it performs this injection automatically (after first performing constructor injection), so if you're able to let Guice create all your objects for you, you'll never need to use this method.
injectMembers
in interface Injector
instance
- to inject members onfor a preferred alternative that supports checks before
run time
public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral)
Injector
T
.getMembersInjector
in interface Injector
getMembersInjector
in interface Lookups
typeLiteral
- type to get members injector forfor an alternative that offers up front error
detection
public <T> MembersInjector<T> getMembersInjector(java.lang.Class<T> type)
Injector
T
. When feasible, use Binder.getMembersInjector(TypeLiteral)
instead to get increased up front error detection.getMembersInjector
in interface Injector
type
- type to get members injector forfor an alternative that offers up front error
detection
public <T> Provider<T> getProvider(java.lang.Class<T> type)
Injector
getProvider
in interface Injector
for an alternative that offers up front error detection
<T> Provider<T> getProviderOrThrow(Dependency<T> dependency, Errors errors) throws ErrorsException
ErrorsException
public <T> Provider<T> getProvider(Key<T> key)
Injector
getProvider
in interface Injector
getProvider
in interface Lookups
for an alternative that offers up front error detection
public <T> T getInstance(Key<T> key)
Injector
getProvider(key).get()
. When feasible, avoid using this method, in favor of having Guice
inject your dependencies ahead of time.getInstance
in interface Injector
public <T> T getInstance(java.lang.Class<T> type)
Injector
getProvider(type).get()
. When feasible, avoid using this method, in favor of having Guice
inject your dependencies ahead of time.getInstance
in interface Injector
InternalContext getLocalContext()
SingletonScope
provider.<T> T callInContext(ContextualCallable<T> callable) throws ErrorsException
ErrorsException
public java.lang.String toString()
toString
in class java.lang.Object