Top Description Inners Fields Constructors Methods
com.google.inject.internal

public final Class InternalInjectorCreator

extends Object
Class Inheritance
Imports
com.google.common.base.Stopwatch, com.google.inject.Binding, .Injector, .Key, .MembersInjector, .Module, .Provider, .Scope, .Stage, .TypeLiteral, com.google.inject.internal.util.ContinuousStopwatch, com.google.inject.spi.Dependency, .Element, .InjectionPoint, .TypeConverterBinding, java.lang.annotation.Annotation, java.util.ArrayList, .Collection, .List, .Map, .Set

Builds a tree of injectors. This is a primary injector, plus child injectors needed for each Binder.newPrivateBinder() private environment. The primary injector is not necessarily a top-level injector.

Injector construction happens in two phases.

  1. Static building. In this phase, we interpret commands, create bindings, and inspect dependencies. During this phase, we hold a lock to ensure consistency with parent injectors. No user code is executed in this phase.
  2. Dynamic injection. In this phase, we call user code. We inject members that requested injection. This may require user's objects be created and their providers be called. And we create eager singletons. In this phase, user code may have started other threads. This phase is not executed for injectors created using the tool stage
Authors
[email protected] (Bob Lee), [email protected] (Jesse Wilson)

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
private final Errors
private final Initializer
private final InjectionRequestProcessor
private final ProcessedBindingData
private final InjectorShell.Builder
private List<InjectorShell>
private final ContinuousStopwatch

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public InternalInjectorCreator
addModules(Iterable<? extends Module> modules)

public Injector
private void
initializeStatically()

Initialize and validate everything.

private void
injectDynamically()

Inject everything that can be injected.

private boolean
isEagerSingleton(InjectorImpl injector, BindingImpl<?> binding, Stage stage)

pack-priv void
loadEagerSingletons(InjectorImpl injector, Stage stage, final Errors errors)

Loads eager singletons, or all singletons if we're in Stage.PRODUCTION.

public InternalInjectorCreator
parentInjector(InjectorImpl parent)

Sets the parent of the injector to-be-constructed.

private Injector
primaryInjector()

Returns the injector being constructed.

public InternalInjectorCreator
stage(Stage stage)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

errorsback to summary
private final Errors errors
initializerback to summary
private final Initializer initializer
injectionRequestProcessorback to summary
private final InjectionRequestProcessor injectionRequestProcessor
processedBindingDataback to summary
private final ProcessedBindingData processedBindingData
shellBuilderback to summary
private final InjectorShell.Builder shellBuilder
shellsback to summary
private List<InjectorShell> shells
stopwatchback to summary
private final ContinuousStopwatch stopwatch

Constructor Detail

InternalInjectorCreatorback to summary
public InternalInjectorCreator()

Method Detail

addModulesback to summary
public InternalInjectorCreator addModules(Iterable<? extends Module> modules)
buildback to summary
public Injector build()
initializeStaticallyback to summary
private void initializeStatically()

Initialize and validate everything.

injectDynamicallyback to summary
private void injectDynamically()

Inject everything that can be injected. This method is intentionally not synchronized. If we locked while injecting members (ie. running user code), things would deadlock should the user code build a just-in-time binding from another thread.

isEagerSingletonback to summary
private boolean isEagerSingleton(InjectorImpl injector, BindingImpl<?> binding, Stage stage)
loadEagerSingletonsback to summary
pack-priv void loadEagerSingletons(InjectorImpl injector, Stage stage, final Errors errors)

Loads eager singletons, or all singletons if we're in Stage.PRODUCTION. Bindings discovered while we're binding these singletons are not be eager.

parentInjectorback to summary
public InternalInjectorCreator parentInjector(InjectorImpl parent)

Sets the parent of the injector to-be-constructed. As a side effect, this sets this injector's stage to the stage of parent and sets requireExplicitBindings() if the parent injector also required them.

primaryInjectorback to summary
private Injector primaryInjector()

Returns the injector being constructed. This is not necessarily the root injector.

stageback to summary
public InternalInjectorCreator stage(Stage stage)
com.google.inject.internal back to summary

pack-priv Class InternalInjectorCreator.ToolStageInjector

