Package dagger
Annotation Type BindsOptionalOf
-
@Documented @Beta @Retention(RUNTIME) @Target(METHOD) public @interface BindsOptionalOfAnnotates methods that declare bindings forOptionalcontainers 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 anOptionalofFoo. If there is no binding forFooin the component, theOptionalwill be absent. If there is a binding forFooin the component, theOptionalwill be present, and its value will be the value given by the binding forFoo.A
@BindsOptionalOfmethod:- 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@Nullablebinding 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>, becauseOptionalcannot containnull. You can always inject the other forms, becauseProviderandLazycan always returnnullfrom theirget()methods.Explicit bindings for any of the above will conflict with a
@BindsOptionalOfbinding.If the binding for
Foois a@Producesbinding, then another@Producesbinding can depend on any of:Optional<Foo>Optional<Producer<Foo>>Optional<Produced<Foo>>
You can inject either
com.google.common.base.Optionalorjava.util.Optional. - must be