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.ElementType.PARAMETER; 21*f585d8a3SJacky Wang import static java.lang.annotation.RetentionPolicy.RUNTIME; 22*f585d8a3SJacky Wang 23*f585d8a3SJacky Wang import dagger.internal.Beta; 24*f585d8a3SJacky Wang import java.lang.annotation.Documented; 25*f585d8a3SJacky Wang import java.lang.annotation.Retention; 26*f585d8a3SJacky Wang import java.lang.annotation.Target; 27*f585d8a3SJacky Wang 28*f585d8a3SJacky Wang /** 29*f585d8a3SJacky Wang * Marks a method on a {@linkplain Component.Builder component builder} or a parameter on a 30*f585d8a3SJacky Wang * {@linkplain Component.Factory component factory} as binding an instance to some key within the 31*f585d8a3SJacky Wang * component. 32*f585d8a3SJacky Wang * 33*f585d8a3SJacky Wang * <p>For example: 34*f585d8a3SJacky Wang * 35*f585d8a3SJacky Wang * <pre> 36*f585d8a3SJacky Wang * {@literal @Component.Builder} 37*f585d8a3SJacky Wang * interface Builder { 38*f585d8a3SJacky Wang * {@literal @BindsInstance} Builder foo(Foo foo); 39*f585d8a3SJacky Wang * {@literal @BindsInstance} Builder bar({@literal @Blue} Bar bar); 40*f585d8a3SJacky Wang * ... 41*f585d8a3SJacky Wang * } 42*f585d8a3SJacky Wang * 43*f585d8a3SJacky Wang * // or 44*f585d8a3SJacky Wang * 45*f585d8a3SJacky Wang * {@literal @Component.Factory} 46*f585d8a3SJacky Wang * interface Factory { 47*f585d8a3SJacky Wang * MyComponent newMyComponent( 48*f585d8a3SJacky Wang * {@literal @BindsInstance} Foo foo, 49*f585d8a3SJacky Wang * {@literal @BindsInstance @Blue} Bar bar); 50*f585d8a3SJacky Wang * } 51*f585d8a3SJacky Wang * </pre> 52*f585d8a3SJacky Wang * 53*f585d8a3SJacky Wang * <p>will allow clients of the builder or factory to pass their own instances of {@code Foo} and 54*f585d8a3SJacky Wang * {@code Bar}, and those instances can be injected within the component as {@code Foo} or 55*f585d8a3SJacky Wang * {@code @Blue Bar}, respectively. It's important to note that unlike in factories, the methods in 56*f585d8a3SJacky Wang * builders should only accept and bind a single parameter each. Using the following will result in 57*f585d8a3SJacky Wang * an error: 58*f585d8a3SJacky Wang * 59*f585d8a3SJacky Wang * <pre> 60*f585d8a3SJacky Wang * {@literal @Component.Builder} 61*f585d8a3SJacky Wang * interface Builder { 62*f585d8a3SJacky Wang * // Error! Builder methods can only have one parameter 63*f585d8a3SJacky Wang * {@literal @BindsInstance} Builder fooAndBar(Foo foo, {@literal @Blue} Bar bar); 64*f585d8a3SJacky Wang * ... 65*f585d8a3SJacky Wang * } 66*f585d8a3SJacky Wang * </pre> 67*f585d8a3SJacky Wang * 68*f585d8a3SJacky Wang * <p>{@code @BindsInstance} arguments may not be {@code null} unless the parameter is annotated 69*f585d8a3SJacky Wang * with {@code @Nullable}. 70*f585d8a3SJacky Wang * 71*f585d8a3SJacky Wang * <p>For builders, {@code @BindsInstance} methods must be called before building the component, 72*f585d8a3SJacky Wang * unless their parameter is marked {@code @Nullable}, in which case the component will act as 73*f585d8a3SJacky Wang * though it was called with a {@code null} argument. Primitives, of course, may not be marked 74*f585d8a3SJacky Wang * {@code @Nullable}. 75*f585d8a3SJacky Wang * 76*f585d8a3SJacky Wang * <p>Binding an instance is equivalent to passing an instance to a module constructor and providing 77*f585d8a3SJacky Wang * that instance, but is often more efficient. When possible, binding object instances should be 78*f585d8a3SJacky Wang * preferred to using module instances. 79*f585d8a3SJacky Wang */ 80*f585d8a3SJacky Wang @Documented 81*f585d8a3SJacky Wang @Retention(RUNTIME) 82*f585d8a3SJacky Wang @Target({METHOD, PARAMETER}) 83*f585d8a3SJacky Wang @Beta 84*f585d8a3SJacky Wang public @interface BindsInstance {} 85