Class BoundFieldModule
- java.lang.Object
-
- com.google.inject.testing.fieldbinder.BoundFieldModule
-
- All Implemented Interfaces:
com.google.inject.Module
public final class BoundFieldModule extends Object implements com.google.inject.Module
Automatically creates Guice bindings for fields in an object annotated withBind
.This module is intended for use in tests to reduce the code needed to bind local fields (usually mocks) for injection.
The following rules are followed in determining how fields are bound using this module:
- For each
Bind
annotated field of an object and its superclasses, this module will bind that field's type to that field's value at injector creation time. This includes both instance and static fields. - If
Bind.to()
is specified, the field's value will be bound to the class specified byBind.to()
instead of the field's actual type. - If
Bind.lazy()
is true, this module will delay reading the value from the field until injection time, allowing the field's value to be reassigned during the course of a test's execution. - If a
BindingAnnotation
orQualifier
is present on the field, that field will be bound using that annotation viaAnnotatedBindingBuilder.annotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation>)
. For example,bind(Foo.class).annotatedWith(BarAnnotation.class).toInstance(theValue)
. It is an error to supply more than oneBindingAnnotation
orQualifier
. - If the field is of type
Provider
, the field's value will be bound as aProvider
usingLinkedBindingBuilder.toProvider(com.google.inject.Provider<? extends T>)
to the provider's parameterized type. For example,Provider<Integer>
binds toInteger
. Attempting to bind a non-parameterizedProvider
without aBind.to()
clause is an error.
Example use:
public class TestFoo { // bind(new TypeLiteral
<List<Object>>
() {}).toInstance(listOfObjects); @Bind private List<Object>
listOfObjects = Lists.of(); // private String userName = "string_that_changes_over_time"; // bind(String.class).toProvider(new Provider() { public String get() { return userName; }}); @Bind(lazy = true) private String userName; // bind(SuperClass.class).toInstance(aSubClass); @Bind(to = SuperClass.class) private SubClass aSubClass = new SubClass(); // bind(String.class).annotatedWith(MyBindingAnnotation.class).toInstance(myString); @Bind @MyBindingAnnotation private String myString = "hello"; // bind(Object.class).toProvider(myProvider); @Bind private Provider<Object>
myProvider = getProvider(); @Before public void setUp() { Guice.createInjector(BoundFieldModule.of(this)).injectMembers(this); } }- Author:
- eatnumber1@google.com (Russ Harmon)
- See Also:
Bind
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BoundFieldModule.BoundFieldInfo
Information about a field bound byBoundFieldModule
.static class
BoundFieldModule.WithPermits
Wrapper of BoundFieldModule which enables attaching {@link @RestrictedBindingSource} permits to instances of it.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
configure(com.google.inject.Binder binder)
com.google.common.collect.ImmutableSet<BoundFieldModule.BoundFieldInfo>
getBoundFields()
Returns information about the fields bound by this module.Object
getInstance()
Returns the the object originally passed toof(java.lang.Object)
).static BoundFieldModule
of(Object instance)
Create a BoundFieldModule which binds theBind
annotated fields ofinstance
.
-
-
-
Method Detail
-
of
public static BoundFieldModule of(Object instance)
Create a BoundFieldModule which binds theBind
annotated fields ofinstance
.- Parameters:
instance
- the instance whose fields will be bound.- Returns:
- a module which will bind the
Bind
annotated fields ofinstance
.
-
getInstance
public Object getInstance()
Returns the the object originally passed toof(java.lang.Object)
).
-
getBoundFields
public com.google.common.collect.ImmutableSet<BoundFieldModule.BoundFieldInfo> getBoundFields()
Returns information about the fields bound by this module.Note this is available immediately after construction, fields with errors won't be included but their error messages will be deferred to configuration time.
Fields with invalid null values are included but still cause errors at configuration time.
-
configure
public void configure(com.google.inject.Binder binder)
- Specified by:
configure
in interfacecom.google.inject.Module
-
-