extends Object
implements Injector
Class Inheritance
  • java.lang.Object
  • com.google.inject.internal.InternalInjectorCreator.ToolStageInjector
All Implemented Interfaces
com.google.inject.Injector

Injector exposed to users in Stage#TOOL.

Field Summary

Modifier and TypeField and Description
private final Injector

Constructor Summary

AccessConstructor and Description
pack-priv
ToolStageInjector(Injector delegateInjector)

Method Summary

Modifier and TypeMethod and Description
public Injector
createChildInjector(Iterable<? extends Module> modules)

Implements com.google.inject.Injector.createChildInjector.

Returns a new injector that inherits all state from this injector.
public Injector
createChildInjector(Module... modules)

Implements com.google.inject.Injector.createChildInjector.

Returns a new injector that inherits all state from this injector.
public <T> List<Binding<T>>
findBindingsByType(TypeLiteral<T> type)

Implements com.google.inject.Injector.findBindingsByType.

Returns all explicit bindings for type.
public Map<Key<?>, Binding<?>>
getAllBindings()

Implements com.google.inject.Injector.getAllBindings.

Returns a snapshot of this injector's bindings, both explicit and just-in-time.
public Map<TypeLiteral<?>, List<InjectionPoint>>
getAllMembersInjectorInjectionPoints()

Implements com.google.inject.Injector.getAllMembersInjectorInjectionPoints.

