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