Package dagger
Annotation Type Binds
-
@Documented @Retention(RUNTIME) @Target(METHOD) public @interface Binds
Annotates abstract methods of aModule
that delegate bindings. For example, to bindRandom
toSecureRandom
a module could declare the following:@Binds abstract Random bindRandom(SecureRandom secureRandom);
@Binds
methods are a drop-in replacement forProvides
methods that simply return an injected parameter. Prefer@Binds
because the generated implementation is likely to be more efficient.A
@Binds
method:- Must be
abstract
. - May be scoped.
- May be qualified.
- Must have a single parameter whose type is assignable to the return type. The return type
declares the bound type (just as it would for a @
Provides
method) and the parameter is the type to which it is bound.For multibindings, assignability is checked in similar ways:
IntoSet
- The parameter must be assignable to the only parameter of
Set.add(E)
when viewed as a member of the return type — the parameter must be assignable to the return type. ElementsIntoSet
- The parameter must be assignable to the only parameter of
Set.addAll(java.util.Collection<? extends E>)
when viewed as a member of the return type — if the return type isSet<E>
, the parameter must be assignable toCollection<? extends E>
. IntoMap
- The parameter must be assignable to the
value
parameter ofMap.put(K, V)
when viewed as a member of aMap
in whichV
is bound to the return type — the parameter must be assignable to the return type
- Must be