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 dagger.internal.codegen.binding.SourceFiles.setFactoryClassName; 21 22 import com.squareup.javapoet.CodeBlock; 23 import dagger.assisted.Assisted; 24 import dagger.assisted.AssistedFactory; 25 import dagger.assisted.AssistedInject; 26 import dagger.internal.codegen.base.ContributionType; 27 import dagger.internal.codegen.base.SetType; 28 import dagger.internal.codegen.binding.BindingGraph; 29 import dagger.internal.codegen.binding.BindingType; 30 import dagger.internal.codegen.binding.ContributionBinding; 31 import dagger.internal.codegen.javapoet.TypeNames; 32 import dagger.spi.model.DependencyRequest; 33 34 /** A factory creation expression for a multibound set. */ 35 final class SetFactoryCreationExpression extends MultibindingFactoryCreationExpression { 36 private final BindingGraph graph; 37 private final ContributionBinding binding; 38 39 @AssistedInject SetFactoryCreationExpression( @ssisted ContributionBinding binding, ComponentImplementation componentImplementation, ComponentRequestRepresentations componentRequestRepresentations, BindingGraph graph)40 SetFactoryCreationExpression( 41 @Assisted ContributionBinding binding, 42 ComponentImplementation componentImplementation, 43 ComponentRequestRepresentations componentRequestRepresentations, 44 BindingGraph graph) { 45 super(binding, componentImplementation, componentRequestRepresentations); 46 this.binding = checkNotNull(binding); 47 this.graph = graph; 48 } 49 50 @Override creationExpression()51 public CodeBlock creationExpression() { 52 CodeBlock.Builder builder = CodeBlock.builder().add("$T.", setFactoryClassName(binding)); 53 if (!useRawType()) { 54 SetType setType = SetType.from(binding.key()); 55 builder.add( 56 "<$T>", 57 setType.elementsAreTypeOf(TypeNames.PRODUCED) 58 ? setType.unwrappedElementType(TypeNames.PRODUCED).getTypeName() 59 : setType.elementType().getTypeName()); 60 } 61 62 int individualProviders = 0; 63 int setProviders = 0; 64 CodeBlock.Builder builderMethodCalls = CodeBlock.builder(); 65 String methodNameSuffix = 66 binding.bindingType().equals(BindingType.PROVISION) ? "Provider" : "Producer"; 67 68 for (DependencyRequest dependency : binding.dependencies()) { 69 ContributionType contributionType = 70 graph.contributionBinding(dependency.key()).contributionType(); 71 String methodNamePrefix; 72 switch (contributionType) { 73 case SET: 74 individualProviders++; 75 methodNamePrefix = "add"; 76 break; 77 case SET_VALUES: 78 setProviders++; 79 methodNamePrefix = "addCollection"; 80 break; 81 default: 82 throw new AssertionError(dependency + " is not a set multibinding"); 83 } 84 85 builderMethodCalls.add( 86 ".$N$N($L)", 87 methodNamePrefix, 88 methodNameSuffix, 89 multibindingDependencyExpression(dependency)); 90 } 91 builder.add("builder($L, $L)", individualProviders, setProviders); 92 builder.add(builderMethodCalls.build()); 93 94 return builder.add(".build()").build(); 95 } 96 97 @AssistedFactory 98 static interface Factory { create(ContributionBinding binding)99 SetFactoryCreationExpression create(ContributionBinding binding); 100 } 101 } 102