xref: /aosp_15_r20/external/dagger2/java/dagger/BindsInstance.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2016 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger;
18 
19 import static java.lang.annotation.ElementType.METHOD;
20 import static java.lang.annotation.ElementType.PARAMETER;
21 import static java.lang.annotation.RetentionPolicy.RUNTIME;
22 
23 import dagger.internal.Beta;
24 import java.lang.annotation.Documented;
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.Target;
27 
28 /**
29  * Marks a method on a {@linkplain Component.Builder component builder} or a parameter on a
30  * {@linkplain Component.Factory component factory} as binding an instance to some key within the
31  * component.
32  *
33  * <p>For example:
34  *
35  * <pre>
36  *   {@literal @Component.Builder}
37  *   interface Builder {
38  *     {@literal @BindsInstance} Builder foo(Foo foo);
39  *     {@literal @BindsInstance} Builder bar({@literal @Blue} Bar bar);
40  *     ...
41  *   }
42  *
43  *   // or
44  *
45  *   {@literal @Component.Factory}
46  *   interface Factory {
47  *     MyComponent newMyComponent(
48  *         {@literal @BindsInstance} Foo foo,
49  *         {@literal @BindsInstance @Blue} Bar bar);
50  *   }
51  * </pre>
52  *
53  * <p>will allow clients of the builder or factory to pass their own instances of {@code Foo} and
54  * {@code Bar}, and those instances can be injected within the component as {@code Foo} or
55  * {@code @Blue Bar}, respectively. It's important to note that unlike in factories, the methods in
56  * builders should only accept and bind a single parameter each. Using the following will result in
57  * an error:
58  *
59  * <pre>
60  *   {@literal @Component.Builder}
61  *   interface Builder {
62  *     // Error! Builder methods can only have one parameter
63  *     {@literal @BindsInstance} Builder fooAndBar(Foo foo, {@literal @Blue} Bar bar);
64  *     ...
65  *   }
66  * </pre>
67  *
68  * <p>{@code @BindsInstance} arguments may not be {@code null} unless the parameter is annotated
69  * with {@code @Nullable}.
70  *
71  * <p>For builders, {@code @BindsInstance} methods must be called before building the component,
72  * unless their parameter is marked {@code @Nullable}, in which case the component will act as
73  * though it was called with a {@code null} argument. Primitives, of course, may not be marked
74  * {@code @Nullable}.
75  *
76  * <p>Binding an instance is equivalent to passing an instance to a module constructor and providing
77  * that instance, but is often more efficient. When possible, binding object instances should be
78  * preferred to using module instances.
79  */
80 @Documented
81 @Retention(RUNTIME)
82 @Target({METHOD, PARAMETER})
83 @Beta
84 public @interface BindsInstance {}
85