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.internal.codegen.binding.ContributionBinding; 25 import dagger.internal.codegen.javapoet.CodeBlocks; 26 import dagger.internal.codegen.writing.FrameworkFieldInitializer.FrameworkInstanceCreationExpression; 27 import dagger.model.DependencyRequest; 28 29 /** A framework instance creation expression for a {@link dagger.Binds @Binds} binding. */ 30 final class DelegatingFrameworkInstanceCreationExpression 31 implements FrameworkInstanceCreationExpression { 32 33 private final ContributionBinding binding; 34 private final ComponentImplementation componentImplementation; 35 private final ComponentBindingExpressions componentBindingExpressions; 36 DelegatingFrameworkInstanceCreationExpression( ContributionBinding binding, ComponentImplementation componentImplementation, ComponentBindingExpressions componentBindingExpressions)37 DelegatingFrameworkInstanceCreationExpression( 38 ContributionBinding binding, 39 ComponentImplementation componentImplementation, 40 ComponentBindingExpressions componentBindingExpressions) { 41 this.binding = checkNotNull(binding); 42 this.componentImplementation = checkNotNull(componentImplementation); 43 this.componentBindingExpressions = checkNotNull(componentBindingExpressions); 44 } 45 46 @Override creationExpression()47 public CodeBlock creationExpression() { 48 DependencyRequest dependency = getOnlyElement(binding.dependencies()); 49 return CodeBlocks.cast( 50 componentBindingExpressions 51 .getDependencyExpression( 52 bindingRequest(dependency.key(), binding.frameworkType()), 53 componentImplementation.name()) 54 .codeBlock(), 55 binding.frameworkType().frameworkClass()); 56 } 57 } 58