Class Elements.RecordingBinder
- java.lang.Object
-
- com.google.inject.spi.Elements.RecordingBinder
-
- All Implemented Interfaces:
Binder,PrivateBinder
- Enclosing class:
- Elements
private static class Elements.RecordingBinder extends java.lang.Object implements Binder, PrivateBinder
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<Element>elementsprivate java.util.Map<Module,Elements.ModuleInfo>modulesprivate ModuleSourcemoduleSourceThe current modules stackprivate Elements.RecordingBinderparentThe binder where exposed bindings will be createdprivate java.util.List<Elements.RecordingBinder>privateBindersAll children private binders, so we can scan through them.private PrivateElementsImplprivateElementsprivate java.util.Set<ModuleAnnotatedMethodScanner>scannersprivate java.lang.Objectsourceprivate SourceProvidersourceProviderprivate Stagestage
-
Constructor Summary
Constructors Modifier Constructor Description privateRecordingBinder(Elements.RecordingBinder parent, PrivateElementsImpl privateElements)Creates a private recording binder.privateRecordingBinder(Elements.RecordingBinder prototype, java.lang.Object source, SourceProvider sourceProvider)Creates a recording binder that's backed byprototype.privateRecordingBinder(Stage stage)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddError(Message message)Records an error message to be presented to the user at a later time.voidaddError(java.lang.String message, java.lang.Object... arguments)Records an error message which will be presented to the user at a later time.voidaddError(java.lang.Throwable t)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>bind(Key<T> key)See the EDSL examples atBinder.<T> AnnotatedBindingBuilder<T>bind(TypeLiteral<T> typeLiteral)See the EDSL examples atBinder.<T> AnnotatedBindingBuilder<T>bind(java.lang.Class<T> type)See the EDSL examples atBinder.AnnotatedConstantBindingBuilderbindConstant()See the EDSL examples atBinder.voidbindInterceptor(Matcher<? super java.lang.Class<?>> classMatcher, Matcher<? super java.lang.reflect.Method> methodMatcher, org.aopalliance.intercept.MethodInterceptor... interceptors)Binds method interceptor[s] to methods matched by class and method matchers.voidbindListener(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners)Registers listeners for provisioned objects.voidbindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener)Registers a listener for injectable types.voidbindScope(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, Scope scope)Binds a scope to an annotation.voidconvertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter)Binds a type converter.StagecurrentStage()Gets the current stage.voiddisableCircularProxies()Prevents Guice from injecting dependencies that form a cycle, unless broken by aProvider.voidexpose(Key<?> key)Makes the binding forkeyavailable to the enclosing environmentAnnotatedElementBuilderexpose(TypeLiteral<?> type)Makes a binding fortypeavailable to the enclosing environment.AnnotatedElementBuilderexpose(java.lang.Class<?> type)Makes a binding fortypeavailable to the enclosing environment.private <T> AnnotatedElementBuilderexposeInternal(Key<T> key)private ElementSourcegetElementSource()<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(java.lang.Class<T> type)Returns the members injector used to inject dependencies into methods and fields on instances of the given typeT.private ModuleSourcegetModuleSource(java.lang.Object module)private java.lang.StackTraceElement[]getPartialCallStack(java.lang.StackTraceElement[] callStack)Removes themoduleSourcecall stack from the beginning of current call stack.<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(java.lang.Class<T> type)Returns the provider used to obtain instances for the given injection type.voidinstall(Module module)Uses the given module to configure more bindings.PrivateBindernewPrivateBinder()Creates a new private child environment for bindings and other configuration.<T> voidrequestInjection(TypeLiteral<T> type, T instance)Upon successful creation, theInjectorwill inject instance fields and methods of the given object.voidrequestInjection(java.lang.Object instance)Upon successful creation, theInjectorwill inject instance fields and methods of the given object.voidrequestStaticInjection(java.lang.Class<?>... types)Upon successful creation, theInjectorwill inject static fields and methods in the given classes.voidrequireAtInjectOnConstructors()Requires that a @Injectannotation exists on a constructor in order for Guice to consider it an eligible injectable class.voidrequireExactBindingAnnotations()Requires that Guice finds an exactly matching binding annotation.voidrequireExplicitBindings()Instructs the Injector that bindings must be listed in a Module in order to be injected.(package private) voidscanForAnnotatedMethods()Applies all scanners to the modules we've installed.voidscanModulesForAnnotatedMethods(ModuleAnnotatedMethodScanner scanner)Adds a scanner that will look in all installed modules for annotations the scanner can parse, and binds them like @Provides methods.Elements.RecordingBinderskipSources(java.lang.Class... classesToSkip)Returns a binder that skipsclassesToSkipwhen identify the calling code.java.lang.StringtoString()Elements.RecordingBinderwithSource(java.lang.Object source)Returns a binder that usessourceas the reference location for configuration errors.
-
-
-
Field Detail
-
stage
private final Stage stage
-
modules
private final java.util.Map<Module,Elements.ModuleInfo> modules
-
elements
private final java.util.List<Element> elements
-
source
private final java.lang.Object source
-
moduleSource
private ModuleSource moduleSource
The current modules stack
-
sourceProvider
private final SourceProvider sourceProvider
-
scanners
private final java.util.Set<ModuleAnnotatedMethodScanner> scanners
-
parent
private final Elements.RecordingBinder parent
The binder where exposed bindings will be created
-
privateElements
private final PrivateElementsImpl privateElements
-
privateBinders
private final java.util.List<Elements.RecordingBinder> privateBinders
All children private binders, so we can scan through them.
-
-
Constructor Detail
-
RecordingBinder
private RecordingBinder(Stage stage)
-
RecordingBinder
private RecordingBinder(Elements.RecordingBinder prototype, java.lang.Object source, SourceProvider sourceProvider)
Creates a recording binder that's backed byprototype.
-
RecordingBinder
private RecordingBinder(Elements.RecordingBinder parent, PrivateElementsImpl privateElements)
Creates a private recording binder.
-
-
Method Detail
-
bindInterceptor
public void bindInterceptor(Matcher<? super java.lang.Class<?>> classMatcher, Matcher<? super java.lang.reflect.Method> methodMatcher, org.aopalliance.intercept.MethodInterceptor... interceptors)
Description copied from interface:BinderBinds 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
- Specified by:
bindInterceptorin 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
public void bindScope(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, Scope scope)Description copied from interface:BinderBinds a scope to an annotation.
-
requestInjection
public void requestInjection(java.lang.Object instance)
Description copied from interface:BinderUpon successful creation, theInjectorwill inject instance fields and methods of the given object.- Specified by:
requestInjectionin interfaceBinder- Parameters:
instance- for which members will be injected
-
requestInjection
public <T> void requestInjection(TypeLiteral<T> type, T instance)
Description copied from interface:BinderUpon successful creation, theInjectorwill inject instance fields and methods of the given object.- Specified by:
requestInjectionin interfaceBinder- Parameters:
type- of instanceinstance- for which members will be injected
-
getMembersInjector
public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral)
Description copied from interface:BinderReturns 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 mainInjectorhas been created. The members injector will throw anIllegalStateExceptionif you try to use it beforehand.- Specified by:
getMembersInjectorin interfaceBinder- Parameters:
typeLiteral- type to get members injector for
-
getMembersInjector
public <T> MembersInjector<T> getMembersInjector(java.lang.Class<T> type)
Description copied from interface:BinderReturns 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 mainInjectorhas been created. The members injector will throw anIllegalStateExceptionif you try to use it beforehand.- Specified by:
getMembersInjectorin interfaceBinder- Parameters:
type- type to get members injector for
-
bindListener
public void bindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener)
Description copied from interface:BinderRegisters a listener for injectable types. Guice will notify the listener when it encounters injectable types matched by the given type matcher.- Specified by:
bindListenerin 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:BinderRegisters 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:
bindListenerin interfaceBinder- Parameters:
bindingMatcher- that matches bindings of provisioned objects the listener should be notified oflisteners- for provisioned objects matched by bindingMatcher
-
requestStaticInjection
public void requestStaticInjection(java.lang.Class<?>... types)
Description copied from interface:BinderUpon successful creation, theInjectorwill inject static fields and methods in the given classes.- Specified by:
requestStaticInjectionin 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
public void install(Module module)
Description copied from interface:BinderUses the given module to configure more bindings.
-
currentStage
public Stage currentStage()
Description copied from interface:BinderGets the current stage.- Specified by:
currentStagein interfaceBinder
-
addError
public void addError(java.lang.String message, java.lang.Object... arguments)Description copied from interface:BinderRecords 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
public void addError(java.lang.Throwable t)
Description copied from interface:BinderRecords 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
public void addError(Message message)
Description copied from interface:BinderRecords an error message to be presented to the user at a later time.
-
bind
public <T> AnnotatedBindingBuilder<T> bind(Key<T> key)
Description copied from interface:BinderSee the EDSL examples atBinder.
-
bind
public <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral)
Description copied from interface:BinderSee the EDSL examples atBinder.
-
bind
public <T> AnnotatedBindingBuilder<T> bind(java.lang.Class<T> type)
Description copied from interface:BinderSee the EDSL examples atBinder.
-
bindConstant
public AnnotatedConstantBindingBuilder bindConstant()
Description copied from interface:BinderSee the EDSL examples atBinder.- Specified by:
bindConstantin interfaceBinder
-
getProvider
public <T> Provider<T> getProvider(Key<T> key)
Description copied from interface:BinderReturns the provider used to obtain instances for the given injection key. The returned provider will not be valid until theInjectorhas been created. The provider will throw anIllegalStateExceptionif you try to use it beforehand.- Specified by:
getProviderin interfaceBinder
-
getProvider
public <T> Provider<T> getProvider(Dependency<T> dependency)
Description copied from interface:BinderReturns 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 theInjectorhas been created. The provider will throw anIllegalStateExceptionif you try to use it beforehand.- Specified by:
getProviderin interfaceBinder
-
getProvider
public <T> Provider<T> getProvider(java.lang.Class<T> type)
Description copied from interface:BinderReturns the provider used to obtain instances for the given injection type. The returned provider will not be valid until theInjectorhas been created. The provider will throw anIllegalStateExceptionif you try to use it beforehand.- Specified by:
getProviderin interfaceBinder
-
convertToTypes
public void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter)
Description copied from interface:BinderBinds a type converter. The injector will use the given converter to convert string constants to matching types as needed.- Specified by:
convertToTypesin interfaceBinder- Parameters:
typeMatcher- matches types the converter can handleconverter- converts values
-
withSource
public Elements.RecordingBinder withSource(java.lang.Object source)
Description copied from interface:BinderReturns a binder that usessourceas the reference location for configuration errors. This is typically aStackTraceElementfor.javasource but it could any binding source, such as the path to a.propertiesfile.- Specified by:
withSourcein interfaceBinder- Specified by:
withSourcein interfacePrivateBinder- Parameters:
source- any object representing the source location and has a concisetoString()value- Returns:
- a binder that shares its configuration with this binder
-
skipSources
public Elements.RecordingBinder skipSources(java.lang.Class... classesToSkip)
Description copied from interface:BinderReturns a binder that skipsclassesToSkipwhen identify the calling code. The caller'sStackTraceElementis used to locate the source of configuration errors.- Specified by:
skipSourcesin interfaceBinder- Specified by:
skipSourcesin interfacePrivateBinder- Parameters:
classesToSkip- library classes that create bindings on behalf of their clients.- Returns:
- a binder that shares its configuration with this binder.
-
newPrivateBinder
public PrivateBinder newPrivateBinder()
Description copied from interface:BinderCreates a new private child environment for bindings and other configuration. The returned binder can be used to add and configuration information in this environment. SeePrivateModulefor details.- Specified by:
newPrivateBinderin 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:BinderPrevents 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:
disableCircularProxiesin interfaceBinder
-
requireExplicitBindings
public void requireExplicitBindings()
Description copied from interface:BinderInstructs 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:
requireExplicitBindingsin interfaceBinder
-
requireAtInjectOnConstructors
public void requireAtInjectOnConstructors()
Description copied from interface:BinderRequires that a @Injectannotation 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 @Injectannotation 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:
requireAtInjectOnConstructorsin interfaceBinder
-
requireExactBindingAnnotations
public void requireExactBindingAnnotations()
Description copied from interface:BinderRequires that Guice finds an exactly matching binding annotation. This disables the error-prone feature in Guice where it can substitute a binding for@Named Foowhen attempting to inject@Named("foo") Foo.- Specified by:
requireExactBindingAnnotationsin interfaceBinder
-
scanModulesForAnnotatedMethods
public void scanModulesForAnnotatedMethods(ModuleAnnotatedMethodScanner scanner)
Description copied from interface:BinderAdds 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:
scanModulesForAnnotatedMethodsin interfaceBinder
-
expose
public void expose(Key<?> key)
Description copied from interface:PrivateBinderMakes the binding forkeyavailable to the enclosing environment- Specified by:
exposein interfacePrivateBinder
-
expose
public AnnotatedElementBuilder expose(java.lang.Class<?> type)
Description copied from interface:PrivateBinderMakes a binding fortypeavailable to the enclosing environment. UseannotatedWith()to exposetypewith a binding annotation.- Specified by:
exposein interfacePrivateBinder
-
expose
public AnnotatedElementBuilder expose(TypeLiteral<?> type)
Description copied from interface:PrivateBinderMakes a binding fortypeavailable to the enclosing environment. UseannotatedWith()to exposetypewith a binding annotation.- Specified by:
exposein interfacePrivateBinder
-
exposeInternal
private <T> AnnotatedElementBuilder exposeInternal(Key<T> key)
-
getModuleSource
private ModuleSource getModuleSource(java.lang.Object module)
-
getElementSource
private ElementSource getElementSource()
-
getPartialCallStack
private java.lang.StackTraceElement[] getPartialCallStack(java.lang.StackTraceElement[] callStack)
Removes themoduleSourcecall stack from the beginning of current call stack. It also removes the last two elements in order to makeinstall(Module)the last call in the call stack.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-