Skip to content

Commit f53c3f9

Browse files
gk5885ronshapiro
authored andcommitted
Change a lot of the method signatures in BindingGraph and the Binding subtypes to only require Key and BindingKey instead of DependencyRequest because they didn't actually need the full request.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=130530677
1 parent b1b536b commit f53c3f9

File tree

4 files changed

+73
-87
lines changed

4 files changed

+73
-87
lines changed

compiler/src/main/java/dagger/internal/codegen/BindingGraph.java

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ && isComponentProductionMethod(elements, method)
252252
for (ComponentMethodDescriptor componentMethod : componentDescriptor.componentMethods()) {
253253
Optional<DependencyRequest> componentMethodRequest = componentMethod.dependencyRequest();
254254
if (componentMethodRequest.isPresent()) {
255-
requestResolver.resolve(componentMethodRequest.get());
255+
requestResolver.resolve(componentMethodRequest.get().bindingKey());
256256
}
257257
}
258258

@@ -316,35 +316,33 @@ private final class Resolver {
316316
}
317317

318318
/**
319-
* Returns the bindings that satisfy a given dependency request.
319+
* Returns the bindings for the given {@link BindingKey}.
320320
*
321321
* <p>For {@link BindingKey.Kind#CONTRIBUTION} requests, returns all of:
322+
*
322323
* <ul>
323324
* <li>All explicit bindings for:
324325
* <ul>
325326
* <li>the requested key
326327
* <li>{@code Set<T>} if the requested key's type is {@code Set<Produced<T>>}
327-
* <li>{@code Map<K, Provider<V>>} if the requested key's type is
328-
* {@code Map<K, Producer<V>>}.
328+
* <li>{@code Map<K, Provider<V>>} if the requested key's type is {@code Map<K,
329+
* Producer<V>>}.
329330
* </ul>
330331
*
331332
* <li>A synthetic binding that depends on {@code Map<K, Producer<V>>} if the requested key's
332-
* type is {@code Map<K, V>} and there are some explicit bindings for
333-
* {@code Map<K, Producer<V>>}.
334-
*
333+
* type is {@code Map<K, V>} and there are some explicit bindings for {@code Map<K,
334+
* Producer<V>>}.
335335
* <li>A synthetic binding that depends on {@code Map<K, Provider<V>>} if the requested key's
336-
* type is {@code Map<K, V>} and there are some explicit bindings for
337-
* {@code Map<K, Provider<V>>} but no explicit bindings for {@code Map<K, Producer<V>>}.
338-
*
336+
* type is {@code Map<K, V>} and there are some explicit bindings for {@code Map<K,
337+
* Provider<V>>} but no explicit bindings for {@code Map<K, Producer<V>>}.
339338
* <li>An implicit {@link Inject @Inject}-annotated constructor binding if there is one and
340339
* there are no explicit bindings or synthetic bindings.
341340
* </ul>
342341
*
343-
* <p>For {@link BindingKey.Kind#MEMBERS_INJECTION} requests, returns the
344-
* {@link MembersInjectionBinding} for the type.
342+
* <p>For {@link BindingKey.Kind#MEMBERS_INJECTION} requests, returns the {@link
343+
* MembersInjectionBinding} for the type.
345344
*/
346-
ResolvedBindings lookUpBindings(DependencyRequest request) {
347-
BindingKey bindingKey = request.bindingKey();
345+
ResolvedBindings lookUpBindings(BindingKey bindingKey) {
348346
Key requestKey = bindingKey.key();
349347
switch (bindingKey.kind()) {
350348
case CONTRIBUTION:
@@ -365,9 +363,10 @@ ResolvedBindings lookUpBindings(DependencyRequest request) {
365363
ImmutableSet<MultibindingDeclaration> multibindingDeclarations =
366364
multibindingDeclarationsBuilder.build();
367365

368-
contributionBindings.addAll(syntheticMapOfValuesBinding(request).asSet());
366+
contributionBindings.addAll(syntheticMapOfValuesBinding(bindingKey.key()).asSet());
369367
contributionBindings.addAll(
370-
syntheticMultibinding(request, multibindingContributions, multibindingDeclarations)
368+
syntheticMultibinding(
369+
bindingKey.key(), multibindingContributions, multibindingDeclarations)
371370
.asSet());
372371

373372
/* If there are no bindings, add the implicit @Inject-constructed binding if there is
@@ -380,7 +379,8 @@ ResolvedBindings lookUpBindings(DependencyRequest request) {
380379
return ResolvedBindings.forContributionBindings(
381380
bindingKey,
382381
componentDescriptor,
383-
indexBindingsByOwningComponent(request, ImmutableSet.copyOf(contributionBindings)),
382+
indexBindingsByOwningComponent(
383+
bindingKey, ImmutableSet.copyOf(contributionBindings)),
384384
multibindingDeclarations);
385385

386386
case MEMBERS_INJECTION:
@@ -407,32 +407,29 @@ private Iterable<Key> keysMatchingRequest(Key requestKey) {
407407
}
408408

409409
/**
410-
* If {@code request} is for a {@code Map<K, V>} or {@code Map<K, Produced<V>>}, and there are
411-
* any multibinding contributions or declarations that apply to that map, returns a synthetic
412-
* binding for the {@code request} that depends on an {@linkplain
413-
* #syntheticMultibinding(DependencyRequest, Iterable, Iterable) underlying synthetic
414-
* multibinding}.
410+
* If {@code key} is a {@code Map<K, V>} or {@code Map<K, Produced<V>>}, and there are any
411+
* multibinding contributions or declarations that apply to that map, returns a synthetic
412+
* binding for the {@code key} that depends on an {@linkplain #syntheticMultibinding(Key,
413+
* Iterable, Iterable) underlying synthetic multibinding}.
415414
*
416415
* <p>The returned binding has the same {@link BindingType} as the underlying synthetic
417416
* multibinding.
418417
*/
419-
private Optional<ContributionBinding> syntheticMapOfValuesBinding(
420-
final DependencyRequest request) {
418+
private Optional<ContributionBinding> syntheticMapOfValuesBinding(final Key key) {
421419
return syntheticMultibinding(
422-
request,
423-
multibindingContributionsForValueMap(request.key()),
424-
multibindingDeclarationsForValueMap(request.key()))
420+
key,
421+
multibindingContributionsForValueMap(key),
422+
multibindingDeclarationsForValueMap(key))
425423
.transform(
426424
new Function<ContributionBinding, ContributionBinding>() {
427425
@Override
428426
public ContributionBinding apply(ContributionBinding syntheticMultibinding) {
429427
switch (syntheticMultibinding.bindingType()) {
430428
case PROVISION:
431-
return provisionBindingFactory.syntheticMapOfValuesBinding(request);
429+
return provisionBindingFactory.syntheticMapOfValuesBinding(key);
432430

433431
case PRODUCTION:
434-
return productionBindingFactory.syntheticMapOfValuesOrProducedBinding(
435-
request);
432+
return productionBindingFactory.syntheticMapOfValuesOrProducedBinding(key);
436433

437434
default:
438435
throw new VerifyException(syntheticMultibinding.toString());
@@ -495,17 +492,17 @@ public Iterable<MultibindingDeclaration> apply(Key key) {
495492
* Otherwise, returns a {@link ProvisionBinding}.
496493
*/
497494
private Optional<? extends ContributionBinding> syntheticMultibinding(
498-
DependencyRequest request,
495+
Key key,
499496
Iterable<ContributionBinding> multibindingContributions,
500497
Iterable<MultibindingDeclaration> multibindingDeclarations) {
501498
if (isEmpty(multibindingContributions) && isEmpty(multibindingDeclarations)) {
502499
return Optional.absent();
503-
} else if (multibindingsRequireProduction(multibindingContributions, request.key())) {
500+
} else if (multibindingsRequireProduction(multibindingContributions, key)) {
504501
return Optional.of(
505-
productionBindingFactory.syntheticMultibinding(request, multibindingContributions));
502+
productionBindingFactory.syntheticMultibinding(key, multibindingContributions));
506503
} else {
507504
return Optional.of(
508-
provisionBindingFactory.syntheticMultibinding(request, multibindingContributions));
505+
provisionBindingFactory.syntheticMultibinding(key, multibindingContributions));
509506
}
510507
}
511508

@@ -539,7 +536,8 @@ private ImmutableSet<ContributionBinding> createDelegateBindings(
539536
* delegate key.
540537
*/
541538
private ContributionBinding createDelegateBinding(DelegateDeclaration delegateDeclaration) {
542-
ResolvedBindings resolvedDelegate = lookUpBindings(delegateDeclaration.delegateRequest());
539+
ResolvedBindings resolvedDelegate =
540+
lookUpBindings(delegateDeclaration.delegateRequest().bindingKey());
543541
if (resolvedDelegate.contributionBindings().isEmpty()) {
544542
// This is guaranteed to result in a missing binding error, so it doesn't matter if the
545543
// binding is a Provision or Production, except if it is a @IntoMap method, in which
@@ -572,11 +570,11 @@ private ContributionBinding createDelegateBinding(DelegateDeclaration delegateDe
572570

573571
private ImmutableSetMultimap<ComponentDescriptor, ContributionBinding>
574572
indexBindingsByOwningComponent(
575-
DependencyRequest request, Iterable<? extends ContributionBinding> bindings) {
573+
BindingKey bindingKey, Iterable<? extends ContributionBinding> bindings) {
576574
ImmutableSetMultimap.Builder<ComponentDescriptor, ContributionBinding> index =
577575
ImmutableSetMultimap.builder();
578576
for (ContributionBinding binding : bindings) {
579-
index.put(getOwningComponent(request, binding), binding);
577+
index.put(getOwningComponent(bindingKey, binding), binding);
580578
}
581579
return index.build();
582580
}
@@ -588,31 +586,31 @@ private ContributionBinding createDelegateBinding(DelegateDeclaration delegateDe
588586
* multibinding contributions in this component, returns this component.
589587
*
590588
* <p>Otherwise, resolves {@code request} in this component's parent in order to resolve any
591-
* multibinding contributions in the parent, and returns the parent-resolved
592-
* {@link ResolvedBindings#owningComponent(ContributionBinding)}.
589+
* multibinding contributions in the parent, and returns the parent-resolved {@link
590+
* ResolvedBindings#owningComponent(ContributionBinding)}.
593591
*/
594592
private ComponentDescriptor getOwningComponent(
595-
DependencyRequest request, ContributionBinding binding) {
596-
if (isResolvedInParent(request, binding)
593+
BindingKey bindingKey, ContributionBinding binding) {
594+
if (isResolvedInParent(bindingKey, binding)
597595
&& !new MultibindingDependencies().dependsOnLocalMultibindings(binding)) {
598596
ResolvedBindings parentResolvedBindings =
599-
parentResolver.get().resolvedBindings.get(request.bindingKey());
597+
parentResolver.get().resolvedBindings.get(bindingKey);
600598
return parentResolvedBindings.owningComponent(binding);
601599
} else {
602600
return componentDescriptor;
603601
}
604602
}
605603

606604
/**
607-
* Returns {@code true} if {@code binding} is owned by an ancestor. If so,
608-
* {@linkplain #resolve(DependencyRequest) resolves} the request in this component's parent.
609-
* Don't resolve directly in the owning component in case it depends on multibindings in any
610-
* of its descendants.
605+
* Returns {@code true} if {@code binding} is owned by an ancestor. If so, {@linkplain
606+
* #resolve resolves} the {@link BindingKey} in this component's parent. Don't resolve
607+
* directly in the owning component in case it depends on multibindings in any of its
608+
* descendants.
611609
*/
612-
private boolean isResolvedInParent(DependencyRequest request, ContributionBinding binding) {
610+
private boolean isResolvedInParent(BindingKey bindingKey, ContributionBinding binding) {
613611
Optional<Resolver> owningResolver = getOwningResolver(binding);
614612
if (owningResolver.isPresent() && !owningResolver.get().equals(this)) {
615-
parentResolver.get().resolve(request);
613+
parentResolver.get().resolve(bindingKey);
616614
return true;
617615
} else {
618616
return false;
@@ -724,9 +722,7 @@ private Optional<ResolvedBindings> getPreviouslyResolvedBindings(
724722
}
725723
}
726724

727-
void resolve(DependencyRequest request) {
728-
BindingKey bindingKey = request.bindingKey();
729-
725+
void resolve(BindingKey bindingKey) {
730726
// If we find a cycle, stop resolving. The original request will add it with all of the
731727
// other resolved deps.
732728
if (cycleStack.contains(bindingKey)) {
@@ -750,7 +746,7 @@ void resolve(DependencyRequest request) {
750746
if (getPreviouslyResolvedBindings(bindingKey).isPresent()) {
751747
/* Resolve in the parent in case there are multibinding contributions or conflicts in some
752748
* component between this one and the previously-resolved one. */
753-
parentResolver.get().resolve(request);
749+
parentResolver.get().resolve(bindingKey);
754750
if (!new MultibindingDependencies().dependsOnLocalMultibindings(bindingKey)
755751
&& getExplicitBindings(bindingKey.key()).isEmpty()) {
756752
/* Cache the inherited parent component's bindings in case resolving at the parent found
@@ -764,10 +760,10 @@ && getExplicitBindings(bindingKey.key()).isEmpty()) {
764760

765761
cycleStack.push(bindingKey);
766762
try {
767-
ResolvedBindings bindings = lookUpBindings(request);
763+
ResolvedBindings bindings = lookUpBindings(bindingKey);
768764
for (Binding binding : bindings.ownedBindings()) {
769765
for (DependencyRequest dependency : binding.implicitDependencies()) {
770-
resolve(dependency);
766+
resolve(dependency.bindingKey());
771767
}
772768
}
773769
resolvedBindings.put(bindingKey, bindings);

compiler/src/main/java/dagger/internal/codegen/ContributionBinding.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ enum Kind {
134134
FUTURE_PRODUCTION,
135135

136136
/**
137-
* A production method on a production component's
138-
* {@linkplain ProductionComponent#dependencies() dependency} that returns a
137+
* A production method on a production component's {@linkplain
138+
* dagger.producers.ProductionComponent#dependencies()} dependency} that returns a
139139
* {@link ListenableFuture}. Methods on production component dependencies that don't return a
140140
* {@link ListenableFuture} are considered {@linkplain #PROVISION provision bindings}.
141141
*/
@@ -149,18 +149,15 @@ enum Kind {
149149
Predicates.in(immutableEnumSet(SYNTHETIC_MULTIBOUND_SET, SYNTHETIC_MULTIBOUND_MAP));
150150

151151
/**
152-
* {@link #SYNTHETIC_MULTIBOUND_SET} or {@link #SYNTHETIC_MULTIBOUND_MAP}, depending on the
153-
* request's key.
152+
* {@link #SYNTHETIC_MULTIBOUND_SET} or {@link #SYNTHETIC_MULTIBOUND_MAP}, depending on the key.
154153
*/
155-
static Kind forMultibindingRequest(DependencyRequest request) {
156-
Key key = request.key();
154+
static Kind forMultibindingKey(Key key) {
157155
if (SetType.isSet(key)) {
158156
return SYNTHETIC_MULTIBOUND_SET;
159157
} else if (MapType.isMap(key)) {
160158
return SYNTHETIC_MULTIBOUND_MAP;
161159
} else {
162-
throw new IllegalArgumentException(
163-
String.format("request is not for a set or map: %s", request));
160+
throw new IllegalArgumentException(String.format("key is not for a set or map: %s", key));
164161
}
165162
}
166163
}

compiler/src/main/java/dagger/internal/codegen/ProductionBinding.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,19 @@ ProductionBinding forProducesMethod(
151151
* A synthetic binding of {@code Map<K, V>} or {@code Map<K, Produced<V>>} that depends on
152152
* {@code Map<K, Producer<V>>}.
153153
*/
154-
ProductionBinding syntheticMapOfValuesOrProducedBinding(
155-
DependencyRequest requestForMapOfValuesOrProduced) {
156-
checkNotNull(requestForMapOfValuesOrProduced);
154+
ProductionBinding syntheticMapOfValuesOrProducedBinding(Key mapOfValuesOrProducedKey) {
155+
checkNotNull(mapOfValuesOrProducedKey);
157156
Optional<Key> mapOfProducersKey =
158-
keyFactory.implicitMapProducerKeyFrom(requestForMapOfValuesOrProduced.key());
157+
keyFactory.implicitMapProducerKeyFrom(mapOfValuesOrProducedKey);
159158
checkArgument(
160159
mapOfProducersKey.isPresent(),
161-
"%s is not for a Map<K, V> or Map<K, Produced<V>>",
162-
requestForMapOfValuesOrProduced);
160+
"%s is not a key for of Map<K, V> or Map<K, Produced<V>>",
161+
mapOfValuesOrProducedKey);
163162
DependencyRequest requestForMapOfProducers =
164163
dependencyRequestFactory.forImplicitMapBinding(mapOfProducersKey.get());
165164
return ProductionBinding.builder()
166165
.contributionType(ContributionType.UNIQUE)
167-
.key(requestForMapOfValuesOrProduced.key())
166+
.key(mapOfValuesOrProducedKey)
168167
.dependencies(requestForMapOfProducers)
169168
.bindingKind(Kind.SYNTHETIC_MAP)
170169
.build();
@@ -177,13 +176,13 @@ ProductionBinding syntheticMapOfValuesOrProducedBinding(
177176
* <p>Note that these could be set multibindings or map multibindings.
178177
*/
179178
ProductionBinding syntheticMultibinding(
180-
DependencyRequest request, Iterable<ContributionBinding> multibindingContributions) {
179+
Key key, Iterable<ContributionBinding> multibindingContributions) {
181180
return ProductionBinding.builder()
182181
.contributionType(ContributionType.UNIQUE)
183-
.key(request.key())
182+
.key(key)
184183
.dependencies(
185184
dependencyRequestFactory.forMultibindingContributions(multibindingContributions))
186-
.bindingKind(Kind.forMultibindingRequest(request))
185+
.bindingKind(Kind.forMultibindingKey(key))
187186
.build();
188187
}
189188

compiler/src/main/java/dagger/internal/codegen/ProvisionBinding.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,16 @@ ProvisionBinding forProvidesMethod(
205205
.build();
206206
}
207207

208-
/**
209-
* A synthetic binding of {@code Map<K, V>} that depends on {@code Map<K, Provider<V>>}.
210-
*/
211-
ProvisionBinding syntheticMapOfValuesBinding(DependencyRequest requestForMapOfValues) {
212-
checkNotNull(requestForMapOfValues);
213-
Optional<Key> mapOfProvidersKey =
214-
keyFactory.implicitMapProviderKeyFrom(requestForMapOfValues.key());
215-
checkArgument(
216-
mapOfProvidersKey.isPresent(),
217-
"%s is not a request for Map<K, V>",
218-
requestForMapOfValues);
208+
/** A synthetic binding of {@code Map<K, V>} that depends on {@code Map<K, Provider<V>>}. */
209+
ProvisionBinding syntheticMapOfValuesBinding(Key mapOfValuesKey) {
210+
checkNotNull(mapOfValuesKey);
211+
Optional<Key> mapOfProvidersKey = keyFactory.implicitMapProviderKeyFrom(mapOfValuesKey);
212+
checkArgument(mapOfProvidersKey.isPresent(), "%s is not a key for Map<K, V>", mapOfValuesKey);
219213
DependencyRequest requestForMapOfProviders =
220214
dependencyRequestFactory.forImplicitMapBinding(mapOfProvidersKey.get());
221215
return ProvisionBinding.builder()
222216
.contributionType(ContributionType.UNIQUE)
223-
.key(requestForMapOfValues.key())
217+
.key(mapOfValuesKey)
224218
.dependencies(requestForMapOfProviders)
225219
.bindingKind(Kind.SYNTHETIC_MAP)
226220
.build();
@@ -233,13 +227,13 @@ ProvisionBinding syntheticMapOfValuesBinding(DependencyRequest requestForMapOfVa
233227
* <p>Note that these could be set multibindings or map multibindings.
234228
*/
235229
ProvisionBinding syntheticMultibinding(
236-
DependencyRequest request, Iterable<ContributionBinding> multibindingContributions) {
230+
Key key, Iterable<ContributionBinding> multibindingContributions) {
237231
return ProvisionBinding.builder()
238232
.contributionType(ContributionType.UNIQUE)
239-
.key(request.key())
233+
.key(key)
240234
.dependencies(
241235
dependencyRequestFactory.forMultibindingContributions(multibindingContributions))
242-
.bindingKind(Kind.forMultibindingRequest(request))
236+
.bindingKind(Kind.forMultibindingKey(key))
243237
.build();
244238
}
245239

0 commit comments

Comments
 (0)