Package dagger
Annotation Type BindsOptionalOf
-
@Documented @Beta @Retention(RUNTIME) @Target(METHOD) public @interface BindsOptionalOf
Annotates methods that declare bindings forOptional
containers of values from bindings that may or may not be present in the component.If a module contains a method declaration like this:
@BindsOptionalOf abstract Foo optionalFoo();
then any binding in the component can depend on anOptional
ofFoo
. If there is no binding forFoo
in the component, theOptional
will be absent. If there is a binding forFoo
in the component, theOptional
will be present, and its value will be the value given by the binding forFoo
.A
@BindsOptionalOf
method:- must be
abstract
- may have a qualifier annotation
- must not return
void
- must not have parameters
- must not throw exceptions
- must not return an unqualified type with an
@Inject
-annotated constructor, since such a type is always present
Other bindings may inject any of:
Optional<Foo>
(unless there is a@Nullable
binding forFoo
; see below)Optional<Provider<Foo>>
Optional<Lazy<Foo>>
Optional<Provider<Lazy<Foo>>>
If there is a binding for
Foo
, and that binding is@Nullable
, then it is a compile-time error to injectOptional<Foo>
, becauseOptional
cannot containnull
. You can always inject the other forms, becauseProvider
andLazy
can always returnnull
from theirget()
methods.Explicit bindings for any of the above will conflict with a
@BindsOptionalOf
binding.If the binding for
Foo
is a@Produces
binding, then another@Produces
binding can depend on any of:Optional<Foo>
Optional<Producer<Foo>>
Optional<Produced<Foo>>
You can inject either
com.google.common.base.Optional
orjava.util.Optional
. - must be