| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 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; |
| 18 | |
| dpb | 3379b95 | 2018-09-24 13:39:04 -0700 | [diff] [blame] | 19 | import static dagger.internal.codegen.BindingRequest.bindingRequest; |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 20 | |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.google.common.collect.Sets; |
| 23 | import com.google.common.collect.Sets.SetView; |
| 24 | import com.squareup.javapoet.ClassName; |
| 25 | import com.squareup.javapoet.CodeBlock; |
| 26 | import dagger.internal.codegen.ModifiableBindingMethods.ModifiableBindingMethod; |
| 27 | import dagger.model.DependencyRequest; |
| 28 | import dagger.model.RequestKind; |
| 29 | import java.util.Optional; |
| 30 | |
| 31 | /** An abstract base class for multibinding {@link BindingExpression}s. */ |
| 32 | abstract 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 | /** |
| dstrasburg | b147c99 | 2018-10-16 19:04:33 -0700 | [diff] [blame] | 57 | * 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. |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 62 | */ |
| 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( |
| dpb | 3379b95 | 2018-09-24 13:39:04 -0700 | [diff] [blame] | 78 | bindingRequest(binding.key(), RequestKind.INSTANCE)); |
| dstrasburg | b147c99 | 2018-10-16 19:04:33 -0700 | [diff] [blame] | 79 | 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 | } |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | return Optional.empty(); |
| 88 | } |
| 89 | } |