54
54
import com .google .common .collect .Maps ;
55
55
import com .google .common .collect .Sets ;
56
56
import com .google .common .collect .TreeTraverser ;
57
- import com .google .common .util .concurrent .ListenableFuture ;
58
57
import dagger .Component ;
59
58
import dagger .Reusable ;
60
59
import dagger .internal .codegen .ComponentDescriptor .ComponentMethodDescriptor ;
@@ -253,7 +252,7 @@ && isComponentProductionMethod(elements, method)
253
252
for (ComponentMethodDescriptor componentMethod : componentDescriptor .componentMethods ()) {
254
253
Optional <DependencyRequest > componentMethodRequest = componentMethod .dependencyRequest ();
255
254
if (componentMethodRequest .isPresent ()) {
256
- requestResolver .resolve (componentMethodRequest .get ());
255
+ requestResolver .resolve (componentMethodRequest .get (). bindingKey () );
257
256
}
258
257
}
259
258
@@ -293,9 +292,9 @@ private final class Resolver {
293
292
final Map <BindingKey , ResolvedBindings > resolvedBindings ;
294
293
final Deque <BindingKey > cycleStack = new ArrayDeque <>();
295
294
final Cache <BindingKey , Boolean > dependsOnLocalMultibindingsCache =
296
- CacheBuilder .newBuilder ().< BindingKey , Boolean > build ();
295
+ CacheBuilder .newBuilder ().build ();
297
296
final Cache <Binding , Boolean > bindingDependsOnLocalMultibindingsCache =
298
- CacheBuilder .newBuilder ().< Binding , Boolean > build ();
297
+ CacheBuilder .newBuilder ().build ();
299
298
300
299
Resolver (
301
300
Optional <Resolver > parentResolver ,
@@ -317,35 +316,33 @@ private final class Resolver {
317
316
}
318
317
319
318
/**
320
- * Returns the bindings that satisfy a given dependency request .
319
+ * Returns the bindings for the given {@link BindingKey} .
321
320
*
322
321
* <p>For {@link BindingKey.Kind#CONTRIBUTION} requests, returns all of:
322
+ *
323
323
* <ul>
324
324
* <li>All explicit bindings for:
325
325
* <ul>
326
326
* <li>the requested key
327
327
* <li>{@code Set<T>} if the requested key's type is {@code Set<Produced<T>>}
328
- * <li>{@code Map<K, Provider<V>>} if the requested key's type is
329
- * {@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>>}.
330
330
* </ul>
331
331
*
332
332
* <li>A synthetic binding that depends on {@code Map<K, Producer<V>>} if the requested key's
333
- * type is {@code Map<K, V>} and there are some explicit bindings for
334
- * {@code Map<K, Producer<V>>}.
335
- *
333
+ * type is {@code Map<K, V>} and there are some explicit bindings for {@code Map<K,
334
+ * Producer<V>>}.
336
335
* <li>A synthetic binding that depends on {@code Map<K, Provider<V>>} if the requested key's
337
- * type is {@code Map<K, V>} and there are some explicit bindings for
338
- * {@code Map<K, Provider<V>>} but no explicit bindings for {@code Map<K, Producer<V>>}.
339
- *
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>>}.
340
338
* <li>An implicit {@link Inject @Inject}-annotated constructor binding if there is one and
341
339
* there are no explicit bindings or synthetic bindings.
342
340
* </ul>
343
341
*
344
- * <p>For {@link BindingKey.Kind#MEMBERS_INJECTION} requests, returns the
345
- * {@link MembersInjectionBinding} for the type.
342
+ * <p>For {@link BindingKey.Kind#MEMBERS_INJECTION} requests, returns the {@link
343
+ * MembersInjectionBinding} for the type.
346
344
*/
347
- ResolvedBindings lookUpBindings (DependencyRequest request ) {
348
- BindingKey bindingKey = request .bindingKey ();
345
+ ResolvedBindings lookUpBindings (BindingKey bindingKey ) {
349
346
Key requestKey = bindingKey .key ();
350
347
switch (bindingKey .kind ()) {
351
348
case CONTRIBUTION :
@@ -366,9 +363,10 @@ ResolvedBindings lookUpBindings(DependencyRequest request) {
366
363
ImmutableSet <MultibindingDeclaration > multibindingDeclarations =
367
364
multibindingDeclarationsBuilder .build ();
368
365
369
- contributionBindings .addAll (syntheticMapOfValuesBinding (request ).asSet ());
366
+ contributionBindings .addAll (syntheticMapOfValuesBinding (bindingKey . key () ).asSet ());
370
367
contributionBindings .addAll (
371
- syntheticMultibinding (request , multibindingContributions , multibindingDeclarations )
368
+ syntheticMultibinding (
369
+ bindingKey .key (), multibindingContributions , multibindingDeclarations )
372
370
.asSet ());
373
371
374
372
/* If there are no bindings, add the implicit @Inject-constructed binding if there is
@@ -381,7 +379,8 @@ ResolvedBindings lookUpBindings(DependencyRequest request) {
381
379
return ResolvedBindings .forContributionBindings (
382
380
bindingKey ,
383
381
componentDescriptor ,
384
- indexBindingsByOwningComponent (request , ImmutableSet .copyOf (contributionBindings )),
382
+ indexBindingsByOwningComponent (
383
+ bindingKey , ImmutableSet .copyOf (contributionBindings )),
385
384
multibindingDeclarations );
386
385
387
386
case MEMBERS_INJECTION :
@@ -408,32 +407,29 @@ private Iterable<Key> keysMatchingRequest(Key requestKey) {
408
407
}
409
408
410
409
/**
411
- * If {@code request} is for a {@code Map<K, V>} or {@code Map<K, Produced<V>>}, and there are
412
- * any multibinding contributions or declarations that apply to that map, returns a synthetic
413
- * binding for the {@code request} that depends on an {@linkplain
414
- * #syntheticMultibinding(DependencyRequest, Iterable, Iterable) underlying synthetic
415
- * 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}.
416
414
*
417
415
* <p>The returned binding has the same {@link BindingType} as the underlying synthetic
418
416
* multibinding.
419
417
*/
420
- private Optional <ContributionBinding > syntheticMapOfValuesBinding (
421
- final DependencyRequest request ) {
418
+ private Optional <ContributionBinding > syntheticMapOfValuesBinding (final Key key ) {
422
419
return syntheticMultibinding (
423
- request ,
424
- multibindingContributionsForValueMap (request . key () ),
425
- multibindingDeclarationsForValueMap (request . key () ))
420
+ key ,
421
+ multibindingContributionsForValueMap (key ),
422
+ multibindingDeclarationsForValueMap (key ))
426
423
.transform (
427
424
new Function <ContributionBinding , ContributionBinding >() {
428
425
@ Override
429
426
public ContributionBinding apply (ContributionBinding syntheticMultibinding ) {
430
427
switch (syntheticMultibinding .bindingType ()) {
431
428
case PROVISION :
432
- return provisionBindingFactory .syntheticMapOfValuesBinding (request );
429
+ return provisionBindingFactory .syntheticMapOfValuesBinding (key );
433
430
434
431
case PRODUCTION :
435
- return productionBindingFactory .syntheticMapOfValuesOrProducedBinding (
436
- request );
432
+ return productionBindingFactory .syntheticMapOfValuesOrProducedBinding (key );
437
433
438
434
default :
439
435
throw new VerifyException (syntheticMultibinding .toString ());
@@ -488,9 +484,6 @@ public Iterable<MultibindingDeclaration> apply(Key key) {
488
484
* the following types, returns a {@link ProductionBinding}.
489
485
*
490
486
* <ul>
491
- * <li>{@link Producer Producer<SetOrMap>}
492
- * <li>{@link Produced Produced<SetOrMap>}
493
- * <li>{@link ListenableFuture ListenableFuture<SetOrMap>}
494
487
* <li>{@code Set<Produced<T>>}
495
488
* <li>{@code Map<K, Producer<V>>}
496
489
* <li>{@code Map<K, Produced<V>>}
@@ -499,48 +492,32 @@ public Iterable<MultibindingDeclaration> apply(Key key) {
499
492
* Otherwise, returns a {@link ProvisionBinding}.
500
493
*/
501
494
private Optional <? extends ContributionBinding > syntheticMultibinding (
502
- DependencyRequest request ,
495
+ Key key ,
503
496
Iterable <ContributionBinding > multibindingContributions ,
504
497
Iterable <MultibindingDeclaration > multibindingDeclarations ) {
505
498
if (isEmpty (multibindingContributions ) && isEmpty (multibindingDeclarations )) {
506
499
return Optional .absent ();
507
- } else if (multibindingsRequireProduction (multibindingContributions , request )) {
500
+ } else if (multibindingsRequireProduction (multibindingContributions , key )) {
508
501
return Optional .of (
509
- productionBindingFactory .syntheticMultibinding (request , multibindingContributions ));
502
+ productionBindingFactory .syntheticMultibinding (key , multibindingContributions ));
510
503
} else {
511
504
return Optional .of (
512
- provisionBindingFactory .syntheticMultibinding (request , multibindingContributions ));
505
+ provisionBindingFactory .syntheticMultibinding (key , multibindingContributions ));
513
506
}
514
507
}
515
508
516
509
private boolean multibindingsRequireProduction (
517
- Iterable <ContributionBinding > multibindingContributions , DependencyRequest request ) {
518
- switch (request .kind ()) {
519
- case PRODUCER :
520
- case PRODUCED :
521
- case FUTURE :
510
+ Iterable <ContributionBinding > multibindingContributions , Key requestKey ) {
511
+ if (MapType .isMap (requestKey )) {
512
+ MapType mapType = MapType .from (requestKey );
513
+ if (mapType .valuesAreTypeOf (Producer .class ) || mapType .valuesAreTypeOf (Produced .class )) {
522
514
return true ;
523
-
524
- case INSTANCE :
525
- case LAZY :
526
- case PROVIDER :
527
- case PROVIDER_OF_LAZY :
528
- if (MapType .isMap (request .key ())) {
529
- MapType mapType = MapType .from (request .key ());
530
- if (mapType .valuesAreTypeOf (Producer .class )
531
- || mapType .valuesAreTypeOf (Produced .class )) {
532
- return true ;
533
- }
534
- } else if (SetType .isSet (request .key ())
535
- && SetType .from (request .key ()).elementsAreTypeOf (Produced .class )) {
536
- return true ;
537
- }
538
- return Iterables .any (multibindingContributions , isOfType (BindingType .PRODUCTION ));
539
-
540
- case MEMBERS_INJECTOR :
541
- default :
542
- throw new AssertionError (request .kind ());
515
+ }
516
+ } else if (SetType .isSet (requestKey )
517
+ && SetType .from (requestKey ).elementsAreTypeOf (Produced .class )) {
518
+ return true ;
543
519
}
520
+ return Iterables .any (multibindingContributions , isOfType (BindingType .PRODUCTION ));
544
521
}
545
522
546
523
private ImmutableSet <ContributionBinding > createDelegateBindings (
@@ -559,7 +536,8 @@ private ImmutableSet<ContributionBinding> createDelegateBindings(
559
536
* delegate key.
560
537
*/
561
538
private ContributionBinding createDelegateBinding (DelegateDeclaration delegateDeclaration ) {
562
- ResolvedBindings resolvedDelegate = lookUpBindings (delegateDeclaration .delegateRequest ());
539
+ ResolvedBindings resolvedDelegate =
540
+ lookUpBindings (delegateDeclaration .delegateRequest ().bindingKey ());
563
541
if (resolvedDelegate .contributionBindings ().isEmpty ()) {
564
542
// This is guaranteed to result in a missing binding error, so it doesn't matter if the
565
543
// binding is a Provision or Production, except if it is a @IntoMap method, in which
@@ -592,11 +570,11 @@ private ContributionBinding createDelegateBinding(DelegateDeclaration delegateDe
592
570
593
571
private ImmutableSetMultimap <ComponentDescriptor , ContributionBinding >
594
572
indexBindingsByOwningComponent (
595
- DependencyRequest request , Iterable <? extends ContributionBinding > bindings ) {
573
+ BindingKey bindingKey , Iterable <? extends ContributionBinding > bindings ) {
596
574
ImmutableSetMultimap .Builder <ComponentDescriptor , ContributionBinding > index =
597
575
ImmutableSetMultimap .builder ();
598
576
for (ContributionBinding binding : bindings ) {
599
- index .put (getOwningComponent (request , binding ), binding );
577
+ index .put (getOwningComponent (bindingKey , binding ), binding );
600
578
}
601
579
return index .build ();
602
580
}
@@ -608,31 +586,31 @@ private ContributionBinding createDelegateBinding(DelegateDeclaration delegateDe
608
586
* multibinding contributions in this component, returns this component.
609
587
*
610
588
* <p>Otherwise, resolves {@code request} in this component's parent in order to resolve any
611
- * multibinding contributions in the parent, and returns the parent-resolved
612
- * {@link ResolvedBindings#owningComponent(ContributionBinding)}.
589
+ * multibinding contributions in the parent, and returns the parent-resolved {@link
590
+ * ResolvedBindings#owningComponent(ContributionBinding)}.
613
591
*/
614
592
private ComponentDescriptor getOwningComponent (
615
- DependencyRequest request , ContributionBinding binding ) {
616
- if (isResolvedInParent (request , binding )
593
+ BindingKey bindingKey , ContributionBinding binding ) {
594
+ if (isResolvedInParent (bindingKey , binding )
617
595
&& !new MultibindingDependencies ().dependsOnLocalMultibindings (binding )) {
618
596
ResolvedBindings parentResolvedBindings =
619
- parentResolver .get ().resolvedBindings .get (request . bindingKey () );
597
+ parentResolver .get ().resolvedBindings .get (bindingKey );
620
598
return parentResolvedBindings .owningComponent (binding );
621
599
} else {
622
600
return componentDescriptor ;
623
601
}
624
602
}
625
603
626
604
/**
627
- * Returns {@code true} if {@code binding} is owned by an ancestor. If so,
628
- * {@linkplain #resolve(DependencyRequest) resolves} the request in this component's parent.
629
- * Don't resolve directly in the owning component in case it depends on multibindings in any
630
- * 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.
631
609
*/
632
- private boolean isResolvedInParent (DependencyRequest request , ContributionBinding binding ) {
610
+ private boolean isResolvedInParent (BindingKey bindingKey , ContributionBinding binding ) {
633
611
Optional <Resolver > owningResolver = getOwningResolver (binding );
634
612
if (owningResolver .isPresent () && !owningResolver .get ().equals (this )) {
635
- parentResolver .get ().resolve (request );
613
+ parentResolver .get ().resolve (bindingKey );
636
614
return true ;
637
615
} else {
638
616
return false ;
@@ -744,9 +722,7 @@ private Optional<ResolvedBindings> getPreviouslyResolvedBindings(
744
722
}
745
723
}
746
724
747
- void resolve (DependencyRequest request ) {
748
- BindingKey bindingKey = request .bindingKey ();
749
-
725
+ void resolve (BindingKey bindingKey ) {
750
726
// If we find a cycle, stop resolving. The original request will add it with all of the
751
727
// other resolved deps.
752
728
if (cycleStack .contains (bindingKey )) {
@@ -770,7 +746,7 @@ void resolve(DependencyRequest request) {
770
746
if (getPreviouslyResolvedBindings (bindingKey ).isPresent ()) {
771
747
/* Resolve in the parent in case there are multibinding contributions or conflicts in some
772
748
* component between this one and the previously-resolved one. */
773
- parentResolver .get ().resolve (request );
749
+ parentResolver .get ().resolve (bindingKey );
774
750
if (!new MultibindingDependencies ().dependsOnLocalMultibindings (bindingKey )
775
751
&& getExplicitBindings (bindingKey .key ()).isEmpty ()) {
776
752
/* Cache the inherited parent component's bindings in case resolving at the parent found
@@ -784,10 +760,10 @@ && getExplicitBindings(bindingKey.key()).isEmpty()) {
784
760
785
761
cycleStack .push (bindingKey );
786
762
try {
787
- ResolvedBindings bindings = lookUpBindings (request );
763
+ ResolvedBindings bindings = lookUpBindings (bindingKey );
788
764
for (Binding binding : bindings .ownedBindings ()) {
789
765
for (DependencyRequest dependency : binding .implicitDependencies ()) {
790
- resolve (dependency );
766
+ resolve (dependency . bindingKey () );
791
767
}
792
768
}
793
769
resolvedBindings .put (bindingKey , bindings );
0 commit comments