xref: /aosp_15_r20/external/dagger2/java/dagger/BindsOptionalOf.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1*f585d8a3SJacky Wang /*
2*f585d8a3SJacky Wang  * Copyright (C) 2016 The Dagger Authors.
3*f585d8a3SJacky Wang  *
4*f585d8a3SJacky Wang  * Licensed under the Apache License, Version 2.0 (the "License");
5*f585d8a3SJacky Wang  * you may not use this file except in compliance with the License.
6*f585d8a3SJacky Wang  * You may obtain a copy of the License at
7*f585d8a3SJacky Wang  *
8*f585d8a3SJacky Wang  * http://www.apache.org/licenses/LICENSE-2.0
9*f585d8a3SJacky Wang  *
10*f585d8a3SJacky Wang  * Unless required by applicable law or agreed to in writing, software
11*f585d8a3SJacky Wang  * distributed under the License is distributed on an "AS IS" BASIS,
12*f585d8a3SJacky Wang  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*f585d8a3SJacky Wang  * See the License for the specific language governing permissions and
14*f585d8a3SJacky Wang  * limitations under the License.
15*f585d8a3SJacky Wang  */
16*f585d8a3SJacky Wang 
17*f585d8a3SJacky Wang package dagger;
18*f585d8a3SJacky Wang 
19*f585d8a3SJacky Wang import static java.lang.annotation.ElementType.METHOD;
20*f585d8a3SJacky Wang import static java.lang.annotation.RetentionPolicy.RUNTIME;
21*f585d8a3SJacky Wang 
22*f585d8a3SJacky Wang import dagger.internal.Beta;
23*f585d8a3SJacky Wang import java.lang.annotation.Documented;
24*f585d8a3SJacky Wang import java.lang.annotation.Retention;
25*f585d8a3SJacky Wang import java.lang.annotation.Target;
26*f585d8a3SJacky Wang import javax.inject.Inject;
27*f585d8a3SJacky Wang import javax.inject.Qualifier;
28*f585d8a3SJacky Wang 
29*f585d8a3SJacky Wang /**
30*f585d8a3SJacky Wang  * Annotates methods that declare bindings for {@code Optional} containers of values from bindings
31*f585d8a3SJacky Wang  * that may or may not be present in the component.
32*f585d8a3SJacky Wang  *
33*f585d8a3SJacky Wang  * <p>If a module contains a method declaration like this:
34*f585d8a3SJacky Wang  *
35*f585d8a3SJacky Wang  * <pre>
36*f585d8a3SJacky Wang  * {@literal @BindsOptionalOf} abstract Foo optionalFoo();</pre>
37*f585d8a3SJacky Wang  *
38*f585d8a3SJacky Wang  * then any binding in the component can depend on an {@code Optional} of {@code Foo}. If there is
39*f585d8a3SJacky Wang  * no binding for {@code Foo} in the component, the {@code Optional} will be absent. If there is a
40*f585d8a3SJacky Wang  * binding for {@code Foo} in the component, the {@code Optional} will be present, and its value
41*f585d8a3SJacky Wang  * will be the value given by the binding for {@code Foo}.
42*f585d8a3SJacky Wang  *
43*f585d8a3SJacky Wang  * <p>A {@code @BindsOptionalOf} method:
44*f585d8a3SJacky Wang  *
45*f585d8a3SJacky Wang  * <ul>
46*f585d8a3SJacky Wang  *   <li>must be {@code abstract}
47*f585d8a3SJacky Wang  *   <li>may have a {@linkplain Qualifier qualifier} annotation
48*f585d8a3SJacky Wang  *   <li>must not return {@code void}
49*f585d8a3SJacky Wang  *   <li>must not have parameters
50*f585d8a3SJacky Wang  *   <li>must not throw exceptions
51*f585d8a3SJacky Wang  *   <li>must not return an unqualified type with an {@link Inject @Inject}-annotated constructor,
52*f585d8a3SJacky Wang  *       since such a type is always present
53*f585d8a3SJacky Wang  * </ul>
54*f585d8a3SJacky Wang  *
55*f585d8a3SJacky Wang  * <p>Other bindings may inject any of:
56*f585d8a3SJacky Wang  *
57*f585d8a3SJacky Wang  * <ul>
58*f585d8a3SJacky Wang  *   <li>{@code Optional<Foo>} (unless there is a {@code @Nullable} binding for {@code Foo}; see
59*f585d8a3SJacky Wang  *       below)
60*f585d8a3SJacky Wang  *   <li>{@code Optional<Provider<Foo>>}
61*f585d8a3SJacky Wang  *   <li>{@code Optional<Lazy<Foo>>}
62*f585d8a3SJacky Wang  *   <li>{@code Optional<Provider<Lazy<Foo>>>}
63*f585d8a3SJacky Wang  * </ul>
64*f585d8a3SJacky Wang  *
65*f585d8a3SJacky Wang  * <p>If there is a binding for {@code Foo}, and that binding is {@code @Nullable}, then it is a
66*f585d8a3SJacky Wang  * compile-time error to inject {@code Optional<Foo>}, because {@code Optional} cannot contain
67*f585d8a3SJacky Wang  * {@code null}. You can always inject the other forms, because {@link Provider} and {@link Lazy}
68*f585d8a3SJacky Wang  * can always return {@code null} from their {@code get()} methods.
69*f585d8a3SJacky Wang  *
70*f585d8a3SJacky Wang  * <p>Explicit bindings for any of the above will conflict with a {@code @BindsOptionalOf} binding.
71*f585d8a3SJacky Wang  *
72*f585d8a3SJacky Wang  * <p>If the binding for {@code Foo} is a {@code @Produces} binding, then another {@code @Produces}
73*f585d8a3SJacky Wang  * binding can depend on any of:
74*f585d8a3SJacky Wang  *
75*f585d8a3SJacky Wang  * <ul>
76*f585d8a3SJacky Wang  *   <li>{@code Optional<Foo>}
77*f585d8a3SJacky Wang  *       <!-- TODO(dpb): Update this once producers support nullability checks -->
78*f585d8a3SJacky Wang  *   <li>{@code Optional<Producer<Foo>>}
79*f585d8a3SJacky Wang  *   <li>{@code Optional<Produced<Foo>>}
80*f585d8a3SJacky Wang  * </ul>
81*f585d8a3SJacky Wang  *
82*f585d8a3SJacky Wang  * <p>You can inject either {@code com.google.common.base.Optional} or {@code java.util.Optional}.
83*f585d8a3SJacky Wang  */
84*f585d8a3SJacky Wang @Documented
85*f585d8a3SJacky Wang @Beta
86*f585d8a3SJacky Wang @Retention(RUNTIME)
87*f585d8a3SJacky Wang @Target(METHOD)
88*f585d8a3SJacky Wang public @interface BindsOptionalOf {}
89