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