1 /* 2 * Copyright (C) 2017 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.internal.codegen.writing; 18 19 import static com.google.common.base.Preconditions.checkArgument; 20 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList; 21 import static dagger.internal.codegen.xprocessing.XElements.asConstructor; 22 import static dagger.internal.codegen.xprocessing.XElements.asTypeElement; 23 24 import androidx.room.compiler.processing.XConstructorElement; 25 import androidx.room.compiler.processing.XConstructorType; 26 import androidx.room.compiler.processing.XExecutableParameterElement; 27 import androidx.room.compiler.processing.XMethodType; 28 import androidx.room.compiler.processing.XType; 29 import androidx.room.compiler.processing.XTypeElement; 30 import com.google.common.collect.ImmutableList; 31 import com.squareup.javapoet.ParameterSpec; 32 import dagger.internal.codegen.binding.AssistedInjectionAnnotations; 33 import dagger.internal.codegen.binding.AssistedInjectionAnnotations.AssistedFactoryMetadata; 34 import dagger.internal.codegen.binding.Binding; 35 import dagger.internal.codegen.model.BindingKind; 36 import dagger.internal.codegen.writing.ComponentImplementation.ShardImplementation; 37 import java.util.List; 38 39 /** Utility class for generating unique assisted parameter names for a component shard. */ 40 final class AssistedInjectionParameters { 41 /** 42 * Returns the list of assisted factory parameters as {@link ParameterSpec}s. 43 * 44 * <p>The type of each parameter will be the resolved type given by the binding key, and the name 45 * of each parameter will be the name given in the {@link 46 * dagger.assisted.AssistedInject}-annotated constructor. 47 */ assistedFactoryParameterSpecs( Binding binding, ShardImplementation shardImplementation)48 public static ImmutableList<ParameterSpec> assistedFactoryParameterSpecs( 49 Binding binding, ShardImplementation shardImplementation) { 50 checkArgument(binding.kind() == BindingKind.ASSISTED_FACTORY); 51 XTypeElement factory = asTypeElement(binding.bindingElement().get()); 52 AssistedFactoryMetadata metadata = AssistedFactoryMetadata.create(factory.getType()); 53 XMethodType factoryMethodType = 54 metadata.factoryMethod().asMemberOf(binding.key().type().xprocessing()); 55 return assistedParameterSpecs( 56 // Use the order of the parameters from the @AssistedFactory method but use the parameter 57 // names of the @AssistedInject constructor. 58 metadata.assistedFactoryAssistedParameters().stream() 59 .map(metadata.assistedInjectAssistedParametersMap()::get) 60 .collect(toImmutableList()), 61 factoryMethodType.getParameterTypes(), 62 shardImplementation); 63 } 64 65 /** 66 * Returns the list of assisted parameters as {@link ParameterSpec}s. 67 * 68 * <p>The type of each parameter will be the resolved type given by the binding key, and the name 69 * of each parameter will be the name given in the {@link 70 * dagger.assisted.AssistedInject}-annotated constructor. 71 */ assistedParameterSpecs( Binding binding, ShardImplementation shardImplementation)72 public static ImmutableList<ParameterSpec> assistedParameterSpecs( 73 Binding binding, ShardImplementation shardImplementation) { 74 checkArgument(binding.kind() == BindingKind.ASSISTED_INJECTION); 75 XConstructorElement constructor = asConstructor(binding.bindingElement().get()); 76 XConstructorType constructorType = constructor.asMemberOf(binding.key().type().xprocessing()); 77 return assistedParameterSpecs( 78 constructor.getParameters(), constructorType.getParameterTypes(), shardImplementation); 79 } 80 assistedParameterSpecs( List<XExecutableParameterElement> paramElements, List<XType> paramTypes, ShardImplementation shardImplementation)81 private static ImmutableList<ParameterSpec> assistedParameterSpecs( 82 List<XExecutableParameterElement> paramElements, 83 List<XType> paramTypes, 84 ShardImplementation shardImplementation) { 85 ImmutableList.Builder<ParameterSpec> assistedParameterSpecs = ImmutableList.builder(); 86 for (int i = 0; i < paramElements.size(); i++) { 87 XExecutableParameterElement paramElement = paramElements.get(i); 88 XType paramType = paramTypes.get(i); 89 if (AssistedInjectionAnnotations.isAssistedParameter(paramElement)) { 90 assistedParameterSpecs.add( 91 ParameterSpec.builder( 92 paramType.getTypeName(), 93 shardImplementation.getUniqueFieldNameForAssistedParam(paramElement)) 94 .build()); 95 } 96 } 97 return assistedParameterSpecs.build(); 98 } 99 AssistedInjectionParameters()100 private AssistedInjectionParameters() {} 101 } 102