1 /* 2 * Copyright (C) 2015 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.checkNotNull; 20 import static com.google.common.collect.Iterables.getOnlyElement; 21 import static dagger.internal.codegen.binding.BindingRequest.bindingRequest; 22 23 import com.squareup.javapoet.CodeBlock; 24 import dagger.assisted.Assisted; 25 import dagger.assisted.AssistedFactory; 26 import dagger.assisted.AssistedInject; 27 import dagger.internal.codegen.binding.ContributionBinding; 28 import dagger.internal.codegen.compileroption.CompilerOptions; 29 import dagger.internal.codegen.javapoet.CodeBlocks; 30 import dagger.internal.codegen.model.DependencyRequest; 31 import dagger.internal.codegen.writing.FrameworkFieldInitializer.FrameworkInstanceCreationExpression; 32 33 /** A framework instance creation expression for a {@link dagger.Binds @Binds} binding. */ 34 final class DelegatingFrameworkInstanceCreationExpression 35 implements FrameworkInstanceCreationExpression { 36 37 private final ContributionBinding binding; 38 private final ComponentImplementation componentImplementation; 39 private final ComponentRequestRepresentations componentRequestRepresentations; 40 41 @AssistedInject DelegatingFrameworkInstanceCreationExpression( @ssisted ContributionBinding binding, ComponentImplementation componentImplementation, ComponentRequestRepresentations componentRequestRepresentations, CompilerOptions compilerOptions)42 DelegatingFrameworkInstanceCreationExpression( 43 @Assisted ContributionBinding binding, 44 ComponentImplementation componentImplementation, 45 ComponentRequestRepresentations componentRequestRepresentations, 46 CompilerOptions compilerOptions) { 47 this.binding = checkNotNull(binding); 48 this.componentImplementation = componentImplementation; 49 this.componentRequestRepresentations = componentRequestRepresentations; 50 } 51 52 @Override creationExpression()53 public CodeBlock creationExpression() { 54 DependencyRequest dependency = getOnlyElement(binding.dependencies()); 55 return CodeBlocks.cast( 56 componentRequestRepresentations 57 .getDependencyExpression( 58 bindingRequest(dependency.key(), binding.frameworkType()), 59 componentImplementation.shardImplementation(binding).name()) 60 .codeBlock(), 61 binding.frameworkType().frameworkClassName()); 62 } 63 64 @AssistedFactory 65 static interface Factory { create(ContributionBinding binding)66 DelegatingFrameworkInstanceCreationExpression create(ContributionBinding binding); 67 } 68 } 69