1 /* 2 * Copyright (C) 2020 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.assisted; 18 19 import static java.lang.annotation.ElementType.CONSTRUCTOR; 20 import static java.lang.annotation.RetentionPolicy.RUNTIME; 21 22 import java.lang.annotation.Documented; 23 import java.lang.annotation.Retention; 24 import java.lang.annotation.Target; 25 26 /** 27 * Annotates the constuctor of a type that will be created via assisted injection. 28 * 29 * <p>Note that an assisted injection type cannot be scoped. In addition, assisted injection 30 * requires the use of a factory annotated with {@link AssistedFactory} (see the example below). 31 * 32 * <p>Example usage: 33 * 34 * <p>Suppose we have a type, {@code DataService}, that has two dependencies: {@code DataFetcher} 35 * and {@code Config}. When creating {@code DataService}, we would like to pass in an instance of 36 * {@code Config} manually rather than having Dagger create it for us. This can be done using 37 * assisted injection. 38 * 39 * <p>To start, we annotate the {@code DataService} constructor with {@link AssistedInject} and we 40 * annotate the {@code Config} parameter with {@link Assisted}, as shown below: 41 * 42 * <pre><code> 43 * final class DataService { 44 * private final DataFetcher dataFetcher; 45 * private final Config config; 46 * 47 * {@literal @}AssistedInject 48 * DataService(DataFetcher dataFetcher, {@literal @}Assisted Config config) { 49 * this.dataFetcher = dataFetcher; 50 * this.config = config; 51 * } 52 * } 53 * </code></pre> 54 * 55 * <p>Next, we define a factory for the assisted type, {@code DataService}, and annotate it with 56 * {@link AssistedFactory}. The factory must contain a single abstract, non-default method which 57 * takes in all of the assisted parameters (in order) and returns the assisted type. 58 * 59 * <pre><code> 60 * {@literal @}AssistedFactory 61 * interface DataServiceFactory { 62 * DataService create(Config config); 63 * } 64 * </code></pre> 65 * 66 * <p>Dagger will generate an implementation of the factory and bind it to the factory type. The 67 * factory can then be used to create an instance of the assisted type: 68 * 69 * <pre><code> 70 * class MyApplication { 71 * {@literal @}Inject DataServiceFactory dataServiceFactory; 72 * 73 * dataService = dataServiceFactory.create(new Config(...)); 74 * } 75 * </code></pre> 76 */ 77 @Documented 78 @Retention(RUNTIME) 79 @Target(CONSTRUCTOR) 80 public @interface AssistedInject {} 81