• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
56  *
57  * <p>{@code @BindsInstance} arguments may not be {@code null} unless the parameter is annotated
58  * with {@code @Nullable}.
59  *
60  * <p>For builders, {@code @BindsInstance} methods must be called before building the component,
61  * unless their parameter is marked {@code @Nullable}, in which case the component will act as
62  * though it was called with a {@code null} argument. Primitives, of course, may not be marked
63  * {@code @Nullable}.
64  *
65  * <p>Binding an instance is equivalent to passing an instance to a module constructor and providing
66  * that instance, but is often more efficient. When possible, binding object instances should be
67  * preferred to using module instances.
68  */
69 @Documented
70 @Retention(RUNTIME)
71 @Target({METHOD, PARAMETER})
72 @Beta
73 public @interface BindsInstance {}
74