blob: eab879600c576683e843a908146451f33c2df222 [file] [log] [blame]
bedere05d7872016-12-14 12:01:03 -08001/*
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
17package dagger;
18
19import static java.lang.annotation.ElementType.METHOD;
cgdeckerd3408862019-01-25 11:06:31 -080020import static java.lang.annotation.ElementType.PARAMETER;
bedere05d7872016-12-14 12:01:03 -080021import static java.lang.annotation.RetentionPolicy.RUNTIME;
22
23import dagger.internal.Beta;
24import java.lang.annotation.Documented;
25import java.lang.annotation.Retention;
26import java.lang.annotation.Target;
27
28/**
cgdeckerd3408862019-01-25 11:06:31 -080029 * 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.
bedere05d7872016-12-14 12:01:03 -080032 *
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 * }
cgdeckerd3408862019-01-25 11:06:31 -080042 *
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 * }
bedere05d7872016-12-14 12:01:03 -080051 * </pre>
52 *
cgdeckerd3408862019-01-25 11:06:31 -080053 * <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.
bedere05d7872016-12-14 12:01:03 -080056 *
cgdeckerd3408862019-01-25 11:06:31 -080057 * <p>{@code @BindsInstance} arguments may not be {@code null} unless the parameter is annotated
58 * with {@code @Nullable}.
bedere05d7872016-12-14 12:01:03 -080059 *
cgdeckerd3408862019-01-25 11:06:31 -080060 * <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}.
bedere05d7872016-12-14 12:01:03 -080064 *
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)
cgdeckerd3408862019-01-25 11:06:31 -080071@Target({METHOD, PARAMETER})
bedere05d7872016-12-14 12:01:03 -080072@Beta
73public @interface BindsInstance {}