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