public final class BoundFieldModule extends java.lang.Object implements Module
Bind.
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:
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.
Bind.to() is specified, the field's value will be bound to the class specified by
Bind.to() instead of the field's actual type.
BindingAnnotation or Qualifier is present on the field,
that field will be bound using that annotation via AnnotatedBindingBuilder.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 one BindingAnnotation or
Qualifier.
Provider, the field's value will be bound as a Provider
using LinkedBindingBuilder.toProvider(com.google.inject.Provider<? extends T>) to the provider's parameterized type. For example,
Provider<Integer> binds to Integer. Attempting to bind a non-parameterized
Provider without a Bind.to() clause is an error.
Example use:
public class TestFoo {
// bind(new TypeLiteral <List<Object>>() {}).toInstance(listOfObjects);
@Bind private List <Object> listOfObjects = Lists.of();
// 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);
}
}
Bind| Modifier and Type | Class and Description |
|---|---|
private static class |
BoundFieldModule.BoundFieldException |
private class |
BoundFieldModule.BoundFieldInfo |
| Modifier and Type | Field and Description |
|---|---|
private Binder |
binder |
private java.lang.Object |
instance |
| Modifier | Constructor and Description |
|---|---|
private |
BoundFieldModule(java.lang.Object instance) |
| Modifier and Type | Method and Description |
|---|---|
private void |
bindField(BoundFieldModule.BoundFieldInfo fieldInfo) |
void |
configure(Binder binder)
Contributes bindings and other configurations for this module to
binder. |
private com.google.common.base.Optional<BoundFieldModule.BoundFieldInfo> |
getBoundFieldInfo(TypeLiteral<?> containingClassType,
java.lang.reflect.Field field)
Retrieve a
BoundFieldModule.BoundFieldInfo. |
private java.lang.Object |
getFieldValue(BoundFieldModule.BoundFieldInfo fieldInfo)
Returns the field value to bind, throwing for non-
@Nullable fields with null values,
and for null "transparent providers". |
private static boolean |
hasInject(java.lang.reflect.Field field) |
private static boolean |
isTransparentProvider(java.lang.Class<?> clazz)
Determines if
clazz is a "transparent provider". |
static BoundFieldModule |
of(java.lang.Object instance)
Create a BoundFieldModule which binds the
Bind annotated fields of instance. |
private void |
throwBoundFieldException(java.lang.reflect.Field field,
java.lang.String format,
java.lang.Object... args) |
private LinkedBindingBuilder<?> |
verifyBindingAnnotations(java.lang.reflect.Field field,
AnnotatedBindingBuilder<?> annotatedBinder) |
private final java.lang.Object instance
private Binder binder
public static BoundFieldModule of(java.lang.Object instance)
Bind annotated fields of instance.instance - the instance whose fields will be bound.Bind annotated fields of instance.private static boolean hasInject(java.lang.reflect.Field field)
private com.google.common.base.Optional<BoundFieldModule.BoundFieldInfo> getBoundFieldInfo(TypeLiteral<?> containingClassType, java.lang.reflect.Field field)
BoundFieldModule.BoundFieldInfo.
This returns a BoundFieldModule.BoundFieldInfo if the field has a Bind annotation.
Otherwise it returns Optional.absent().
private LinkedBindingBuilder<?> verifyBindingAnnotations(java.lang.reflect.Field field, AnnotatedBindingBuilder<?> annotatedBinder)
private static boolean isTransparentProvider(java.lang.Class<?> clazz)
clazz is a "transparent provider".
A transparent provider is a Provider or
Provider which binds to it's parameterized type when used as the argument
to Binder.bind(com.google.inject.Key<T>).
A Provider is transparent if the base class of that object is Provider. In
other words, subclasses of Provider are not transparent. As a special case, if a
Provider has no parameterized type but is otherwise transparent, then it is considered
transparent.
Subclasses of Provider are not considered transparent in order to allow users to
bind those subclasses directly, enabling them to inject the providers themselves.
private void bindField(BoundFieldModule.BoundFieldInfo fieldInfo)
private java.lang.Object getFieldValue(BoundFieldModule.BoundFieldInfo fieldInfo)
@Nullable fields with null values,
and for null "transparent providers".private void throwBoundFieldException(java.lang.reflect.Field field,
java.lang.String format,
java.lang.Object... args)
public void configure(Binder binder)
Modulebinder.
Do not invoke this method directly to install submodules. Instead use
Binder.install(Module), which ensures that provider methods are
discovered.