Package dagger

Annotation Type BindsOptionalOf


  • @Documented
    @Beta
    @Retention(RUNTIME)
    @Target(METHOD)
    public @interface BindsOptionalOf
    Annotates methods that declare bindings for Optional 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 an Optional of Foo. If there is no binding for Foo in the component, the Optional will be absent. If there is a binding for Foo in the component, the Optional will be present, and its value will be the value given by the binding for Foo.

    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 for Foo; 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 inject Optional<Foo>, because Optional cannot contain null. You can always inject the other forms, because Provider and Lazy can always return null from their get() 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 or java.util.Optional.