Returns the injection points created for calls to getMembersInjector (either directly or indirectly, e.g. through injectMembers.
public <T> Binding<T>
getBinding(Key<T> key)

Implements com.google.inject.Injector.getBinding.

Returns the binding for the given injection key.
public <T> Binding<T>
getBinding(Class<T> type)

Implements com.google.inject.Injector.getBinding.

Returns the binding for the given type.
public Map<Key<?>, Binding<?>>
getBindings()

Implements com.google.inject.Injector.getBindings.

Returns this injector's explicit bindings.
public List<Element>
getElements()

Implements com.google.inject.Injector.getElements.

Returns the elements that make up this injector.
public <T> Binding<T>
getExistingBinding(Key<T> key)

Implements com.google.inject.Injector.getExistingBinding.

Returns the binding if it already exists, or null if does not exist.
public <T> T
getInstance(Key<T> key)

Implements com.google.inject.Injector.getInstance.

Returns the appropriate instance for the given injection key; equivalent to getProvider(key).get().
public <T> T
getInstance(Class<T> type)

Implements com.google.inject.Injector.getInstance.

Returns the appropriate instance for the given injection type; equivalent to getProvider(type).get().
public <T> MembersInjector<T>
getMembersInjector(TypeLiteral<T>
type to get members injector for
typeLiteral
)

Implements com.google.inject.Injector.getMembersInjector.

Returns the members injector used to inject dependencies into methods and fields on instances of the given type T.
public <T> MembersInjector<T>
getMembersInjector(Class<T>
type to get members injector for
type
)

Implements com.google.inject.Injector.getMembersInjector.

Returns the members injector used to inject dependencies into methods and fields on instances of the given type T.
public Injector
getParent()

Implements com.google.inject.Injector.getParent.

Returns this injector's parent, or null if this is a top-level injector.
public <T> Provider<T>
getProvider(Key<T> key)

Implements com.google.inject.Injector.getProvider.

Returns the provider used to obtain instances for the given injection key.
public <T> Provider<T>
getProvider(Class<T> type)

Implements com.google.inject.Injector.getProvider.

Returns the provider used to obtain instances for the given type.
public Map<Class<? extends Annotation>, Scope>
getScopeBindings()

Implements com.google.inject.Injector.getScopeBindings.

Returns a map containing all scopes in the injector.
public Set<TypeConverterBinding>
getTypeConverterBindings()

Implements com.google.inject.Injector.getTypeConverterBindings.

Returns a set containing all type converter bindings in the injector.
public void
injectMembers(Object
to inject members on
o
)

Implements com.google.inject.Injector.injectMembers.

Injects dependencies into the fields and methods of instance.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

delegateInjectorback to summary
private final Injector delegateInjector

Constructor Detail

ToolStageInjectorback to summary
pack-priv ToolStageInjector(Injector delegateInjector)

Method Detail

createChildInjectorback to summary
public Injector createChildInjector(Iterable<? extends Module> modules)

Implements com.google.inject.Injector.createChildInjector.

Doc from com.google.inject.Injector.createChildInjector.

Returns a new injector that inherits all state from this injector. All bindings, scopes, interceptors and type converters are inherited -- they are visible to the child injector. Elements of the child injector are not visible to its parent.

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.

When using hierarchical injectors (via Binder#newPrivateBinder, Binder#PrivateModule, or Injector#createChildInjector), extra care is required to ensure that bindings are associated with the correct injector. Statements that are otherwise unnecessary - such as {code bind(ServiceImpl.class);} - become critical to associate the binding with this particular injector, otherwise Guice may promote the binding to an ancestor injector. Linked bindings such as bind(Service.class).to(ServiceImpl.class); will only associate the Service binding with the current injector; ServiceImpl will still be promoted to an ancestor injector without an explicit bind(ServiceImpl.class); statement.

Annotations
@Override
createChildInjectorback to summary
public Injector createChildInjector(Module... modules)

Implements com.google.inject.Injector.createChildInjector.

Doc from com.google.inject.Injector.createChildInjector.

Returns a new injector that inherits all state from this injector. All bindings, scopes, interceptors and type converters are inherited -- they are visible to the child injector. Elements of the child injector are not visible to its parent.

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.

Annotations
@Override
findBindingsByTypeback to summary
public <T> List<Binding<T>> findBindingsByType(TypeLiteral<T> type)

Implements com.google.inject.Injector.findBindingsByType.

Doc from com.google.inject.Injector.findBindingsByType.

Returns all explicit bindings for type.

This method is part of the Guice SPI and is intended for use by tools and extensions.

Annotations
@Override
getAllBindingsback to summary
public Map<Key<?>, Binding<?>> getAllBindings()

Implements com.google.inject.Injector.getAllBindings.

Doc from com.google.inject.Injector.getAllBindings.

Returns a snapshot of this injector's bindings, both explicit and just-in-time. The returned map is immutable; it contains only the bindings that were present when getAllBindings() was invoked. Just-in-time bindings are only present if they have been requested at least once. 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.

Annotations
@Override
getAllMembersInjectorInjectionPointsback to summary
public Map<TypeLiteral<?>, List<InjectionPoint>> getAllMembersInjectorInjectionPoints()

Implements com.google.inject.Injector.getAllMembersInjectorInjectionPoints.

Doc from com.google.inject.Injector.getAllMembersInjectorInjectionPoints.

Returns the injection points created for calls to getMembersInjector (either directly or indirectly, e.g. through injectMembers.

This excludes any injection points from elements (which are accessible from each element via the SPI), unless getMembersInjector or injectMembers were also called for the same key.

The returned multimap does not include data 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.

Annotations
@Override
getBindingback to summary
public <T> Binding<T> getBinding(Key<T> key)

Implements com.google.inject.Injector.getBinding.

Doc from com.google.inject.Injector.getBinding.

Returns the binding for the given injection key. This will be an explicit bindings if the key was bound explicitly by a module, or an implicit binding otherwise. The implicit binding will be created if necessary.

This method is part of the Guice SPI and is intended for use by tools and extensions.

Annotations
@Override
getBindingback to summary
public <T> Binding<T> getBinding(Class<T> type)

Implements com.google.inject.Injector.getBinding.

Doc from com.google.inject.Injector.getBinding.

Returns the binding for the given type. This will be an explicit bindings if the injection key was bound explicitly by a module, or an implicit binding otherwise. The implicit binding will be created if necessary.

This method is part of the Guice SPI and is intended for use by tools and extensions.

Annotations
@Override
getBindingsback to summary
public Map<Key<?>, Binding<?>> getBindings()

Implements com.google.inject.Injector.getBindings.

Doc from com.google.inject.Injector.getBindings.

Returns this injector's explicit bindings.

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.

Annotations
@Override
getElementsback to summary
public List<Element> getElements()

Implements com.google.inject.Injector.getElements.

Doc from com.google.inject.Injector.getElements.

Returns the elements that make up this injector. Note that not all kinds of elements are returned.

The returned list does not include elements inherited from a parent injector, should one exist.

The returned list is immutable; it contains only the elements that were present when getElements was invoked. Subsequent calls may return a list with additional elements.

The returned list does not include data 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.

Annotations
@Override
getExistingBindingback to summary
public <T> Binding<T> getExistingBinding(Key<T> key)

Implements com.google.inject.Injector.getExistingBinding.

Doc from com.google.inject.Injector.getExistingBinding.

Returns the binding if it already exists, or null if does not exist. Unlike 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.

Annotations
@Override
getInstanceback to summary
public <T> T getInstance(Key<T> key)

Implements com.google.inject.Injector.getInstance.

Doc from com.google.inject.Injector.getInstance.

Returns the appropriate instance for the given injection key; equivalent to getProvider(key).get(). When feasible, avoid using this method, in favor of having Guice inject your dependencies ahead of time.

Annotations
@Override
getInstanceback to summary
public <T> T getInstance(Class<T> type)

Implements com.google.inject.Injector.getInstance.

Doc from com.google.inject.Injector.getInstance.

Returns the appropriate instance for the given injection type; equivalent to getProvider(type).get(). When feasible, avoid using this method, in favor of having Guice inject your dependencies ahead of time.

Annotations
@Override
getMembersInjectorback to summary
public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral)

Implements com.google.inject.Injector.getMembersInjector.

Doc from com.google.inject.Injector.getMembersInjector.

Returns the members injector used to inject dependencies into methods and fields on instances of the given type T.

Parameters
typeLiteral:TypeLiteral<T>

type to get members injector for

Annotations
@Override
getMembersInjectorback to summary
public <T> MembersInjector<T> getMembersInjector(Class<T> type)

Implements com.google.inject.Injector.getMembersInjector.

Doc from com.google.inject.Injector.getMembersInjector.

Returns the members injector used to inject dependencies into methods and fields on instances of the given type T. When feasible, use Binder#getMembersInjector(TypeLiteral) instead to get increased up front error detection.

Parameters
type:Class<T>

type to get members injector for

Annotations
@Override
getParentback to summary
public Injector getParent()

Implements com.google.inject.Injector.getParent.

Doc from com.google.inject.Injector.getParent.

Returns this injector's parent, or null if this is a top-level injector.

Annotations
@Override
getProviderback to summary
public <T> Provider<T> getProvider(Key<T> key)

Implements com.google.inject.Injector.getProvider.

Doc from com.google.inject.Injector.getProvider.

Returns the provider used to obtain instances for the given injection key. When feasible, avoid using this method, in favor of having Guice inject your dependencies ahead of time.

Annotations
@Override
getProviderback to summary
public <T> Provider<T> getProvider(Class<T> type)

Implements com.google.inject.Injector.getProvider.

Doc from com.google.inject.Injector.getProvider.

Returns the provider used to obtain instances for the given type. When feasible, avoid using this method, in favor of having Guice inject your dependencies ahead of time.

Annotations
@Override
getScopeBindingsback to summary
public Map<Class<? extends Annotation>, Scope> getScopeBindings()

Implements com.google.inject.Injector.getScopeBindings.

Doc from com.google.inject.Injector.getScopeBindings.

Returns a map containing all scopes in the injector. The maps keys are scoping annotations like 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.

Annotations
@Override
getTypeConverterBindingsback to summary
public Set<TypeConverterBinding> getTypeConverterBindings()

Implements com.google.inject.Injector.getTypeConverterBindings.

Doc from com.google.inject.Injector.getTypeConverterBindings.

Returns a set containing all type converter bindings in the injector. The returned set is immutable.

This method is part of the Guice SPI and is intended for use by tools and extensions.

Annotations
@Override
injectMembersback to summary
public void injectMembers(Object o)

Implements com.google.inject.Injector.injectMembers.

Doc from com.google.inject.Injector.injectMembers.

Injects dependencies into the fields and methods of 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.

Parameters
o:Object

to inject members on

Annotations
@Override