| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 1 | /* |
| ronshapiro | 5dde42d | 2016-06-17 09:03:35 -0700 | [diff] [blame] | 2 | * Copyright (C) 2014 The Dagger Authors. |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 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 | */ |
| dpb | 1b65b6a | 2016-07-11 12:11:24 -0700 | [diff] [blame] | 16 | |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 17 | package dagger.internal.codegen; |
| 18 | |
| gak | e55f074 | 2016-07-12 15:36:42 -0700 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkArgument; |
| ronshapiro | d697327 | 2016-07-20 15:41:23 -0700 | [diff] [blame] | 20 | import static com.google.common.base.Preconditions.checkNotNull; |
| gak | e55f074 | 2016-07-12 15:36:42 -0700 | [diff] [blame] | 21 | import static com.google.common.base.Verify.verify; |
| 22 | import static com.google.common.collect.Iterables.isEmpty; |
| dpb | e0998bd | 2016-09-09 12:40:48 -0700 | [diff] [blame] | 23 | import static dagger.internal.codegen.BindingKey.contribution; |
| gak | e55f074 | 2016-07-12 15:36:42 -0700 | [diff] [blame] | 24 | import static dagger.internal.codegen.ComponentDescriptor.Kind.PRODUCTION_COMPONENT; |
| 25 | import static dagger.internal.codegen.ComponentDescriptor.isComponentContributionMethod; |
| 26 | import static dagger.internal.codegen.ComponentDescriptor.isComponentProductionMethod; |
| gak | 6624110 | 2016-09-13 12:23:00 -0700 | [diff] [blame] | 27 | import static dagger.internal.codegen.ContributionBinding.Kind.SYNTHETIC_MULTIBOUND_KINDS; |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 28 | import static dagger.internal.codegen.ContributionBinding.Kind.SYNTHETIC_OPTIONAL_BINDING; |
| gak | e55f074 | 2016-07-12 15:36:42 -0700 | [diff] [blame] | 29 | import static dagger.internal.codegen.Key.indexByKey; |
| 30 | import static dagger.internal.codegen.Scope.reusableScope; |
| ronshapiro | ac6dd2b | 2017-08-04 12:48:07 -0700 | [diff] [blame] | 31 | import static dagger.internal.codegen.Util.reentrantComputeIfAbsent; |
| dpb | 141148b | 2016-10-20 12:31:34 -0700 | [diff] [blame] | 32 | import static dagger.internal.codegen.Util.toImmutableSet; |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 33 | import static java.util.function.Predicate.isEqual; |
| gak | e55f074 | 2016-07-12 15:36:42 -0700 | [diff] [blame] | 34 | import static javax.lang.model.element.Modifier.ABSTRACT; |
| 35 | |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 36 | import com.google.auto.common.MoreTypes; |
| 37 | import com.google.auto.value.AutoValue; |
| ronshapiro | 76fe9b9 | 2016-11-07 14:55:51 -0800 | [diff] [blame] | 38 | import com.google.auto.value.extension.memoized.Memoized; |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 39 | import com.google.common.base.VerifyException; |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 40 | import com.google.common.collect.FluentIterable; |
| gak | 959f5cd | 2015-01-29 12:42:09 -0800 | [diff] [blame] | 41 | import com.google.common.collect.ImmutableList; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 42 | import com.google.common.collect.ImmutableMap; |
| 43 | import com.google.common.collect.ImmutableSet; |
| 44 | import com.google.common.collect.ImmutableSetMultimap; |
| dpb | 99d6408 | 2015-11-12 11:27:12 -0800 | [diff] [blame] | 45 | import com.google.common.collect.Iterables; |
| gak | 959f5cd | 2015-01-29 12:42:09 -0800 | [diff] [blame] | 46 | import com.google.common.collect.Lists; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 47 | import com.google.common.collect.Maps; |
| gak | d296d89 | 2015-03-10 23:34:03 -0700 | [diff] [blame] | 48 | import com.google.common.collect.Sets; |
| gak | 9b81e10 | 2015-09-15 14:45:43 -0700 | [diff] [blame] | 49 | import com.google.common.collect.TreeTraverser; |
| dpb | 4cd56e8 | 2016-03-30 10:12:27 -0700 | [diff] [blame] | 50 | import dagger.Reusable; |
| gak | 60ebd65 | 2016-08-17 12:48:52 -0700 | [diff] [blame] | 51 | import dagger.Subcomponent; |
| beder | b50b8ae | 2016-12-07 08:36:26 -0800 | [diff] [blame] | 52 | import dagger.internal.codegen.ComponentDescriptor.BuilderRequirementMethod; |
| gak | 6322fe7 | 2015-04-03 14:57:16 -0700 | [diff] [blame] | 53 | import dagger.internal.codegen.ComponentDescriptor.ComponentMethodDescriptor; |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 54 | import dagger.internal.codegen.ContributionBinding.Kind; |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 55 | import dagger.internal.codegen.Key.HasKey; |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 56 | import dagger.producers.Produced; |
| 57 | import dagger.producers.Producer; |
| jneufeld | 1756a90 | 2015-07-01 15:22:25 -0700 | [diff] [blame] | 58 | import java.util.ArrayDeque; |
| dpb | f025531 | 2015-07-27 15:43:19 -0700 | [diff] [blame] | 59 | import java.util.Collection; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 60 | import java.util.Deque; |
| dpb | 9563a02 | 2017-06-05 08:39:15 -0700 | [diff] [blame] | 61 | import java.util.HashMap; |
| dpb | 67c8797 | 2015-08-12 09:43:27 -0700 | [diff] [blame] | 62 | import java.util.HashSet; |
| dpb | 51bf8b4 | 2015-12-18 09:59:56 -0800 | [diff] [blame] | 63 | import java.util.LinkedHashSet; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 64 | import java.util.List; |
| 65 | import java.util.Map; |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 66 | import java.util.Optional; |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 67 | import java.util.Queue; |
| dpb | 67c8797 | 2015-08-12 09:43:27 -0700 | [diff] [blame] | 68 | import java.util.Set; |
| beder | b50b8ae | 2016-12-07 08:36:26 -0800 | [diff] [blame] | 69 | import java.util.stream.Stream; |
| 70 | import java.util.stream.StreamSupport; |
| dpb | ba28f41 | 2015-09-24 13:11:21 -0700 | [diff] [blame] | 71 | import javax.inject.Inject; |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 72 | import javax.inject.Provider; |
| cgruber | 1fef576 | 2014-11-09 13:00:07 -0800 | [diff] [blame] | 73 | import javax.lang.model.element.AnnotationMirror; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 74 | import javax.lang.model.element.ExecutableElement; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 75 | import javax.lang.model.element.TypeElement; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 76 | import javax.lang.model.util.ElementFilter; |
| 77 | import javax.lang.model.util.Elements; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 78 | |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 79 | /** |
| 80 | * The canonical representation of a full-resolved graph. |
| 81 | * |
| 82 | * @author Gregory Kick |
| 83 | */ |
| 84 | @AutoValue |
| 85 | abstract class BindingGraph { |
| 86 | abstract ComponentDescriptor componentDescriptor(); |
| beder | 3e950f3 | 2014-12-19 17:48:47 -0800 | [diff] [blame] | 87 | abstract ImmutableMap<BindingKey, ResolvedBindings> resolvedBindings(); |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 88 | abstract ImmutableSet<BindingGraph> subgraphs(); |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 89 | |
| dpb | 141148b | 2016-10-20 12:31:34 -0700 | [diff] [blame] | 90 | /** Returns the resolved bindings for the dependencies of {@code binding}. */ |
| 91 | ImmutableSet<ResolvedBindings> resolvedDependencies(ContributionBinding binding) { |
| 92 | return binding |
| 93 | .dependencies() |
| 94 | .stream() |
| 95 | .map( |
| 96 | dependencyRequest -> |
| 97 | resolvedBindings() |
| 98 | .getOrDefault( |
| 99 | dependencyRequest.bindingKey(), |
| 100 | ResolvedBindings.noBindings( |
| 101 | dependencyRequest.bindingKey(), componentDescriptor()))) |
| 102 | .collect(toImmutableSet()); |
| 103 | } |
| gak | 5721a93 | 2015-07-16 11:40:57 -0700 | [diff] [blame] | 104 | /** |
| dpb | 86fe3c7 | 2016-10-07 10:50:45 -0700 | [diff] [blame] | 105 | * The type that defines the component for this graph. |
| 106 | * |
| 107 | * @see ComponentDescriptor#componentDefinitionType() |
| 108 | */ |
| 109 | TypeElement componentType() { |
| 110 | return componentDescriptor().componentDefinitionType(); |
| 111 | } |
| 112 | |
| 113 | /** |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 114 | * Returns the set of modules that are owned by this graph regardless of whether or not any of |
| dpb | b37b9ed | 2016-11-11 14:17:07 -0800 | [diff] [blame] | 115 | * their bindings are used in this graph. For graphs representing top-level {@link |
| 116 | * dagger.Component components}, this set will be the same as |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 117 | * {@linkplain ComponentDescriptor#transitiveModules the component's transitive modules}. For |
| 118 | * {@linkplain Subcomponent subcomponents}, this set will be the transitive modules that are not |
| 119 | * owned by any of their ancestors. |
| 120 | */ |
| 121 | abstract ImmutableSet<ModuleDescriptor> ownedModules(); |
| 122 | |
| 123 | ImmutableSet<TypeElement> ownedModuleTypes() { |
| gak | 6624110 | 2016-09-13 12:23:00 -0700 | [diff] [blame] | 124 | return FluentIterable.from(ownedModules()).transform(ModuleDescriptor::moduleElement).toSet(); |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| gak | 9b81e10 | 2015-09-15 14:45:43 -0700 | [diff] [blame] | 127 | private static final TreeTraverser<BindingGraph> SUBGRAPH_TRAVERSER = |
| 128 | new TreeTraverser<BindingGraph>() { |
| 129 | @Override |
| 130 | public Iterable<BindingGraph> children(BindingGraph node) { |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 131 | return node.subgraphs(); |
| gak | 9b81e10 | 2015-09-15 14:45:43 -0700 | [diff] [blame] | 132 | } |
| 133 | }; |
| 134 | |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 135 | /** |
| dpb | f764700 | 2016-04-21 14:31:35 -0700 | [diff] [blame] | 136 | * The types for which the component needs instances. |
| beder | b50b8ae | 2016-12-07 08:36:26 -0800 | [diff] [blame] | 137 | * |
| dpb | f764700 | 2016-04-21 14:31:35 -0700 | [diff] [blame] | 138 | * <ul> |
| beder | b50b8ae | 2016-12-07 08:36:26 -0800 | [diff] [blame] | 139 | * <li>component dependencies |
| 140 | * <li>{@linkplain #ownedModules() owned modules} with concrete instance bindings that are used |
| 141 | * in the graph |
| 142 | * <li>bound instances |
| dpb | f764700 | 2016-04-21 14:31:35 -0700 | [diff] [blame] | 143 | * </ul> |
| gak | 5721a93 | 2015-07-16 11:40:57 -0700 | [diff] [blame] | 144 | */ |
| ronshapiro | 76fe9b9 | 2016-11-07 14:55:51 -0800 | [diff] [blame] | 145 | @Memoized |
| beder | b50b8ae | 2016-12-07 08:36:26 -0800 | [diff] [blame] | 146 | ImmutableSet<ComponentRequirement> componentRequirements() { |
| 147 | ImmutableSet.Builder<ComponentRequirement> requirements = ImmutableSet.builder(); |
| 148 | StreamSupport.stream(SUBGRAPH_TRAVERSER.preOrderTraversal(this).spliterator(), false) |
| 149 | .flatMap(graph -> graph.resolvedBindings().values().stream()) |
| 150 | .flatMap(bindings -> bindings.contributionBindings().stream()) |
| gak | 6624110 | 2016-09-13 12:23:00 -0700 | [diff] [blame] | 151 | .filter(ContributionBinding::requiresModuleInstance) |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 152 | .map(bindingDeclaration -> bindingDeclaration.contributingModule()) |
| 153 | .filter(Optional::isPresent) |
| 154 | .map(Optional::get) |
| beder | b50b8ae | 2016-12-07 08:36:26 -0800 | [diff] [blame] | 155 | .filter(module -> ownedModuleTypes().contains(module)) |
| 156 | .map(module -> ComponentRequirement.forModule(module.asType())) |
| 157 | .forEach(requirements::add); |
| 158 | componentDescriptor() |
| 159 | .dependencies() |
| 160 | .stream() |
| 161 | .map(dep -> ComponentRequirement.forDependency(dep.asType())) |
| 162 | .forEach(requirements::add); |
| 163 | if (componentDescriptor().builderSpec().isPresent()) { |
| 164 | componentDescriptor() |
| 165 | .builderSpec() |
| 166 | .get() |
| 167 | .requirementMethods() |
| 168 | .stream() |
| 169 | .map(BuilderRequirementMethod::requirement) |
| 170 | .filter(req -> req.kind().equals(ComponentRequirement.Kind.BINDING)) |
| 171 | .forEach(requirements::add); |
| 172 | } |
| 173 | return requirements.build(); |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 174 | } |
| beder | b50b8ae | 2016-12-07 08:36:26 -0800 | [diff] [blame] | 175 | /** Returns the {@link ComponentDescriptor}s for this component and its subcomponents. */ |
| ronshapiro | 0771bf0 | 2015-11-30 10:11:23 -0800 | [diff] [blame] | 176 | ImmutableSet<ComponentDescriptor> componentDescriptors() { |
| gak | 6624110 | 2016-09-13 12:23:00 -0700 | [diff] [blame] | 177 | return SUBGRAPH_TRAVERSER |
| 178 | .preOrderTraversal(this) |
| 179 | .transform(BindingGraph::componentDescriptor) |
| 180 | .toSet(); |
| ronshapiro | 0771bf0 | 2015-11-30 10:11:23 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| beder | b50b8ae | 2016-12-07 08:36:26 -0800 | [diff] [blame] | 183 | ImmutableSet<ComponentRequirement> availableDependencies() { |
| 184 | return Stream.concat( |
| 185 | componentDescriptor() |
| 186 | .transitiveModuleTypes() |
| 187 | .stream() |
| 188 | .filter(dep -> !dep.getModifiers().contains(ABSTRACT)) |
| 189 | .map(module -> ComponentRequirement.forModule(module.asType())), |
| 190 | componentDescriptor() |
| 191 | .dependencies() |
| 192 | .stream() |
| 193 | .map(dep -> ComponentRequirement.forDependency(dep.asType()))) |
| 194 | .collect(toImmutableSet()); |
| gak | 5721a93 | 2015-07-16 11:40:57 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 197 | static final class Factory { |
| 198 | private final Elements elements; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 199 | private final InjectBindingRegistry injectBindingRegistry; |
| 200 | private final Key.Factory keyFactory; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 201 | private final ProvisionBinding.Factory provisionBindingFactory; |
| beder | ca9c82b | 2015-01-23 18:30:22 -0800 | [diff] [blame] | 202 | private final ProductionBinding.Factory productionBindingFactory; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 203 | |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 204 | Factory( |
| 205 | Elements elements, |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 206 | InjectBindingRegistry injectBindingRegistry, |
| beder | 3e950f3 | 2014-12-19 17:48:47 -0800 | [diff] [blame] | 207 | Key.Factory keyFactory, |
| beder | ca9c82b | 2015-01-23 18:30:22 -0800 | [diff] [blame] | 208 | ProvisionBinding.Factory provisionBindingFactory, |
| 209 | ProductionBinding.Factory productionBindingFactory) { |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 210 | this.elements = elements; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 211 | this.injectBindingRegistry = injectBindingRegistry; |
| 212 | this.keyFactory = keyFactory; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 213 | this.provisionBindingFactory = provisionBindingFactory; |
| beder | ca9c82b | 2015-01-23 18:30:22 -0800 | [diff] [blame] | 214 | this.productionBindingFactory = productionBindingFactory; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | BindingGraph create(ComponentDescriptor componentDescriptor) { |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 218 | return create(Optional.empty(), componentDescriptor); |
| gak | 959f5cd | 2015-01-29 12:42:09 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 221 | private BindingGraph create( |
| 222 | Optional<Resolver> parentResolver, ComponentDescriptor componentDescriptor) { |
| dpb | 6bd55de | 2015-09-28 07:58:09 -0700 | [diff] [blame] | 223 | ImmutableSet.Builder<ContributionBinding> explicitBindingsBuilder = ImmutableSet.builder(); |
| gak | e1b68a2 | 2016-04-07 11:54:06 -0700 | [diff] [blame] | 224 | ImmutableSet.Builder<DelegateDeclaration> delegatesBuilder = ImmutableSet.builder(); |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 225 | ImmutableSet.Builder<OptionalBindingDeclaration> optionalsBuilder = ImmutableSet.builder(); |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 226 | |
| 227 | // binding for the component itself |
| dpb | b37b9ed | 2016-11-11 14:17:07 -0800 | [diff] [blame] | 228 | explicitBindingsBuilder.add( |
| 229 | provisionBindingFactory.forComponent(componentDescriptor.componentDefinitionType())); |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 230 | |
| 231 | // Collect Component dependencies. |
| dpb | b37b9ed | 2016-11-11 14:17:07 -0800 | [diff] [blame] | 232 | for (TypeElement componentDependency : componentDescriptor.dependencies()) { |
| ronshapiro | dc07ed5 | 2017-08-23 08:52:10 -0700 | [diff] [blame^] | 233 | explicitBindingsBuilder.add( |
| 234 | provisionBindingFactory.forComponentDependency(componentDependency)); |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 235 | List<ExecutableElement> dependencyMethods = |
| 236 | ElementFilter.methodsIn(elements.getAllMembers(componentDependency)); |
| 237 | for (ExecutableElement method : dependencyMethods) { |
| beder | ca9c82b | 2015-01-23 18:30:22 -0800 | [diff] [blame] | 238 | // MembersInjection methods aren't "provided" explicitly, so ignore them. |
| 239 | if (isComponentContributionMethod(elements, method)) { |
| dpb | 6bd55de | 2015-09-28 07:58:09 -0700 | [diff] [blame] | 240 | explicitBindingsBuilder.add( |
| 241 | componentDescriptor.kind().equals(PRODUCTION_COMPONENT) |
| 242 | && isComponentProductionMethod(elements, method) |
| 243 | ? productionBindingFactory.forComponentMethod(method) |
| 244 | : provisionBindingFactory.forComponentMethod(method)); |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| beder | b50b8ae | 2016-12-07 08:36:26 -0800 | [diff] [blame] | 249 | // Collect bindings on the builder. |
| 250 | if (componentDescriptor.builderSpec().isPresent()) { |
| 251 | for (BuilderRequirementMethod method : |
| 252 | componentDescriptor.builderSpec().get().requirementMethods()) { |
| 253 | if (method.requirement().kind().equals(ComponentRequirement.Kind.BINDING)) { |
| 254 | explicitBindingsBuilder.add(provisionBindingFactory.forBuilderBinding(method)); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 259 | for (Map.Entry<ComponentMethodDescriptor, ComponentDescriptor> |
| 260 | componentMethodAndSubcomponent : |
| 261 | componentDescriptor.subcomponentsByBuilderMethod().entrySet()) { |
| 262 | ComponentMethodDescriptor componentMethod = componentMethodAndSubcomponent.getKey(); |
| 263 | ComponentDescriptor subcomponentDescriptor = componentMethodAndSubcomponent.getValue(); |
| 264 | if (!componentDescriptor.subcomponentsFromModules().contains(subcomponentDescriptor)) { |
| 265 | explicitBindingsBuilder.add( |
| 266 | provisionBindingFactory.forSubcomponentBuilderMethod( |
| 267 | componentMethod.methodElement(), |
| 268 | componentDescriptor.componentDefinitionType())); |
| 269 | } |
| dpb | 99d6408 | 2015-11-12 11:27:12 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| dpb | 51bf8b4 | 2015-12-18 09:59:56 -0800 | [diff] [blame] | 272 | ImmutableSet.Builder<MultibindingDeclaration> multibindingDeclarations = |
| 273 | ImmutableSet.builder(); |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 274 | ImmutableSet.Builder<SubcomponentDeclaration> subcomponentDeclarations = |
| 275 | ImmutableSet.builder(); |
| dpb | 51bf8b4 | 2015-12-18 09:59:56 -0800 | [diff] [blame] | 276 | |
| 277 | // Collect transitive module bindings and multibinding declarations. |
| gak | 5721a93 | 2015-07-16 11:40:57 -0700 | [diff] [blame] | 278 | for (ModuleDescriptor moduleDescriptor : componentDescriptor.transitiveModules()) { |
| dpb | 51bf8b4 | 2015-12-18 09:59:56 -0800 | [diff] [blame] | 279 | explicitBindingsBuilder.addAll(moduleDescriptor.bindings()); |
| 280 | multibindingDeclarations.addAll(moduleDescriptor.multibindingDeclarations()); |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 281 | subcomponentDeclarations.addAll(moduleDescriptor.subcomponentDeclarations()); |
| gak | e1b68a2 | 2016-04-07 11:54:06 -0700 | [diff] [blame] | 282 | delegatesBuilder.addAll(moduleDescriptor.delegateDeclarations()); |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 283 | optionalsBuilder.addAll(moduleDescriptor.optionalDeclarations()); |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| ronshapiro | d804a30 | 2016-11-08 10:21:56 -0800 | [diff] [blame] | 286 | // TODO(dpb,gak): Do we need to bind an empty Set<ReleasableReferenceManager> if there are |
| 287 | // none? |
| 288 | for (Scope scope : componentDescriptor.releasableReferencesScopes()) { |
| 289 | // Add a binding for @ForReleasableReferences(scope) ReleasableReferenceManager. |
| 290 | explicitBindingsBuilder.add( |
| 291 | provisionBindingFactory.provideReleasableReferenceManager(scope)); |
| 292 | |
| 293 | /* Add a binding for Set<ReleasableReferenceManager>. Even if these are added more than |
| 294 | * once, each instance will be equal to the rest. Since they're being added to a set, there |
| 295 | * will be only one instance. */ |
| 296 | explicitBindingsBuilder.add( |
| 297 | provisionBindingFactory.provideSetOfReleasableReferenceManagers()); |
| 298 | |
| 299 | for (AnnotationMirror metadata : scope.releasableReferencesMetadata()) { |
| 300 | // Add a binding for @ForReleasableReferences(scope) TypedReleasableReferenceManager<M>. |
| 301 | explicitBindingsBuilder.add( |
| 302 | provisionBindingFactory.provideTypedReleasableReferenceManager( |
| 303 | scope, metadata.getAnnotationType())); |
| 304 | |
| 305 | /* Add a binding for Set<TypedReleasableReferenceManager<M>>. Even if these are added more |
| 306 | * than once, each instance will be equal to the rest. Since they're being added to a set, |
| 307 | * there will be only one instance. */ |
| 308 | explicitBindingsBuilder.add( |
| 309 | provisionBindingFactory.provideSetOfTypedReleasableReferenceManagers( |
| 310 | metadata.getAnnotationType())); |
| 311 | } |
| 312 | } |
| 313 | |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 314 | final Resolver requestResolver = |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 315 | new Resolver( |
| dpb | 51bf8b4 | 2015-12-18 09:59:56 -0800 | [diff] [blame] | 316 | parentResolver, |
| 317 | componentDescriptor, |
| 318 | indexByKey(explicitBindingsBuilder.build()), |
| gak | e1b68a2 | 2016-04-07 11:54:06 -0700 | [diff] [blame] | 319 | indexByKey(multibindingDeclarations.build()), |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 320 | indexByKey(subcomponentDeclarations.build()), |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 321 | indexByKey(delegatesBuilder.build()), |
| 322 | indexByKey(optionalsBuilder.build())); |
| gak | 6322fe7 | 2015-04-03 14:57:16 -0700 | [diff] [blame] | 323 | for (ComponentMethodDescriptor componentMethod : componentDescriptor.componentMethods()) { |
| 324 | Optional<DependencyRequest> componentMethodRequest = componentMethod.dependencyRequest(); |
| 325 | if (componentMethodRequest.isPresent()) { |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 326 | requestResolver.resolve(componentMethodRequest.get().bindingKey()); |
| gak | 6322fe7 | 2015-04-03 14:57:16 -0700 | [diff] [blame] | 327 | } |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 330 | // Resolve all bindings for subcomponents, creating subgraphs for all subcomponents that have |
| 331 | // been detected during binding resolution. If a binding for a subcomponent is never resolved, |
| 332 | // no BindingGraph will be created for it and no implementation will be generated. This is |
| 333 | // done in a queue since resolving one subcomponent might resolve a key for a subcomponent |
| 334 | // from a parent graph. This is done until no more new subcomponents are resolved. |
| 335 | Set<ComponentDescriptor> resolvedSubcomponents = new HashSet<>(); |
| 336 | ImmutableSet.Builder<BindingGraph> subgraphs = ImmutableSet.builder(); |
| 337 | for (ComponentDescriptor subcomponent : |
| 338 | Iterables.consumingIterable(requestResolver.subcomponentsToResolve)) { |
| 339 | if (resolvedSubcomponents.add(subcomponent)) { |
| 340 | subgraphs.add(create(Optional.of(requestResolver), subcomponent)); |
| 341 | } |
| gak | 959f5cd | 2015-01-29 12:42:09 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| gak | 924b5f4 | 2015-08-25 13:27:02 -0700 | [diff] [blame] | 344 | for (ResolvedBindings resolvedBindings : requestResolver.getResolvedBindings().values()) { |
| 345 | verify( |
| 346 | resolvedBindings.owningComponent().equals(componentDescriptor), |
| 347 | "%s is not owned by %s", |
| 348 | resolvedBindings, |
| 349 | componentDescriptor); |
| 350 | } |
| 351 | |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 352 | return new AutoValue_BindingGraph( |
| 353 | componentDescriptor, |
| gak | 78ac368 | 2015-03-30 15:48:00 -0700 | [diff] [blame] | 354 | requestResolver.getResolvedBindings(), |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 355 | subgraphs.build(), |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 356 | requestResolver.getOwnedModules()); |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 359 | private final class Resolver { |
| 360 | final Optional<Resolver> parentResolver; |
| gak | 924b5f4 | 2015-08-25 13:27:02 -0700 | [diff] [blame] | 361 | final ComponentDescriptor componentDescriptor; |
| dpb | 6bd55de | 2015-09-28 07:58:09 -0700 | [diff] [blame] | 362 | final ImmutableSetMultimap<Key, ContributionBinding> explicitBindings; |
| 363 | final ImmutableSet<ContributionBinding> explicitBindingsSet; |
| dpb | db24072 | 2016-01-25 14:06:41 -0800 | [diff] [blame] | 364 | final ImmutableSetMultimap<Key, ContributionBinding> explicitMultibindings; |
| dpb | 51bf8b4 | 2015-12-18 09:59:56 -0800 | [diff] [blame] | 365 | final ImmutableSetMultimap<Key, MultibindingDeclaration> multibindingDeclarations; |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 366 | final ImmutableSetMultimap<Key, SubcomponentDeclaration> subcomponentDeclarations; |
| gak | e1b68a2 | 2016-04-07 11:54:06 -0700 | [diff] [blame] | 367 | final ImmutableSetMultimap<Key, DelegateDeclaration> delegateDeclarations; |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 368 | final ImmutableSetMultimap<Key, OptionalBindingDeclaration> optionalBindingDeclarations; |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 369 | final ImmutableSetMultimap<Key, DelegateDeclaration> delegateMultibindingDeclarations; |
| beder | 3e950f3 | 2014-12-19 17:48:47 -0800 | [diff] [blame] | 370 | final Map<BindingKey, ResolvedBindings> resolvedBindings; |
| jneufeld | 1756a90 | 2015-07-01 15:22:25 -0700 | [diff] [blame] | 371 | final Deque<BindingKey> cycleStack = new ArrayDeque<>(); |
| dpb | 9563a02 | 2017-06-05 08:39:15 -0700 | [diff] [blame] | 372 | final Map<BindingKey, Boolean> bindingKeyDependsOnLocalBindingsCache = new HashMap<>(); |
| 373 | final Map<Binding, Boolean> bindingDependsOnLocalBindingsCache = new HashMap<>(); |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 374 | final Queue<ComponentDescriptor> subcomponentsToResolve = new ArrayDeque<>(); |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 375 | |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 376 | Resolver( |
| 377 | Optional<Resolver> parentResolver, |
| gak | 924b5f4 | 2015-08-25 13:27:02 -0700 | [diff] [blame] | 378 | ComponentDescriptor componentDescriptor, |
| dpb | 51bf8b4 | 2015-12-18 09:59:56 -0800 | [diff] [blame] | 379 | ImmutableSetMultimap<Key, ContributionBinding> explicitBindings, |
| gak | e1b68a2 | 2016-04-07 11:54:06 -0700 | [diff] [blame] | 380 | ImmutableSetMultimap<Key, MultibindingDeclaration> multibindingDeclarations, |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 381 | ImmutableSetMultimap<Key, SubcomponentDeclaration> subcomponentDeclarations, |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 382 | ImmutableSetMultimap<Key, DelegateDeclaration> delegateDeclarations, |
| 383 | ImmutableSetMultimap<Key, OptionalBindingDeclaration> optionalBindingDeclarations) { |
| ronshapiro | d697327 | 2016-07-20 15:41:23 -0700 | [diff] [blame] | 384 | this.parentResolver = checkNotNull(parentResolver); |
| 385 | this.componentDescriptor = checkNotNull(componentDescriptor); |
| 386 | this.explicitBindings = checkNotNull(explicitBindings); |
| dpb | 6bd55de | 2015-09-28 07:58:09 -0700 | [diff] [blame] | 387 | this.explicitBindingsSet = ImmutableSet.copyOf(explicitBindings.values()); |
| ronshapiro | d697327 | 2016-07-20 15:41:23 -0700 | [diff] [blame] | 388 | this.multibindingDeclarations = checkNotNull(multibindingDeclarations); |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 389 | this.subcomponentDeclarations = checkNotNull(subcomponentDeclarations); |
| ronshapiro | d697327 | 2016-07-20 15:41:23 -0700 | [diff] [blame] | 390 | this.delegateDeclarations = checkNotNull(delegateDeclarations); |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 391 | this.optionalBindingDeclarations = checkNotNull(optionalBindingDeclarations); |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 392 | this.resolvedBindings = Maps.newLinkedHashMap(); |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 393 | this.explicitMultibindings = |
| dpb | 90263a5 | 2016-07-13 07:52:41 -0700 | [diff] [blame] | 394 | multibindingContributionsByMultibindingKey(explicitBindingsSet); |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 395 | this.delegateMultibindingDeclarations = |
| dpb | 90263a5 | 2016-07-13 07:52:41 -0700 | [diff] [blame] | 396 | multibindingContributionsByMultibindingKey(delegateDeclarations.values()); |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 397 | subcomponentsToResolve.addAll(componentDescriptor.subcomponentsFromEntryPoints()); |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| gak | 959f5cd | 2015-01-29 12:42:09 -0800 | [diff] [blame] | 400 | /** |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 401 | * Returns the bindings for the given {@link BindingKey}. |
| dpb | ba28f41 | 2015-09-24 13:11:21 -0700 | [diff] [blame] | 402 | * |
| dpb | db8fa1a | 2015-12-04 07:22:14 -0800 | [diff] [blame] | 403 | * <p>For {@link BindingKey.Kind#CONTRIBUTION} requests, returns all of: |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 404 | * |
| dpb | db8fa1a | 2015-12-04 07:22:14 -0800 | [diff] [blame] | 405 | * <ul> |
| dpb | 828b1a2 | 2016-06-14 08:22:11 -0700 | [diff] [blame] | 406 | * <li>All explicit bindings for: |
| 407 | * <ul> |
| 408 | * <li>the requested key |
| 409 | * <li>{@code Set<T>} if the requested key's type is {@code Set<Produced<T>>} |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 410 | * <li>{@code Map<K, Provider<V>>} if the requested key's type is {@code Map<K, |
| 411 | * Producer<V>>}. |
| dpb | 828b1a2 | 2016-06-14 08:22:11 -0700 | [diff] [blame] | 412 | * </ul> |
| 413 | * |
| dpb | db8fa1a | 2015-12-04 07:22:14 -0800 | [diff] [blame] | 414 | * <li>A synthetic binding that depends on {@code Map<K, Producer<V>>} if the requested key's |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 415 | * type is {@code Map<K, V>} and there are some explicit bindings for {@code Map<K, |
| 416 | * Producer<V>>}. |
| dpb | db8fa1a | 2015-12-04 07:22:14 -0800 | [diff] [blame] | 417 | * <li>A synthetic binding that depends on {@code Map<K, Provider<V>>} if the requested key's |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 418 | * type is {@code Map<K, V>} and there are some explicit bindings for {@code Map<K, |
| 419 | * Provider<V>>} but no explicit bindings for {@code Map<K, Producer<V>>}. |
| dpb | db8fa1a | 2015-12-04 07:22:14 -0800 | [diff] [blame] | 420 | * <li>An implicit {@link Inject @Inject}-annotated constructor binding if there is one and |
| 421 | * there are no explicit bindings or synthetic bindings. |
| 422 | * </ul> |
| dpb | ba28f41 | 2015-09-24 13:11:21 -0700 | [diff] [blame] | 423 | * |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 424 | * <p>For {@link BindingKey.Kind#MEMBERS_INJECTION} requests, returns the {@link |
| 425 | * MembersInjectionBinding} for the type. |
| gak | 959f5cd | 2015-01-29 12:42:09 -0800 | [diff] [blame] | 426 | */ |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 427 | ResolvedBindings lookUpBindings(BindingKey bindingKey) { |
| dpb | 828b1a2 | 2016-06-14 08:22:11 -0700 | [diff] [blame] | 428 | Key requestKey = bindingKey.key(); |
| beder | dbe0e9c | 2015-01-06 12:19:22 -0800 | [diff] [blame] | 429 | switch (bindingKey.kind()) { |
| 430 | case CONTRIBUTION: |
| dpb | 51bf8b4 | 2015-12-18 09:59:56 -0800 | [diff] [blame] | 431 | Set<ContributionBinding> contributionBindings = new LinkedHashSet<>(); |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 432 | ImmutableSet.Builder<ContributionBinding> multibindingContributionsBuilder = |
| 433 | ImmutableSet.builder(); |
| dpb | 51bf8b4 | 2015-12-18 09:59:56 -0800 | [diff] [blame] | 434 | ImmutableSet.Builder<MultibindingDeclaration> multibindingDeclarationsBuilder = |
| dpb | bf29ab3 | 2015-12-09 07:37:21 -0800 | [diff] [blame] | 435 | ImmutableSet.builder(); |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 436 | ImmutableSet.Builder<SubcomponentDeclaration> subcomponentDeclarationsBuilder = |
| 437 | ImmutableSet.builder(); |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 438 | ImmutableSet.Builder<OptionalBindingDeclaration> optionalBindingDeclarationsBuilder = |
| 439 | ImmutableSet.builder(); |
| beder | ca9c82b | 2015-01-23 18:30:22 -0800 | [diff] [blame] | 440 | |
| dpb | 828b1a2 | 2016-06-14 08:22:11 -0700 | [diff] [blame] | 441 | for (Key key : keysMatchingRequest(requestKey)) { |
| 442 | contributionBindings.addAll(getExplicitBindings(key)); |
| ronshapiro | d697327 | 2016-07-20 15:41:23 -0700 | [diff] [blame] | 443 | multibindingContributionsBuilder.addAll(getExplicitMultibindings(key)); |
| dpb | 828b1a2 | 2016-06-14 08:22:11 -0700 | [diff] [blame] | 444 | multibindingDeclarationsBuilder.addAll(getMultibindingDeclarations(key)); |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 445 | subcomponentDeclarationsBuilder.addAll(getSubcomponentDeclarations(key)); |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 446 | optionalBindingDeclarationsBuilder.addAll(getOptionalBindingDeclarations(key)); |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 447 | } |
| dpb | 51bf8b4 | 2015-12-18 09:59:56 -0800 | [diff] [blame] | 448 | |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 449 | ImmutableSet<ContributionBinding> multibindingContributions = |
| 450 | multibindingContributionsBuilder.build(); |
| ronshapiro | 23c17b8 | 2016-06-10 09:57:13 -0700 | [diff] [blame] | 451 | ImmutableSet<MultibindingDeclaration> multibindingDeclarations = |
| 452 | multibindingDeclarationsBuilder.build(); |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 453 | ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations = |
| 454 | subcomponentDeclarationsBuilder.build(); |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 455 | ImmutableSet<OptionalBindingDeclaration> optionalBindingDeclarations = |
| 456 | optionalBindingDeclarationsBuilder.build(); |
| ronshapiro | 23c17b8 | 2016-06-10 09:57:13 -0700 | [diff] [blame] | 457 | |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 458 | ImmutableSet.Builder<Optional<ContributionBinding>> maybeContributionBindings = |
| 459 | ImmutableSet.builder(); |
| 460 | maybeContributionBindings.add(syntheticMapOfValuesBinding(requestKey)); |
| 461 | maybeContributionBindings.add( |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 462 | syntheticMultibinding( |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 463 | requestKey, multibindingContributions, multibindingDeclarations)); |
| 464 | syntheticSubcomponentBuilderBinding(subcomponentDeclarations) |
| 465 | .ifPresent( |
| 466 | binding -> { |
| 467 | contributionBindings.add(binding); |
| 468 | addSubcomponentToOwningResolver(binding); |
| 469 | }); |
| 470 | maybeContributionBindings.add( |
| 471 | syntheticOptionalBinding(requestKey, optionalBindingDeclarations)); |
| dpb | db24072 | 2016-01-25 14:06:41 -0800 | [diff] [blame] | 472 | |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 473 | /* If there are no bindings, add the implicit @Inject-constructed binding if there is |
| 474 | * one. */ |
| dpb | 828b1a2 | 2016-06-14 08:22:11 -0700 | [diff] [blame] | 475 | if (contributionBindings.isEmpty()) { |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 476 | maybeContributionBindings.add( |
| 477 | injectBindingRegistry |
| 478 | .getOrFindProvisionBinding(requestKey) |
| 479 | .map((ContributionBinding b) -> b)); |
| beder | dbe0e9c | 2015-01-06 12:19:22 -0800 | [diff] [blame] | 480 | } |
| dpb | 51bf8b4 | 2015-12-18 09:59:56 -0800 | [diff] [blame] | 481 | |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 482 | maybeContributionBindings |
| 483 | .build() |
| 484 | .stream() |
| 485 | .filter(Optional::isPresent) |
| 486 | .map(Optional::get) |
| 487 | .forEach(contributionBindings::add); |
| 488 | |
| dpb | db8fa1a | 2015-12-04 07:22:14 -0800 | [diff] [blame] | 489 | return ResolvedBindings.forContributionBindings( |
| 490 | bindingKey, |
| 491 | componentDescriptor, |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 492 | indexBindingsByOwningComponent( |
| 493 | bindingKey, ImmutableSet.copyOf(contributionBindings)), |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 494 | multibindingDeclarations, |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 495 | subcomponentDeclarations, |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 496 | optionalBindingDeclarations); |
| dpb | 6bd55de | 2015-09-28 07:58:09 -0700 | [diff] [blame] | 497 | |
| beder | dbe0e9c | 2015-01-06 12:19:22 -0800 | [diff] [blame] | 498 | case MEMBERS_INJECTION: |
| 499 | // no explicit deps for members injection, so just look it up |
| beder | c82d8ab | 2015-12-30 06:45:27 -0800 | [diff] [blame] | 500 | Optional<MembersInjectionBinding> binding = |
| dpb | 828b1a2 | 2016-06-14 08:22:11 -0700 | [diff] [blame] | 501 | injectBindingRegistry.getOrFindMembersInjectionBinding(requestKey); |
| beder | c82d8ab | 2015-12-30 06:45:27 -0800 | [diff] [blame] | 502 | return binding.isPresent() |
| 503 | ? ResolvedBindings.forMembersInjectionBinding( |
| 504 | bindingKey, componentDescriptor, binding.get()) |
| 505 | : ResolvedBindings.noBindings(bindingKey, componentDescriptor); |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 506 | |
| beder | dbe0e9c | 2015-01-06 12:19:22 -0800 | [diff] [blame] | 507 | default: |
| 508 | throw new AssertionError(); |
| 509 | } |
| 510 | } |
| 511 | |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 512 | /** |
| 513 | * When a binding is resolved for a {@link SubcomponentDeclaration}, adds corresponding |
| 514 | * {@link ComponentDescriptor subcomponent} to a queue in the owning component's resolver. |
| 515 | * The queue will be used to detect which subcomponents need to be resolved. |
| 516 | */ |
| 517 | private void addSubcomponentToOwningResolver(ProvisionBinding subcomponentBuilderBinding) { |
| 518 | checkArgument(subcomponentBuilderBinding.bindingKind().equals(Kind.SUBCOMPONENT_BUILDER)); |
| 519 | Resolver owningResolver = getOwningResolver(subcomponentBuilderBinding).get(); |
| 520 | |
| 521 | TypeElement builderType = MoreTypes.asTypeElement(subcomponentBuilderBinding.key().type()); |
| 522 | owningResolver.subcomponentsToResolve.add( |
| 523 | owningResolver.componentDescriptor.subcomponentsByBuilderType().get(builderType)); |
| 524 | } |
| 525 | |
| dpb | 828b1a2 | 2016-06-14 08:22:11 -0700 | [diff] [blame] | 526 | private Iterable<Key> keysMatchingRequest(Key requestKey) { |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 527 | ImmutableSet.Builder<Key> keys = ImmutableSet.builder(); |
| 528 | keys.add(requestKey); |
| 529 | keyFactory.unwrapSetKey(requestKey, Produced.class).ifPresent(keys::add); |
| 530 | keyFactory.rewrapMapKey(requestKey, Producer.class, Provider.class).ifPresent(keys::add); |
| 531 | keyFactory.rewrapMapKey(requestKey, Provider.class, Producer.class).ifPresent(keys::add); |
| 532 | return keys.build(); |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 535 | /** |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 536 | * If {@code key} is a {@code Map<K, V>} or {@code Map<K, Produced<V>>}, and there are any |
| 537 | * multibinding contributions or declarations that apply to that map, returns a synthetic |
| 538 | * binding for the {@code key} that depends on an {@linkplain #syntheticMultibinding(Key, |
| 539 | * Iterable, Iterable) underlying synthetic multibinding}. |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 540 | * |
| 541 | * <p>The returned binding has the same {@link BindingType} as the underlying synthetic |
| 542 | * multibinding. |
| 543 | */ |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 544 | private Optional<ContributionBinding> syntheticMapOfValuesBinding(final Key key) { |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 545 | return syntheticMultibinding( |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 546 | key, |
| 547 | multibindingContributionsForValueMap(key), |
| 548 | multibindingDeclarationsForValueMap(key)) |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 549 | .map( |
| gak | 6624110 | 2016-09-13 12:23:00 -0700 | [diff] [blame] | 550 | syntheticMultibinding -> { |
| 551 | switch (syntheticMultibinding.bindingType()) { |
| 552 | case PROVISION: |
| 553 | return provisionBindingFactory.syntheticMapOfValuesBinding(key); |
| ronshapiro | 23c17b8 | 2016-06-10 09:57:13 -0700 | [diff] [blame] | 554 | |
| gak | 6624110 | 2016-09-13 12:23:00 -0700 | [diff] [blame] | 555 | case PRODUCTION: |
| 556 | return productionBindingFactory.syntheticMapOfValuesOrProducedBinding(key); |
| ronshapiro | 23c17b8 | 2016-06-10 09:57:13 -0700 | [diff] [blame] | 557 | |
| gak | 6624110 | 2016-09-13 12:23:00 -0700 | [diff] [blame] | 558 | default: |
| 559 | throw new VerifyException(syntheticMultibinding.toString()); |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 560 | } |
| 561 | }); |
| 562 | } |
| 563 | |
| 564 | /** |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 565 | * If {@code key} is for {@code Map<K, V>} or {@code Map<K, Produced<V>>}, returns all |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 566 | * multibinding contributions whose key is for {@code Map<K, Provider<V>>} or {@code Map<K, |
| 567 | * Producer<V>>} with the same qualifier and {@code K} and {@code V}. |
| 568 | */ |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 569 | private ImmutableSet<ContributionBinding> multibindingContributionsForValueMap(Key key) { |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 570 | return keyFactory |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 571 | .implicitFrameworkMapKeys(key) |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 572 | .stream() |
| 573 | .flatMap(mapKey -> getExplicitMultibindings(mapKey).stream()) |
| 574 | .collect(toImmutableSet()); |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /** |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 578 | * If {@code key} is for {@code Map<K, V>} or {@code Map<K, Produced<V>>}, returns all |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 579 | * multibinding declarations whose key is for {@code Map<K, Provider<V>>} or {@code Map<K, |
| 580 | * Producer<V>>} with the same qualifier and {@code K} and {@code V}. |
| 581 | */ |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 582 | private ImmutableSet<MultibindingDeclaration> multibindingDeclarationsForValueMap(Key key) { |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 583 | return keyFactory |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 584 | .implicitFrameworkMapKeys(key) |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 585 | .stream() |
| 586 | .flatMap(mapKey -> getMultibindingDeclarations(mapKey).stream()) |
| 587 | .collect(toImmutableSet()); |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Returns a synthetic binding that depends on individual multibinding contributions. |
| 592 | * |
| 593 | * <p>If there are no {@code multibindingContributions} or {@code multibindingDeclarations}, |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 594 | * returns {@link Optional#empty()}. |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 595 | * |
| 596 | * <p>If there are production {@code multibindingContributions} or the request is for any of |
| 597 | * the following types, returns a {@link ProductionBinding}. |
| 598 | * |
| 599 | * <ul> |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 600 | * <li>{@code Set<Produced<T>>} |
| 601 | * <li>{@code Map<K, Producer<V>>} |
| 602 | * <li>{@code Map<K, Produced<V>>} |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 603 | * </ul> |
| 604 | * |
| 605 | * Otherwise, returns a {@link ProvisionBinding}. |
| 606 | */ |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 607 | private Optional<ContributionBinding> syntheticMultibinding( |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 608 | Key key, |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 609 | Iterable<ContributionBinding> multibindingContributions, |
| 610 | Iterable<MultibindingDeclaration> multibindingDeclarations) { |
| 611 | if (isEmpty(multibindingContributions) && isEmpty(multibindingDeclarations)) { |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 612 | return Optional.empty(); |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 613 | } else if (multibindingsRequireProduction(multibindingContributions, key)) { |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 614 | return Optional.of( |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 615 | productionBindingFactory.syntheticMultibinding(key, multibindingContributions)); |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 616 | } else { |
| 617 | return Optional.of( |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 618 | provisionBindingFactory.syntheticMultibinding(key, multibindingContributions)); |
| dpb | 1da2999 | 2016-06-24 07:54:52 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
| 622 | private boolean multibindingsRequireProduction( |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 623 | Iterable<ContributionBinding> multibindingContributions, Key key) { |
| 624 | if (MapType.isMap(key)) { |
| 625 | MapType mapType = MapType.from(key); |
| gak | b1b536b | 2016-08-17 09:40:33 -0700 | [diff] [blame] | 626 | if (mapType.valuesAreTypeOf(Producer.class) || mapType.valuesAreTypeOf(Produced.class)) { |
| ronshapiro | 23c17b8 | 2016-06-10 09:57:13 -0700 | [diff] [blame] | 627 | return true; |
| gak | b1b536b | 2016-08-17 09:40:33 -0700 | [diff] [blame] | 628 | } |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 629 | } else if (SetType.isSet(key) && SetType.from(key).elementsAreTypeOf(Produced.class)) { |
| gak | b1b536b | 2016-08-17 09:40:33 -0700 | [diff] [blame] | 630 | return true; |
| ronshapiro | 23c17b8 | 2016-06-10 09:57:13 -0700 | [diff] [blame] | 631 | } |
| gak | 6624110 | 2016-09-13 12:23:00 -0700 | [diff] [blame] | 632 | return Iterables.any(multibindingContributions, |
| 633 | hasBindingType -> hasBindingType.bindingType().equals(BindingType.PRODUCTION)); |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 636 | private Optional<ProvisionBinding> syntheticSubcomponentBuilderBinding( |
| 637 | ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations) { |
| 638 | return subcomponentDeclarations.isEmpty() |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 639 | ? Optional.empty() |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 640 | : Optional.of( |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 641 | provisionBindingFactory.syntheticSubcomponentBuilder(subcomponentDeclarations)); |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 644 | /** |
| 645 | * Returns a synthetic binding for {@code @Qualifier Optional<Type>} if there are any {@code |
| 646 | * optionalBindingDeclarations}. |
| dpb | e0998bd | 2016-09-09 12:40:48 -0700 | [diff] [blame] | 647 | * |
| 648 | * <p>If there are no bindings for the underlying key (the key for dependency requests for |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 649 | * {@code Type}), returns a provision binding that always returns {@link Optional#empty()}. |
| dpb | e0998bd | 2016-09-09 12:40:48 -0700 | [diff] [blame] | 650 | * |
| 651 | * <p>If there are any production bindings for the underlying key, returns a production |
| 652 | * binding. Otherwise returns a provision binding. |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 653 | */ |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 654 | private Optional<ContributionBinding> syntheticOptionalBinding( |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 655 | Key key, ImmutableSet<OptionalBindingDeclaration> optionalBindingDeclarations) { |
| 656 | if (optionalBindingDeclarations.isEmpty()) { |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 657 | return Optional.empty(); |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 658 | } |
| ronshapiro | 3a891dd | 2017-07-04 20:04:14 -0700 | [diff] [blame] | 659 | DependencyRequest.Kind kind = |
| 660 | DependencyRequest.extractKindAndType(OptionalType.from(key).valueType()).kind(); |
| dpb | e0998bd | 2016-09-09 12:40:48 -0700 | [diff] [blame] | 661 | ResolvedBindings underlyingKeyBindings = |
| 662 | lookUpBindings(contribution(keyFactory.unwrapOptional(key).get())); |
| 663 | if (underlyingKeyBindings.isEmpty()) { |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 664 | return Optional.of(provisionBindingFactory.syntheticAbsentBinding(key)); |
| ronshapiro | 3a891dd | 2017-07-04 20:04:14 -0700 | [diff] [blame] | 665 | } else if (underlyingKeyBindings.bindingTypes().contains(BindingType.PRODUCTION) |
| 666 | // handles producerFromProvider cases |
| 667 | || kind.equals(DependencyRequest.Kind.PRODUCER) |
| 668 | || kind.equals(DependencyRequest.Kind.PRODUCED)) { |
| 669 | return Optional.of(productionBindingFactory.syntheticPresentBinding(key, kind)); |
| dpb | e0998bd | 2016-09-09 12:40:48 -0700 | [diff] [blame] | 670 | } else { |
| ronshapiro | 3a891dd | 2017-07-04 20:04:14 -0700 | [diff] [blame] | 671 | return Optional.of(provisionBindingFactory.syntheticPresentBinding(key, kind)); |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
| dpb | 828b1a2 | 2016-06-14 08:22:11 -0700 | [diff] [blame] | 675 | private ImmutableSet<ContributionBinding> createDelegateBindings( |
| gak | e1b68a2 | 2016-04-07 11:54:06 -0700 | [diff] [blame] | 676 | ImmutableSet<DelegateDeclaration> delegateDeclarations) { |
| 677 | ImmutableSet.Builder<ContributionBinding> builder = ImmutableSet.builder(); |
| 678 | for (DelegateDeclaration delegateDeclaration : delegateDeclarations) { |
| ronshapiro | a0bd775 | 2016-08-10 09:48:51 -0700 | [diff] [blame] | 679 | builder.add(createDelegateBinding(delegateDeclaration)); |
| gak | e1b68a2 | 2016-04-07 11:54:06 -0700 | [diff] [blame] | 680 | } |
| 681 | return builder.build(); |
| 682 | } |
| 683 | |
| ronshapiro | a0bd775 | 2016-08-10 09:48:51 -0700 | [diff] [blame] | 684 | /** |
| 685 | * Creates one (and only one) delegate binding for a delegate declaration, based on the |
| 686 | * resolved bindings of the right-hand-side of a {@link dagger.Binds} method. If there are |
| 687 | * duplicate bindings for the dependency key, there should still be only one binding for the |
| 688 | * delegate key. |
| 689 | */ |
| 690 | private ContributionBinding createDelegateBinding(DelegateDeclaration delegateDeclaration) { |
| ronshapiro | 21e9542 | 2016-09-06 11:07:42 -0700 | [diff] [blame] | 691 | BindingKey delegateBindingKey = delegateDeclaration.delegateRequest().bindingKey(); |
| 692 | |
| 693 | if (cycleStack.contains(delegateBindingKey)) { |
| 694 | return provisionBindingFactory.missingDelegate(delegateDeclaration); |
| 695 | } |
| 696 | |
| 697 | ResolvedBindings resolvedDelegate; |
| 698 | try { |
| 699 | cycleStack.push(delegateBindingKey); |
| 700 | resolvedDelegate = lookUpBindings(delegateBindingKey); |
| 701 | } finally { |
| 702 | cycleStack.pop(); |
| 703 | } |
| ronshapiro | a0bd775 | 2016-08-10 09:48:51 -0700 | [diff] [blame] | 704 | if (resolvedDelegate.contributionBindings().isEmpty()) { |
| 705 | // This is guaranteed to result in a missing binding error, so it doesn't matter if the |
| 706 | // binding is a Provision or Production, except if it is a @IntoMap method, in which |
| 707 | // case the key will be of type Map<K, Provider<V>>, which will be "upgraded" into a |
| 708 | // Map<K, Producer<V>> if it's requested in a ProductionComponent. This may result in a |
| 709 | // strange error, that the RHS needs to be provided with an @Inject or @Provides |
| 710 | // annotated method, but a user should be able to figure out if a @Produces annotation |
| 711 | // is needed. |
| 712 | // TODO(gak): revisit how we model missing delegates if/when we clean up how we model |
| 713 | // binding declarations |
| 714 | return provisionBindingFactory.missingDelegate(delegateDeclaration); |
| 715 | } |
| 716 | // It doesn't matter which of these is selected, since they will later on produce a |
| 717 | // duplicate binding error. |
| 718 | // TODO(ronshapiro): Once compile-testing has a CompilationResult, add a test which asserts |
| 719 | // that a duplicate binding for the RHS does not result in a duplicate binding for the LHS. |
| 720 | ContributionBinding explicitDelegate = |
| 721 | resolvedDelegate.contributionBindings().iterator().next(); |
| 722 | switch (explicitDelegate.bindingType()) { |
| 723 | case PRODUCTION: |
| 724 | return productionBindingFactory.delegate( |
| 725 | delegateDeclaration, (ProductionBinding) explicitDelegate); |
| 726 | case PROVISION: |
| 727 | return provisionBindingFactory.delegate( |
| 728 | delegateDeclaration, (ProvisionBinding) explicitDelegate); |
| 729 | default: |
| 730 | throw new AssertionError("bindingType: " + explicitDelegate); |
| 731 | } |
| 732 | } |
| 733 | |
| dpb | db8fa1a | 2015-12-04 07:22:14 -0800 | [diff] [blame] | 734 | private ImmutableSetMultimap<ComponentDescriptor, ContributionBinding> |
| 735 | indexBindingsByOwningComponent( |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 736 | BindingKey bindingKey, Iterable<? extends ContributionBinding> bindings) { |
| dpb | db8fa1a | 2015-12-04 07:22:14 -0800 | [diff] [blame] | 737 | ImmutableSetMultimap.Builder<ComponentDescriptor, ContributionBinding> index = |
| 738 | ImmutableSetMultimap.builder(); |
| 739 | for (ContributionBinding binding : bindings) { |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 740 | index.put(getOwningComponent(bindingKey, binding), binding); |
| dpb | db8fa1a | 2015-12-04 07:22:14 -0800 | [diff] [blame] | 741 | } |
| 742 | return index.build(); |
| 743 | } |
| 744 | |
| dpb | 76e0747 | 2015-08-04 15:43:09 -0700 | [diff] [blame] | 745 | /** |
| dpb | 2df89f9 | 2016-05-24 06:53:48 -0700 | [diff] [blame] | 746 | * Returns the component that should contain the framework field for {@code binding}. |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 747 | * |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 748 | * <p>If {@code binding} is either not bound in an ancestor component or depends transitively |
| 749 | * on bindings in this component, returns this component. |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 750 | * |
| dpb | 2df89f9 | 2016-05-24 06:53:48 -0700 | [diff] [blame] | 751 | * <p>Otherwise, resolves {@code request} in this component's parent in order to resolve any |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 752 | * multibinding contributions in the parent, and returns the parent-resolved {@link |
| 753 | * ResolvedBindings#owningComponent(ContributionBinding)}. |
| dpb | 5dcbd96 | 2015-11-03 09:46:44 -0800 | [diff] [blame] | 754 | */ |
| 755 | private ComponentDescriptor getOwningComponent( |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 756 | BindingKey bindingKey, ContributionBinding binding) { |
| 757 | if (isResolvedInParent(bindingKey, binding) |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 758 | && !new LocalDependencyChecker().dependsOnLocalBindings(binding)) { |
| dpb | 2df89f9 | 2016-05-24 06:53:48 -0700 | [diff] [blame] | 759 | ResolvedBindings parentResolvedBindings = |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 760 | parentResolver.get().resolvedBindings.get(bindingKey); |
| dpb | 2df89f9 | 2016-05-24 06:53:48 -0700 | [diff] [blame] | 761 | return parentResolvedBindings.owningComponent(binding); |
| 762 | } else { |
| 763 | return componentDescriptor; |
| 764 | } |
| dpb | 5dcbd96 | 2015-11-03 09:46:44 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /** |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 768 | * Returns {@code true} if {@code binding} is owned by an ancestor. If so, {@linkplain |
| 769 | * #resolve resolves} the {@link BindingKey} in this component's parent. Don't resolve |
| 770 | * directly in the owning component in case it depends on multibindings in any of its |
| 771 | * descendants. |
| dpb | 76e0747 | 2015-08-04 15:43:09 -0700 | [diff] [blame] | 772 | */ |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 773 | private boolean isResolvedInParent(BindingKey bindingKey, ContributionBinding binding) { |
| dpb | 6bd55de | 2015-09-28 07:58:09 -0700 | [diff] [blame] | 774 | Optional<Resolver> owningResolver = getOwningResolver(binding); |
| dpb | 76e0747 | 2015-08-04 15:43:09 -0700 | [diff] [blame] | 775 | if (owningResolver.isPresent() && !owningResolver.get().equals(this)) { |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 776 | parentResolver.get().resolve(bindingKey); |
| dpb | 76e0747 | 2015-08-04 15:43:09 -0700 | [diff] [blame] | 777 | return true; |
| 778 | } else { |
| 779 | return false; |
| 780 | } |
| 781 | } |
| 782 | |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 783 | private Optional<Resolver> getOwningResolver(ContributionBinding binding) { |
| dpb | 4cd56e8 | 2016-03-30 10:12:27 -0700 | [diff] [blame] | 784 | if (binding.scope().isPresent() && binding.scope().get().equals(reusableScope(elements))) { |
| 785 | for (Resolver requestResolver : getResolverLineage().reverse()) { |
| 786 | // If a @Reusable binding was resolved in an ancestor, use that component. |
| dpb | a69ead5 | 2016-07-11 10:05:20 -0700 | [diff] [blame] | 787 | if (requestResolver.resolvedBindings.containsKey( |
| 788 | BindingKey.contribution(binding.key()))) { |
| dpb | 4cd56e8 | 2016-03-30 10:12:27 -0700 | [diff] [blame] | 789 | return Optional.of(requestResolver); |
| 790 | } |
| 791 | } |
| 792 | // If a @Reusable binding was not resolved in any ancestor, resolve it here. |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 793 | return Optional.empty(); |
| dpb | 4cd56e8 | 2016-03-30 10:12:27 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 796 | for (Resolver requestResolver : getResolverLineage().reverse()) { |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 797 | if (requestResolver.explicitBindingsSet.contains(binding) |
| 798 | || requestResolver.subcomponentDeclarations.containsKey(binding.key())) { |
| matvore | 36fc8bf | 2015-07-16 09:17:49 -0700 | [diff] [blame] | 799 | return Optional.of(requestResolver); |
| gak | bce6475 | 2015-05-08 12:25:38 -0700 | [diff] [blame] | 800 | } |
| 801 | } |
| dpb | f025531 | 2015-07-27 15:43:19 -0700 | [diff] [blame] | 802 | |
| gak | bce6475 | 2015-05-08 12:25:38 -0700 | [diff] [blame] | 803 | // look for scope separately. we do this for the case where @Singleton can appear twice |
| 804 | // in the †compatibility mode |
| beder | 3bdf32d | 2016-02-16 11:37:07 -0800 | [diff] [blame] | 805 | Optional<Scope> bindingScope = binding.scope(); |
| dpb | f025531 | 2015-07-27 15:43:19 -0700 | [diff] [blame] | 806 | if (bindingScope.isPresent()) { |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 807 | for (Resolver requestResolver : getResolverLineage().reverse()) { |
| beder | 3bdf32d | 2016-02-16 11:37:07 -0800 | [diff] [blame] | 808 | if (requestResolver.componentDescriptor.scopes().contains(bindingScope.get())) { |
| dpb | f025531 | 2015-07-27 15:43:19 -0700 | [diff] [blame] | 809 | return Optional.of(requestResolver); |
| 810 | } |
| gak | 959f5cd | 2015-01-29 12:42:09 -0800 | [diff] [blame] | 811 | } |
| 812 | } |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 813 | return Optional.empty(); |
| gak | 959f5cd | 2015-01-29 12:42:09 -0800 | [diff] [blame] | 814 | } |
| 815 | |
| gak | bce6475 | 2015-05-08 12:25:38 -0700 | [diff] [blame] | 816 | /** Returns the resolver lineage from parent to child. */ |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 817 | private ImmutableList<Resolver> getResolverLineage() { |
| 818 | List<Resolver> resolverList = Lists.newArrayList(); |
| 819 | for (Optional<Resolver> currentResolver = Optional.of(this); |
| gak | 959f5cd | 2015-01-29 12:42:09 -0800 | [diff] [blame] | 820 | currentResolver.isPresent(); |
| 821 | currentResolver = currentResolver.get().parentResolver) { |
| 822 | resolverList.add(currentResolver.get()); |
| 823 | } |
| 824 | return ImmutableList.copyOf(Lists.reverse(resolverList)); |
| 825 | } |
| 826 | |
| dpb | a262042 | 2015-08-15 10:06:54 -0700 | [diff] [blame] | 827 | /** |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 828 | * Returns the explicit {@link ContributionBinding}s that match the {@code key} from this and |
| 829 | * all ancestor resolvers. |
| dpb | a262042 | 2015-08-15 10:06:54 -0700 | [diff] [blame] | 830 | */ |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 831 | private ImmutableSet<ContributionBinding> getExplicitBindings(Key key) { |
| ronshapiro | d697327 | 2016-07-20 15:41:23 -0700 | [diff] [blame] | 832 | ImmutableSet.Builder<ContributionBinding> bindings = ImmutableSet.builder(); |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 833 | for (Resolver resolver : getResolverLineage()) { |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 834 | bindings.addAll(resolver.getLocalExplicitBindings(key)); |
| gak | 959f5cd | 2015-01-29 12:42:09 -0800 | [diff] [blame] | 835 | } |
| ronshapiro | d697327 | 2016-07-20 15:41:23 -0700 | [diff] [blame] | 836 | return bindings.build(); |
| gak | 959f5cd | 2015-01-29 12:42:09 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| dpb | 51bf8b4 | 2015-12-18 09:59:56 -0800 | [diff] [blame] | 839 | /** |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 840 | * Returns the explicit {@link ContributionBinding}s that match the {@code key} from this |
| 841 | * resolver. |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 842 | */ |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 843 | private ImmutableSet<ContributionBinding> getLocalExplicitBindings(Key key) { |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 844 | return new ImmutableSet.Builder<ContributionBinding>() |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 845 | .addAll(explicitBindings.get(key)) |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 846 | .addAll( |
| 847 | createDelegateBindings( |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 848 | delegateDeclarations.get(keyFactory.convertToDelegateKey(key)))) |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 849 | .build(); |
| 850 | } |
| 851 | |
| 852 | /** |
| dpb | 90263a5 | 2016-07-13 07:52:41 -0700 | [diff] [blame] | 853 | * Returns the explicit multibinding contributions that contribute to the map or set requested |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 854 | * by {@code key} from this and all ancestor resolvers. |
| dpb | db24072 | 2016-01-25 14:06:41 -0800 | [diff] [blame] | 855 | */ |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 856 | private ImmutableSet<ContributionBinding> getExplicitMultibindings(Key key) { |
| ronshapiro | d697327 | 2016-07-20 15:41:23 -0700 | [diff] [blame] | 857 | ImmutableSet.Builder<ContributionBinding> multibindings = ImmutableSet.builder(); |
| dpb | db24072 | 2016-01-25 14:06:41 -0800 | [diff] [blame] | 858 | for (Resolver resolver : getResolverLineage()) { |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 859 | multibindings.addAll(resolver.getLocalExplicitMultibindings(key)); |
| 860 | } |
| 861 | return multibindings.build(); |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * Returns the explicit multibinding contributions that contribute to the map or set requested |
| 866 | * by {@code key} from this resolver. |
| 867 | */ |
| 868 | private ImmutableSet<ContributionBinding> getLocalExplicitMultibindings(Key key) { |
| 869 | ImmutableSet.Builder<ContributionBinding> multibindings = ImmutableSet.builder(); |
| 870 | multibindings.addAll(explicitMultibindings.get(key)); |
| 871 | if (!MapType.isMap(key) || MapType.from(key).valuesAreFrameworkType()) { |
| 872 | // There are no @Binds @IntoMap delegate declarations for Map<K, V> requests. All |
| 873 | // @IntoMap requests must be for Map<K, Framework<V>>. |
| 874 | multibindings.addAll( |
| 875 | createDelegateBindings( |
| 876 | delegateMultibindingDeclarations.get(keyFactory.convertToDelegateKey(key)))); |
| dpb | db24072 | 2016-01-25 14:06:41 -0800 | [diff] [blame] | 877 | } |
| ronshapiro | d697327 | 2016-07-20 15:41:23 -0700 | [diff] [blame] | 878 | return multibindings.build(); |
| dpb | db24072 | 2016-01-25 14:06:41 -0800 | [diff] [blame] | 879 | } |
| 880 | |
| dpb | db24072 | 2016-01-25 14:06:41 -0800 | [diff] [blame] | 881 | /** |
| dpb | 51bf8b4 | 2015-12-18 09:59:56 -0800 | [diff] [blame] | 882 | * Returns the {@link MultibindingDeclaration}s that match the {@code key} from this and all |
| 883 | * ancestor resolvers. |
| 884 | */ |
| 885 | private ImmutableSet<MultibindingDeclaration> getMultibindingDeclarations(Key key) { |
| 886 | ImmutableSet.Builder<MultibindingDeclaration> multibindingDeclarations = |
| 887 | ImmutableSet.builder(); |
| 888 | for (Resolver resolver : getResolverLineage()) { |
| 889 | multibindingDeclarations.addAll(resolver.multibindingDeclarations.get(key)); |
| 890 | } |
| 891 | return multibindingDeclarations.build(); |
| 892 | } |
| 893 | |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 894 | /** |
| ronshapiro | fbb6840 | 2016-08-31 06:44:45 -0700 | [diff] [blame] | 895 | * Returns the {@link SubcomponentDeclaration}s that match the {@code key} from this and all |
| 896 | * ancestor resolvers. |
| 897 | */ |
| 898 | private ImmutableSet<SubcomponentDeclaration> getSubcomponentDeclarations(Key key) { |
| 899 | ImmutableSet.Builder<SubcomponentDeclaration> subcomponentDeclarations = |
| 900 | ImmutableSet.builder(); |
| 901 | for (Resolver resolver : getResolverLineage()) { |
| 902 | subcomponentDeclarations.addAll(resolver.subcomponentDeclarations.get(key)); |
| 903 | } |
| 904 | return subcomponentDeclarations.build(); |
| 905 | } |
| 906 | /** |
| dpb | d8d950a | 2016-08-30 09:24:37 -0700 | [diff] [blame] | 907 | * Returns the {@link OptionalBindingDeclaration}s that match the {@code key} from this and |
| 908 | * all ancestor resolvers. |
| 909 | */ |
| 910 | private ImmutableSet<OptionalBindingDeclaration> getOptionalBindingDeclarations(Key key) { |
| 911 | Optional<Key> unwrapped = keyFactory.unwrapOptional(key); |
| 912 | if (!unwrapped.isPresent()) { |
| 913 | return ImmutableSet.of(); |
| 914 | } |
| 915 | ImmutableSet.Builder<OptionalBindingDeclaration> declarations = ImmutableSet.builder(); |
| 916 | for (Resolver resolver : getResolverLineage()) { |
| 917 | declarations.addAll(resolver.optionalBindingDeclarations.get(unwrapped.get())); |
| 918 | } |
| 919 | return declarations.build(); |
| 920 | } |
| 921 | |
| gak | 29aeaca | 2015-02-05 16:48:47 -0800 | [diff] [blame] | 922 | private Optional<ResolvedBindings> getPreviouslyResolvedBindings( |
| 923 | final BindingKey bindingKey) { |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 924 | Optional<ResolvedBindings> result = Optional.ofNullable(resolvedBindings.get(bindingKey)); |
| gak | 29aeaca | 2015-02-05 16:48:47 -0800 | [diff] [blame] | 925 | if (result.isPresent()) { |
| 926 | return result; |
| 927 | } else if (parentResolver.isPresent()) { |
| 928 | return parentResolver.get().getPreviouslyResolvedBindings(bindingKey); |
| 929 | } else { |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 930 | return Optional.empty(); |
| gak | 29aeaca | 2015-02-05 16:48:47 -0800 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 934 | void resolve(BindingKey bindingKey) { |
| dpb | f025531 | 2015-07-27 15:43:19 -0700 | [diff] [blame] | 935 | // If we find a cycle, stop resolving. The original request will add it with all of the |
| 936 | // other resolved deps. |
| 937 | if (cycleStack.contains(bindingKey)) { |
| beder | faaa969 | 2015-01-09 17:34:53 -0800 | [diff] [blame] | 938 | return; |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| dpb | f025531 | 2015-07-27 15:43:19 -0700 | [diff] [blame] | 941 | // If the binding was previously resolved in this (sub)component, don't resolve it again. |
| 942 | if (resolvedBindings.containsKey(bindingKey)) { |
| 943 | return; |
| 944 | } |
| 945 | |
| dpb | 938b752 | 2016-02-23 07:55:08 -0800 | [diff] [blame] | 946 | /* If the binding was previously resolved in a supercomponent, then we may be able to avoid |
| 947 | * resolving it here and just depend on the supercomponent resolution. |
| 948 | * |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 949 | * 1. If it depends transitively on multibinding contributions or optional bindings with |
| 950 | * bindings from this subcomponent, then we have to resolve it in this subcomponent so |
| 951 | * that it sees the local bindings. |
| dpb | 938b752 | 2016-02-23 07:55:08 -0800 | [diff] [blame] | 952 | * |
| 953 | * 2. If there are any explicit bindings in this component, they may conflict with those in |
| 954 | * the supercomponent, so resolve them here so that conflicts can be caught. |
| 955 | */ |
| dpb | 2df89f9 | 2016-05-24 06:53:48 -0700 | [diff] [blame] | 956 | if (getPreviouslyResolvedBindings(bindingKey).isPresent()) { |
| dpb | 407b7bd | 2016-02-29 09:05:39 -0800 | [diff] [blame] | 957 | /* Resolve in the parent in case there are multibinding contributions or conflicts in some |
| 958 | * component between this one and the previously-resolved one. */ |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 959 | parentResolver.get().resolve(bindingKey); |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 960 | if (!new LocalDependencyChecker().dependsOnLocalBindings(bindingKey) |
| 961 | && getLocalExplicitBindings(bindingKey.key()).isEmpty()) { |
| dpb | 2df89f9 | 2016-05-24 06:53:48 -0700 | [diff] [blame] | 962 | /* Cache the inherited parent component's bindings in case resolving at the parent found |
| 963 | * bindings in some component between this one and the previously-resolved one. */ |
| 964 | ResolvedBindings inheritedBindings = |
| 965 | getPreviouslyResolvedBindings(bindingKey).get().asInheritedIn(componentDescriptor); |
| 966 | resolvedBindings.put(bindingKey, inheritedBindings); |
| 967 | return; |
| 968 | } |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| beder | 3e950f3 | 2014-12-19 17:48:47 -0800 | [diff] [blame] | 971 | cycleStack.push(bindingKey); |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 972 | try { |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 973 | ResolvedBindings bindings = lookUpBindings(bindingKey); |
| gak | d296d89 | 2015-03-10 23:34:03 -0700 | [diff] [blame] | 974 | for (Binding binding : bindings.ownedBindings()) { |
| gak | 75b9b72 | 2016-09-29 15:48:40 -0700 | [diff] [blame] | 975 | for (DependencyRequest dependency : binding.dependencies()) { |
| gak | f53c3f9 | 2016-08-17 09:55:56 -0700 | [diff] [blame] | 976 | resolve(dependency.bindingKey()); |
| gak | 959f5cd | 2015-01-29 12:42:09 -0800 | [diff] [blame] | 977 | } |
| beder | faaa969 | 2015-01-09 17:34:53 -0800 | [diff] [blame] | 978 | } |
| gak | d296d89 | 2015-03-10 23:34:03 -0700 | [diff] [blame] | 979 | resolvedBindings.put(bindingKey, bindings); |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 980 | } finally { |
| 981 | cycleStack.pop(); |
| 982 | } |
| 983 | } |
| gak | 78ac368 | 2015-03-30 15:48:00 -0700 | [diff] [blame] | 984 | |
| 985 | ImmutableMap<BindingKey, ResolvedBindings> getResolvedBindings() { |
| 986 | ImmutableMap.Builder<BindingKey, ResolvedBindings> resolvedBindingsBuilder = |
| 987 | ImmutableMap.builder(); |
| 988 | resolvedBindingsBuilder.putAll(resolvedBindings); |
| 989 | if (parentResolver.isPresent()) { |
| dpb | f025531 | 2015-07-27 15:43:19 -0700 | [diff] [blame] | 990 | Collection<ResolvedBindings> bindingsResolvedInParent = |
| 991 | Maps.difference(parentResolver.get().getResolvedBindings(), resolvedBindings) |
| 992 | .entriesOnlyOnLeft() |
| 993 | .values(); |
| 994 | for (ResolvedBindings resolvedInParent : bindingsResolvedInParent) { |
| 995 | resolvedBindingsBuilder.put( |
| gak | 924b5f4 | 2015-08-25 13:27:02 -0700 | [diff] [blame] | 996 | resolvedInParent.bindingKey(), |
| 997 | resolvedInParent.asInheritedIn(componentDescriptor)); |
| gak | 78ac368 | 2015-03-30 15:48:00 -0700 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | return resolvedBindingsBuilder.build(); |
| 1001 | } |
| gak | 3a5e81e | 2015-08-25 16:22:26 -0700 | [diff] [blame] | 1002 | |
| 1003 | ImmutableSet<ModuleDescriptor> getInheritedModules() { |
| 1004 | return parentResolver.isPresent() |
| 1005 | ? Sets.union( |
| 1006 | parentResolver.get().getInheritedModules(), |
| 1007 | parentResolver.get().componentDescriptor.transitiveModules()) |
| 1008 | .immutableCopy() |
| 1009 | : ImmutableSet.<ModuleDescriptor>of(); |
| 1010 | } |
| 1011 | |
| 1012 | ImmutableSet<ModuleDescriptor> getOwnedModules() { |
| 1013 | return Sets.difference(componentDescriptor.transitiveModules(), getInheritedModules()) |
| 1014 | .immutableCopy(); |
| 1015 | } |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1016 | |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 1017 | private final class LocalDependencyChecker { |
| dpb | 42e4a48 | 2016-01-21 12:23:40 -0800 | [diff] [blame] | 1018 | private final Set<Object> cycleChecker = new HashSet<>(); |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1019 | |
| 1020 | /** |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 1021 | * Returns {@code true} if any of the bindings resolved for {@code bindingKey} are |
| 1022 | * multibindings with contributions declared within this component's modules or optional |
| 1023 | * bindings with present values declared within this component's modules, or if any of its |
| 1024 | * unscoped dependencies depend on such bindings. |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1025 | * |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 1026 | * <p>We don't care about scoped dependencies because they will never depend on bindings |
| 1027 | * from subcomponents. |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1028 | * |
| 1029 | * @throws IllegalArgumentException if {@link #getPreviouslyResolvedBindings(BindingKey)} is |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 1030 | * empty |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1031 | */ |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 1032 | boolean dependsOnLocalBindings(BindingKey bindingKey) { |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1033 | // Don't recur infinitely if there are valid cycles in the dependency graph. |
| Christian Gruber | 40260f1 | 2016-06-14 08:22:11 -0700 | [diff] [blame] | 1034 | // http://b/23032377 |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1035 | if (!cycleChecker.add(bindingKey)) { |
| 1036 | return false; |
| 1037 | } |
| ronshapiro | ac6dd2b | 2017-08-04 12:48:07 -0700 | [diff] [blame] | 1038 | return reentrantComputeIfAbsent( |
| 1039 | bindingKeyDependsOnLocalBindingsCache, |
| 1040 | bindingKey, |
| 1041 | this::dependsOnLocalBindingsUncached); |
| dpb | 9563a02 | 2017-06-05 08:39:15 -0700 | [diff] [blame] | 1042 | } |
| gak | 6624110 | 2016-09-13 12:23:00 -0700 | [diff] [blame] | 1043 | |
| dpb | 9563a02 | 2017-06-05 08:39:15 -0700 | [diff] [blame] | 1044 | private boolean dependsOnLocalBindingsUncached(BindingKey bindingKey) { |
| 1045 | checkArgument( |
| 1046 | getPreviouslyResolvedBindings(bindingKey).isPresent(), |
| 1047 | "no previously resolved bindings in %s for %s", |
| 1048 | Resolver.this, |
| 1049 | bindingKey); |
| 1050 | ResolvedBindings previouslyResolvedBindings = |
| 1051 | getPreviouslyResolvedBindings(bindingKey).get(); |
| 1052 | if (hasLocalMultibindingContributions(previouslyResolvedBindings) |
| 1053 | || hasLocallyPresentOptionalBinding(previouslyResolvedBindings)) { |
| 1054 | return true; |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1055 | } |
| dpb | 9563a02 | 2017-06-05 08:39:15 -0700 | [diff] [blame] | 1056 | |
| 1057 | for (Binding binding : previouslyResolvedBindings.bindings()) { |
| 1058 | if (dependsOnLocalBindings(binding)) { |
| 1059 | return true; |
| 1060 | } |
| 1061 | } |
| 1062 | return false; |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /** |
| dpb | 4cd56e8 | 2016-03-30 10:12:27 -0700 | [diff] [blame] | 1066 | * Returns {@code true} if {@code binding} is unscoped (or has {@link Reusable @Reusable} |
| 1067 | * scope) and depends on multibindings with contributions declared within this component's |
| 1068 | * modules, or if any of its unscoped or {@link Reusable @Reusable} scoped dependencies |
| 1069 | * depend on such local multibindings. |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1070 | * |
| dpb | 4cd56e8 | 2016-03-30 10:12:27 -0700 | [diff] [blame] | 1071 | * <p>We don't care about non-reusable scoped dependencies because they will never depend on |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1072 | * multibindings with contributions from subcomponents. |
| 1073 | */ |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 1074 | boolean dependsOnLocalBindings(Binding binding) { |
| dpb | 42e4a48 | 2016-01-21 12:23:40 -0800 | [diff] [blame] | 1075 | if (!cycleChecker.add(binding)) { |
| 1076 | return false; |
| 1077 | } |
| ronshapiro | ac6dd2b | 2017-08-04 12:48:07 -0700 | [diff] [blame] | 1078 | return reentrantComputeIfAbsent( |
| 1079 | bindingDependsOnLocalBindingsCache, binding, this::dependsOnLocalBindingsUncached); |
| dpb | 9563a02 | 2017-06-05 08:39:15 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | private boolean dependsOnLocalBindingsUncached(Binding binding) { |
| 1083 | if ((!binding.scope().isPresent() |
| 1084 | || binding.scope().get().equals(reusableScope(elements))) |
| 1085 | // TODO(beder): Figure out what happens with production subcomponents. |
| 1086 | && !binding.bindingType().equals(BindingType.PRODUCTION)) { |
| 1087 | for (DependencyRequest dependency : binding.dependencies()) { |
| 1088 | if (dependsOnLocalBindings(dependency.bindingKey())) { |
| 1089 | return true; |
| 1090 | } |
| 1091 | } |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1092 | } |
| dpb | 9563a02 | 2017-06-05 08:39:15 -0700 | [diff] [blame] | 1093 | return false; |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
| dpb | 0835031 | 2016-09-27 10:12:41 -0700 | [diff] [blame] | 1096 | /** |
| 1097 | * Returns {@code true} if {@code resolvedBindings} contains a synthetic multibinding with |
| 1098 | * at least one contribution declared within this component's modules. |
| 1099 | */ |
| 1100 | private boolean hasLocalMultibindingContributions(ResolvedBindings resolvedBindings) { |
| 1101 | return resolvedBindings |
| 1102 | .contributionBindings() |
| 1103 | .stream() |
| 1104 | .map(ContributionBinding::bindingKind) |
| 1105 | .anyMatch(SYNTHETIC_MULTIBOUND_KINDS::contains) |
| 1106 | && !getLocalExplicitMultibindings(resolvedBindings.key()).isEmpty(); |
| 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * Returns {@code true} if {@code resolvedBindings} contains a synthetic optional binding |
| 1111 | * for which there is an explicit present binding in this component. |
| 1112 | */ |
| 1113 | private boolean hasLocallyPresentOptionalBinding(ResolvedBindings resolvedBindings) { |
| 1114 | return resolvedBindings |
| 1115 | .contributionBindings() |
| 1116 | .stream() |
| 1117 | .map(ContributionBinding::bindingKind) |
| 1118 | .anyMatch(isEqual(SYNTHETIC_OPTIONAL_BINDING)) |
| 1119 | && !getLocalExplicitBindings(keyFactory.unwrapOptional(resolvedBindings.key()).get()) |
| 1120 | .isEmpty(); |
| dpb | 778d522 | 2015-12-22 08:33:07 -0800 | [diff] [blame] | 1121 | } |
| 1122 | } |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 1123 | } |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 1124 | |
| 1125 | /** |
| dpb | 90263a5 | 2016-07-13 07:52:41 -0700 | [diff] [blame] | 1126 | * A multimap of those {@code declarations} that are multibinding contribution declarations, |
| 1127 | * indexed by the key of the set or map to which they contribute. |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 1128 | */ |
| dpb | 7edcd4d | 2016-06-21 07:52:39 -0700 | [diff] [blame] | 1129 | static <T extends HasKey> |
| dpb | 90263a5 | 2016-07-13 07:52:41 -0700 | [diff] [blame] | 1130 | ImmutableSetMultimap<Key, T> multibindingContributionsByMultibindingKey( |
| 1131 | Iterable<T> declarations) { |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 1132 | ImmutableSetMultimap.Builder<Key, T> builder = ImmutableSetMultimap.builder(); |
| dpb | 90263a5 | 2016-07-13 07:52:41 -0700 | [diff] [blame] | 1133 | for (T declaration : declarations) { |
| 1134 | if (declaration.key().multibindingContributionIdentifier().isPresent()) { |
| 1135 | builder.put(declaration.key().withoutMultibindingContributionIdentifier(), declaration); |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | return builder.build(); |
| 1139 | } |
| gak | 16e0cd4 | 2014-09-18 09:24:04 -0700 | [diff] [blame] | 1140 | } |
| 1141 | } |