beder | e05d787 | 2016-12-14 12:01:03 -0800 | [diff] [blame] | 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; |
cgdecker | d340886 | 2019-01-25 11:06:31 -0800 | [diff] [blame] | 20 | import static java.lang.annotation.ElementType.PARAMETER; |
beder | e05d787 | 2016-12-14 12:01:03 -0800 | [diff] [blame] | 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 | /** |
cgdecker | d340886 | 2019-01-25 11:06:31 -0800 | [diff] [blame] | 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. |
beder | e05d787 | 2016-12-14 12:01:03 -0800 | [diff] [blame] | 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 | * } |
cgdecker | d340886 | 2019-01-25 11:06:31 -0800 | [diff] [blame] | 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 | * } |
beder | e05d787 | 2016-12-14 12:01:03 -0800 | [diff] [blame] | 51 | * </pre> |
| 52 | * |
cgdecker | d340886 | 2019-01-25 11:06:31 -0800 | [diff] [blame] | 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. |
beder | e05d787 | 2016-12-14 12:01:03 -0800 | [diff] [blame] | 56 | * |
cgdecker | d340886 | 2019-01-25 11:06:31 -0800 | [diff] [blame] | 57 | * <p>{@code @BindsInstance} arguments may not be {@code null} unless the parameter is annotated |
| 58 | * with {@code @Nullable}. |
beder | e05d787 | 2016-12-14 12:01:03 -0800 | [diff] [blame] | 59 | * |
cgdecker | d340886 | 2019-01-25 11:06:31 -0800 | [diff] [blame] | 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}. |
beder | e05d787 | 2016-12-14 12:01:03 -0800 | [diff] [blame] | 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) |
cgdecker | d340886 | 2019-01-25 11:06:31 -0800 | [diff] [blame] | 71 | @Target({METHOD, PARAMETER}) |
beder | e05d787 | 2016-12-14 12:01:03 -0800 | [diff] [blame] | 72 | @Beta |
| 73 | public @interface BindsInstance {} |