1 /* 2 * Copyright (C) 2018 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 21 import com.squareup.javapoet.CodeBlock; 22 import dagger.internal.codegen.binding.BindingRequest; 23 import dagger.internal.codegen.binding.ContributionBinding; 24 import dagger.internal.codegen.javapoet.CodeBlocks; 25 import dagger.internal.codegen.writing.FrameworkFieldInitializer.FrameworkInstanceCreationExpression; 26 import dagger.model.DependencyRequest; 27 28 /** An abstract factory creation expression for multibindings. */ 29 abstract class MultibindingFactoryCreationExpression 30 implements FrameworkInstanceCreationExpression { 31 private final ComponentImplementation componentImplementation; 32 private final ComponentBindingExpressions componentBindingExpressions; 33 private final ContributionBinding binding; 34 MultibindingFactoryCreationExpression( ContributionBinding binding, ComponentImplementation componentImplementation, ComponentBindingExpressions componentBindingExpressions)35 MultibindingFactoryCreationExpression( 36 ContributionBinding binding, 37 ComponentImplementation componentImplementation, 38 ComponentBindingExpressions componentBindingExpressions) { 39 this.binding = checkNotNull(binding); 40 this.componentImplementation = checkNotNull(componentImplementation); 41 this.componentBindingExpressions = checkNotNull(componentBindingExpressions); 42 } 43 44 /** Returns the expression for a dependency of this multibinding. */ multibindingDependencyExpression(DependencyRequest dependency)45 protected final CodeBlock multibindingDependencyExpression(DependencyRequest dependency) { 46 CodeBlock expression = 47 componentBindingExpressions 48 .getDependencyExpression( 49 BindingRequest.bindingRequest(dependency.key(), binding.frameworkType()), 50 componentImplementation.name()) 51 .codeBlock(); 52 53 return useRawType() 54 ? CodeBlocks.cast(expression, binding.frameworkType().frameworkClass()) 55 : expression; 56 } 57 58 /** The binding request for this framework instance. */ bindingRequest()59 protected final BindingRequest bindingRequest() { 60 return BindingRequest.bindingRequest(binding.key(), binding.frameworkType()); 61 } 62 63 /** 64 * Returns true if the {@linkplain ContributionBinding#key() key type} is inaccessible from the 65 * component, and therefore a raw type must be used. 66 */ useRawType()67 protected final boolean useRawType() { 68 return !componentImplementation.isTypeAccessible(binding.key().type()); 69 } 70 71 @Override useInnerSwitchingProvider()72 public final boolean useInnerSwitchingProvider() { 73 return !binding.dependencies().isEmpty(); 74 } 75 } 76