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 dagger.internal.codegen.javapoet.CodeBlocks.toParametersCodeBlock; 20 21 import com.squareup.javapoet.ClassName; 22 import com.squareup.javapoet.CodeBlock; 23 import dagger.assisted.Assisted; 24 import dagger.assisted.AssistedFactory; 25 import dagger.assisted.AssistedInject; 26 import dagger.internal.codegen.binding.ContributionBinding; 27 import dagger.internal.codegen.javapoet.Expression; 28 import dagger.internal.codegen.writing.ComponentImplementation.ShardImplementation; 29 30 /** A binding expression for a subcomponent creator that just invokes the constructor. */ 31 final class SubcomponentCreatorRequestRepresentation extends RequestRepresentation { 32 private final ShardImplementation shardImplementation; 33 private final ContributionBinding binding; 34 35 @AssistedInject SubcomponentCreatorRequestRepresentation( @ssisted ContributionBinding binding, ComponentImplementation componentImplementation)36 SubcomponentCreatorRequestRepresentation( 37 @Assisted ContributionBinding binding, ComponentImplementation componentImplementation) { 38 this.binding = binding; 39 this.shardImplementation = componentImplementation.shardImplementation(binding); 40 } 41 42 @Override getDependencyExpression(ClassName requestingClass)43 Expression getDependencyExpression(ClassName requestingClass) { 44 return Expression.create( 45 binding.key().type().xprocessing(), 46 "new $T($L)", 47 shardImplementation.getSubcomponentCreatorSimpleName(binding.key()), 48 shardImplementation.componentFieldsByImplementation().values().stream() 49 .map(field -> CodeBlock.of("$N", field)) 50 .collect(toParametersCodeBlock())); 51 } 52 getDependencyExpressionArguments()53 CodeBlock getDependencyExpressionArguments() { 54 return shardImplementation.componentFieldsByImplementation().values().stream() 55 .map(field -> CodeBlock.of("$N", field)) 56 .collect(toParametersCodeBlock()); 57 } 58 59 @AssistedFactory 60 static interface Factory { create(ContributionBinding binding)61 SubcomponentCreatorRequestRepresentation create(ContributionBinding binding); 62 } 63 } 64