blob: 93c773740fc997f459c73d455ce51cd495a0fb83 [file] [log] [blame]
dstrasburg09adeae2018-09-07 12:11:06 -07001/*
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
17package dagger.internal.codegen;
18
dpb3379b952018-09-24 13:39:04 -070019import static dagger.internal.codegen.BindingRequest.bindingRequest;
dstrasburg09adeae2018-09-07 12:11:06 -070020
21import com.google.common.collect.ImmutableSet;
22import com.google.common.collect.Sets;
23import com.google.common.collect.Sets.SetView;
24import com.squareup.javapoet.ClassName;
25import com.squareup.javapoet.CodeBlock;
26import dagger.internal.codegen.ModifiableBindingMethods.ModifiableBindingMethod;
27import dagger.model.DependencyRequest;
28import dagger.model.RequestKind;
29import java.util.Optional;
30
31/** An abstract base class for multibinding {@link BindingExpression}s. */
32abstract class MultibindingExpression extends SimpleInvocationBindingExpression {
33 private final ProvisionBinding binding;
34 private final GeneratedComponentModel generatedComponentModel;
35
36 MultibindingExpression(
37 ResolvedBindings resolvedBindings, GeneratedComponentModel generatedComponentModel) {
38 super(resolvedBindings);
39 this.generatedComponentModel = generatedComponentModel;
40 this.binding = (ProvisionBinding) resolvedBindings.contributionBinding();
41 }
42
43 @Override
44 Expression getDependencyExpression(ClassName requestingClass) {
45 Expression expression = buildDependencyExpression(requestingClass);
46 generatedComponentModel.registerImplementedMultibinding(binding);
47 return expression;
48 }
49
50 /**
51 * Returns an expression that evaluates to the value of a multibinding request for the given
52 * requesting class.
53 */
54 protected abstract Expression buildDependencyExpression(ClassName requestingClass);
55
56 /**
dstrasburgb147c992018-10-16 19:04:33 -070057 * Returns the subset of {@code dependencies} that represent multibinding contributions that were
58 * not included in a superclass implementation of this multibinding method. This is relevant only
59 * for ahead-of-time subcomponents. When not generating ahead-of-time subcomponents there is only
60 * one implementation of a multibinding expression and all {@link DependencyRequest}s from the
61 * argment are returned.
dstrasburg09adeae2018-09-07 12:11:06 -070062 */
63 protected SetView<DependencyRequest> getNewContributions(
64 ImmutableSet<DependencyRequest> dependencies) {
65 return Sets.difference(
66 dependencies, generatedComponentModel.superclassContributionsMade(binding.key()));
67 }
68
69 /**
70 * Returns the {@link CodeBlock} representing a call to a superclass implementation of the
71 * modifiable binding method that encapsulates this binding, if it exists. This is only possible
72 * when generating ahead-of-time subcomponents.
73 */
74 protected Optional<CodeBlock> superMethodCall() {
75 if (generatedComponentModel.supermodel().isPresent()) {
76 Optional<ModifiableBindingMethod> method =
77 generatedComponentModel.getModifiableBindingMethod(
dpb3379b952018-09-24 13:39:04 -070078 bindingRequest(binding.key(), RequestKind.INSTANCE));
dstrasburgb147c992018-10-16 19:04:33 -070079 if (method.isPresent()) {
80 ImmutableSet<DependencyRequest> superclassContributions =
81 generatedComponentModel.superclassContributionsMade(binding.key());
82 if (!superclassContributions.isEmpty()) {
83 return Optional.of(CodeBlock.of("super.$L()", method.get().methodSpec().name));
84 }
dstrasburg09adeae2018-09-07 12:11:06 -070085 }
86 }
87 return Optional.empty();
88 }
89}