Class Elements.RecordingBinder
- All Implemented Interfaces:
Binder
,PrivateBinder
- Enclosing class:
- Elements
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ModuleAnnotatedMethodScanner
private final Map<Module,
Elements.ModuleInfo> private ModuleSource
The current modules stackprivate final Elements.RecordingBinder
The binder where exposed bindings will be createdprivate final BindingSourceRestriction.PermitMapConstruction
private final List<Elements.RecordingBinder>
All children private binders, so we can scan through them.private final PrivateElementsImpl
private final Set<ModuleAnnotatedMethodScanner>
private ModuleAnnotatedMethodScanner
The current scanner.private final Object
private final SourceProvider
private final Stage
private boolean
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
RecordingBinder
(Elements.RecordingBinder parent, PrivateElementsImpl privateElements) Creates a private recording binder.private
RecordingBinder
(Elements.RecordingBinder prototype, Object source, SourceProvider sourceProvider, boolean trustedSource) Creates a recording binder that's backed byprototype
.private
RecordingBinder
(Stage stage) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Records an error message to be presented to the user at a later time.void
Records an error message which will be presented to the user at a later time.void
Records an exception, the full details of which will be logged, and the message of which will be presented to the user at a later time.<T> AnnotatedBindingBuilder<T>
See the EDSL examples atBinder
.<T> AnnotatedBindingBuilder<T>
bind
(TypeLiteral<T> typeLiteral) See the EDSL examples atBinder
.<T> AnnotatedBindingBuilder<T>
See the EDSL examples atBinder
.See the EDSL examples atBinder
.void
bindInterceptor
(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, org.aopalliance.intercept.MethodInterceptor... interceptors) Binds method interceptor[s] to methods matched by class and method matchers.void
bindListener
(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners) Registers listeners for provisioned objects.void
bindListener
(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener) Registers a listener for injectable types.void
bindScope
(Class<? extends Annotation> annotationType, Scope scope) Binds a scope to an annotation.void
convertToTypes
(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) Binds a type converter.Gets the current stage.void
Prevents Guice from injecting dependencies that form a cycle, unless broken by aProvider
.void
Makes the binding forkey
available to the enclosing environmentexpose
(TypeLiteral<?> type) Makes a binding fortype
available to the enclosing environment.Makes a binding fortype
available to the enclosing environment.private <T> AnnotatedElementBuilder
exposeInternal
(Key<T> key) private void
forbidNestedScannerMethods
(Module module) private Iterable<ModuleAnnotatedMethodScanner>
Get all scanners registered in this binder and its ancestors.private ElementSource
<T> MembersInjector<T>
getMembersInjector
(TypeLiteral<T> typeLiteral) Returns the members injector used to inject dependencies into methods and fields on instances of the given typeT
.<T> MembersInjector<T>
getMembersInjector
(Class<T> type) Returns the members injector used to inject dependencies into methods and fields on instances of the given typeT
.private ModuleSource
getModuleSource
(Class<?> module) <T> Provider<T>
getProvider
(Key<T> key) Returns the provider used to obtain instances for the given injection key.<T> Provider<T>
getProvider
(Dependency<T> dependency) Returns the provider used to obtain instances for the given injection key.<T> Provider<T>
getProvider
(Class<T> type) Returns the provider used to obtain instances for the given injection type.void
Uses the given module to configure more bindings.private boolean
Returns if the binder is in the module scanning phase.Creates a new private child environment for bindings and other configuration.<T> void
requestInjection
(TypeLiteral<T> type, T instance) Upon successful creation, theInjector
will inject instance fields and methods of the given object.void
requestInjection
(Object instance) Upon successful creation, theInjector
will inject instance fields and methods of the given object.void
requestStaticInjection
(Class<?>... types) Upon successful creation, theInjector
will inject static fields and methods in the given classes.void
Requires that a @Inject
annotation exists on a constructor in order for Guice to consider it an eligible injectable class.void
Requires that Guice finds an exactly matching binding annotation.void
Instructs the Injector that bindings must be listed in a Module in order to be injected.(package private) void
Applies all scanners to the modules we've installed.void
Adds a scanner that will look in all installed modules for annotations the scanner can parse, and binds them like @Provides methods.skipSources
(Class<?>... classesToSkip) Returns a binder that skipsclassesToSkip
when identify the calling code.toString()
withSource
(Object source) Returns a binder that usessource
as the reference location for configuration errors.withTrustedSource
(Object source)
-
Field Details
-
stage
-
modules
-
elements
-
source
-
sourceProvider
-
scanners
-
parent
The binder where exposed bindings will be created -
privateElements
-
privateBindersForScanning
All children private binders, so we can scan through them. -
permitMapConstruction
-
moduleSource
The current modules stack -
scannerSource
The current scanner.Note that scanners cannot nest, ie. a scanner cannot install a module that requires scanning - except the built-in @Provides* methods. The built-in scanner isn't tracked by this variable, only custom scanners are.
-
currentScanner
-
trustedSource
private boolean trustedSource
-
-
Constructor Details
-
RecordingBinder
-
RecordingBinder
private RecordingBinder(Elements.RecordingBinder prototype, Object source, SourceProvider sourceProvider, boolean trustedSource) Creates a recording binder that's backed byprototype
. -
RecordingBinder
Creates a private recording binder.
-
-
Method Details
-
bindInterceptor
public void bindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, org.aopalliance.intercept.MethodInterceptor... interceptors) Description copied from interface:Binder
Binds method interceptor[s] to methods matched by class and method matchers. A method is eligible for interception if:- Guice created the instance the method is on
- Neither the enclosing type nor the method is final
- And the method is package-private, protected, or public
Note: this API only works if
guice_bytecode_gen_option
is set toENABLED
.- Specified by:
bindInterceptor
in interfaceBinder
- Parameters:
classMatcher
- matches classes the interceptor should apply to. For example:only(Runnable.class)
.methodMatcher
- matches methods the interceptor should apply to. For example:annotatedWith(Transactional.class)
.interceptors
- to bind. The interceptors are called in the order they are given.
-
bindScope
Description copied from interface:Binder
Binds a scope to an annotation. -
requestInjection
Description copied from interface:Binder
Upon successful creation, theInjector
will inject instance fields and methods of the given object.- Specified by:
requestInjection
in interfaceBinder
- Parameters:
instance
- for which members will be injected
-
requestInjection
Description copied from interface:Binder
Upon successful creation, theInjector
will inject instance fields and methods of the given object.- Specified by:
requestInjection
in interfaceBinder
- Parameters:
type
- of instanceinstance
- for which members will be injected
-
getMembersInjector
Description copied from interface:Binder
Returns the members injector used to inject dependencies into methods and fields on instances of the given typeT
. The returned members injector will not be valid until the mainInjector
has been created. The members injector will throw anIllegalStateException
if you try to use it beforehand.- Specified by:
getMembersInjector
in interfaceBinder
- Parameters:
typeLiteral
- type to get members injector for
-
getMembersInjector
Description copied from interface:Binder
Returns the members injector used to inject dependencies into methods and fields on instances of the given typeT
. The returned members injector will not be valid until the mainInjector
has been created. The members injector will throw anIllegalStateException
if you try to use it beforehand.- Specified by:
getMembersInjector
in interfaceBinder
- Parameters:
type
- type to get members injector for
-
bindListener
Description copied from interface:Binder
Registers a listener for injectable types. Guice will notify the listener when it encounters injectable types matched by the given type matcher.- Specified by:
bindListener
in interfaceBinder
- Parameters:
typeMatcher
- that matches injectable types the listener should be notified oflistener
- for injectable types matched by typeMatcher
-
bindListener
public void bindListener(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners) Description copied from interface:Binder
Registers listeners for provisioned objects. Guice will notify the listeners just before and after the object is provisioned. Provisioned objects that are also injectable (everything except objects provided through Providers) can also be notified through TypeListeners registered inBinder.bindListener(com.google.inject.matcher.Matcher<? super com.google.inject.TypeLiteral<?>>, com.google.inject.spi.TypeListener)
.- Specified by:
bindListener
in interfaceBinder
- Parameters:
bindingMatcher
- that matches bindings of provisioned objects the listener should be notified oflisteners
- for provisioned objects matched by bindingMatcher
-
requestStaticInjection
Description copied from interface:Binder
Upon successful creation, theInjector
will inject static fields and methods in the given classes.- Specified by:
requestStaticInjection
in interfaceBinder
- Parameters:
types
- for which static members will be injected
-
scanForAnnotatedMethods
void scanForAnnotatedMethods()Applies all scanners to the modules we've installed. We skip certain PrivateModules because store them in more than one Modules map and only want to process them through one of the maps. (They're stored in both maps to prevent a module from being installed more than once.) -
install
Description copied from interface:Binder
Uses the given module to configure more bindings. -
forbidNestedScannerMethods
-
getAllScanners
Get all scanners registered in this binder and its ancestors.Should only be called during module scanning, because at that point registering new scanners is forbidden.
-
currentStage
Description copied from interface:Binder
Gets the current stage.- Specified by:
currentStage
in interfaceBinder
-
addError
Description copied from interface:Binder
Records an error message which will be presented to the user at a later time. Unlike throwing an exception, this enable us to continue configuring the Injector and discover more errors. UsesString.format(String, Object[])
to insert the arguments into the message. -
addError
Description copied from interface:Binder
Records an exception, the full details of which will be logged, and the message of which will be presented to the user at a later time. If your Module calls something that you worry may fail, you should catch the exception and pass it into this. -
addError
Description copied from interface:Binder
Records an error message to be presented to the user at a later time. -
bind
Description copied from interface:Binder
See the EDSL examples atBinder
. -
bind
Description copied from interface:Binder
See the EDSL examples atBinder
. -
bind
Description copied from interface:Binder
See the EDSL examples atBinder
. -
bindConstant
Description copied from interface:Binder
See the EDSL examples atBinder
.- Specified by:
bindConstant
in interfaceBinder
-
getProvider
Description copied from interface:Binder
Returns the provider used to obtain instances for the given injection key. The returned provider will not be valid until theInjector
has been created. The provider will throw anIllegalStateException
if you try to use it beforehand.- Specified by:
getProvider
in interfaceBinder
-
getProvider
Description copied from interface:Binder
Returns the provider used to obtain instances for the given injection key. The returned provider will be attached to the injection point and will follow the nullability specified in the dependency. Additionally, the returned provider will not be valid until theInjector
has been created. The provider will throw anIllegalStateException
if you try to use it beforehand.- Specified by:
getProvider
in interfaceBinder
-
getProvider
Description copied from interface:Binder
Returns the provider used to obtain instances for the given injection type. The returned provider will not be valid until theInjector
has been created. The provider will throw anIllegalStateException
if you try to use it beforehand.- Specified by:
getProvider
in interfaceBinder
-
convertToTypes
Description copied from interface:Binder
Binds a type converter. The injector will use the given converter to convert string constants to matching types as needed.- Specified by:
convertToTypes
in interfaceBinder
- Parameters:
typeMatcher
- matches types the converter can handleconverter
- converts values
-
withSource
Description copied from interface:Binder
Returns a binder that usessource
as the reference location for configuration errors. This is typically aStackTraceElement
for.java
source but it could any binding source, such as the path to a.properties
file.- Specified by:
withSource
in interfaceBinder
- Specified by:
withSource
in interfacePrivateBinder
- Parameters:
source
- any object representing the source location and has a concisetoString()
value- Returns:
- a binder that shares its configuration with this binder
-
withTrustedSource
-
skipSources
Description copied from interface:Binder
Returns a binder that skipsclassesToSkip
when identify the calling code. The caller'sStackTraceElement
is used to locate the source of configuration errors.- Specified by:
skipSources
in interfaceBinder
- Specified by:
skipSources
in interfacePrivateBinder
- Parameters:
classesToSkip
- library classes that create bindings on behalf of their clients.- Returns:
- a binder that shares its configuration with this binder.
-
newPrivateBinder
Description copied from interface:Binder
Creates a new private child environment for bindings and other configuration. The returned binder can be used to add and configuration information in this environment. SeePrivateModule
for details.- Specified by:
newPrivateBinder
in interfaceBinder
- Returns:
- a binder that inherits configuration from this binder. Only exposed configuration on the returned binder will be visible to this binder.
-
disableCircularProxies
public void disableCircularProxies()Description copied from interface:Binder
Prevents Guice from injecting dependencies that form a cycle, unless broken by aProvider
. By default, circular dependencies are not disabled.If a parent injector disables circular dependencies, then all child injectors (and private modules within that injector) also disable circular dependencies. If a parent does not disable circular dependencies, a child injector or private module may optionally declare itself as disabling circular dependencies. If it does, the behavior is limited only to that child or any grandchildren. No siblings of the child will disable circular dependencies.
- Specified by:
disableCircularProxies
in interfaceBinder
-
requireExplicitBindings
public void requireExplicitBindings()Description copied from interface:Binder
Instructs the Injector that bindings must be listed in a Module in order to be injected. Classes that are not explicitly bound in a module cannot be injected. Bindings created through a linked binding (bind(Foo.class).to(FooImpl.class)
) are allowed, but the implicit binding (FooImpl
) cannot be directly injected unless it is also explicitly bound (bind(FooImpl.class)
).Tools can still retrieve bindings for implicit bindings (bindings created through a linked binding) if explicit bindings are required, however
Binding.getProvider()
will fail.By default, explicit bindings are not required.
If a parent injector requires explicit bindings, then all child injectors (and private modules within that injector) also require explicit bindings. If a parent does not require explicit bindings, a child injector or private module may optionally declare itself as requiring explicit bindings. If it does, the behavior is limited only to that child or any grandchildren. No siblings of the child will require explicit bindings.
In the absence of an explicit binding for the target, linked bindings in child injectors create a binding for the target in the parent. Since this behavior can be surprising, it causes an error instead if explicit bindings are required. To avoid this error, add an explicit binding for the target, either in the child or the parent.
- Specified by:
requireExplicitBindings
in interfaceBinder
-
requireAtInjectOnConstructors
public void requireAtInjectOnConstructors()Description copied from interface:Binder
Requires that a @Inject
annotation exists on a constructor in order for Guice to consider it an eligible injectable class. By default, Guice will inject classes that have a no-args constructor if no @Inject
annotation exists on any constructor.If the class is bound using
LinkedBindingBuilder.toConstructor(java.lang.reflect.Constructor<S>)
, Guice will still inject that constructor regardless of annotations.- Specified by:
requireAtInjectOnConstructors
in interfaceBinder
-
requireExactBindingAnnotations
public void requireExactBindingAnnotations()Description copied from interface:Binder
Requires that Guice finds an exactly matching binding annotation. This disables the error-prone feature in Guice where it can substitute a binding for@Named Foo
when attempting to inject@Named("foo") Foo
.- Specified by:
requireExactBindingAnnotations
in interfaceBinder
-
scanModulesForAnnotatedMethods
Description copied from interface:Binder
Adds a scanner that will look in all installed modules for annotations the scanner can parse, and binds them like @Provides methods. Scanners apply to all modules installed in the injector. Scanners installed in child injectors or private modules do not impact modules in siblings or parents, however scanners installed in parents do apply to all child injectors and private modules.- Specified by:
scanModulesForAnnotatedMethods
in interfaceBinder
-
expose
Description copied from interface:PrivateBinder
Makes the binding forkey
available to the enclosing environment- Specified by:
expose
in interfacePrivateBinder
-
expose
Description copied from interface:PrivateBinder
Makes a binding fortype
available to the enclosing environment. UseannotatedWith()
to exposetype
with a binding annotation.- Specified by:
expose
in interfacePrivateBinder
-
expose
Description copied from interface:PrivateBinder
Makes a binding fortype
available to the enclosing environment. UseannotatedWith()
to exposetype
with a binding annotation.- Specified by:
expose
in interfacePrivateBinder
-
exposeInternal
-
getModuleSource
-
getElementSource
-
moduleScanning
private boolean moduleScanning()Returns if the binder is in the module scanning phase. -
toString
-