Skip to content

Commit 3009ccc

Browse files
authored
Merge pull request square#440 from google/moe_sync_0817
Moe sync 08/17
2 parents 798b5f2 + f53c3f9 commit 3009ccc

File tree

6 files changed

+86
-118
lines changed

6 files changed

+86
-118
lines changed

compiler/src/it/functional-tests/src/main/java/test/multipackage/a/AGrandchild.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected void aParentMethod(APublicObject aParentMethod) {
3636
super.aParentMethod(aParentMethod);
3737
}
3838

39+
@SuppressWarnings("OverridesJavaxInjectableMethod")
3940
@Override
4041
protected void aChildMethod(APublicObject aChildMethod) {
4142
super.aChildMethod(aChildMethod);

compiler/src/it/functional-tests/src/main/java/test/multipackage/b/BChild.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ protected void aChildMethod(APublicObject aChildMethod) {
3131
this.aChildMethod = aChildMethod;
3232
}
3333

34+
@SuppressWarnings("OverridesJavaxInjectableMethod")
3435
@Override
3536
protected void aParentMethod(APublicObject aParentMethod) {
3637
super.aParentMethod(aParentMethod);

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

Lines changed: 61 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import com.google.common.collect.Maps;
5555
import com.google.common.collect.Sets;
5656
import com.google.common.collect.TreeTraverser;
57-
import com.google.common.util.concurrent.ListenableFuture;
5857
import dagger.Component;
5958
import dagger.Reusable;
6059
import dagger.internal.codegen.ComponentDescriptor.ComponentMethodDescriptor;
@@ -253,7 +252,7 @@ && isComponentProductionMethod(elements, method)
253252
for (ComponentMethodDescriptor componentMethod : componentDescriptor.componentMethods()) {
254253
Optional<DependencyRequest> componentMethodRequest = componentMethod.dependencyRequest();
255254
if (componentMethodRequest.isPresent()) {
256-
requestResolver.resolve(componentMethodRequest.get());
255+
requestResolver.resolve(componentMethodRequest.get().bindingKey());
257256
}
258257
}
259258

@@ -293,9 +292,9 @@ private final class Resolver {
293292
final Map<BindingKey, ResolvedBindings> resolvedBindings;
294293
final Deque<BindingKey> cycleStack = new ArrayDeque<>();
295294
final Cache<BindingKey, Boolean> dependsOnLocalMultibindingsCache =
296-
CacheBuilder.newBuilder().<BindingKey, Boolean>build();
295+
CacheBuilder.newBuilder().build();
297296
final Cache<Binding, Boolean> bindingDependsOnLocalMultibindingsCache =
298-
CacheBuilder.newBuilder().<Binding, Boolean>build();
297+
CacheBuilder.newBuilder().build();
299298

300299
Resolver(
301300
Optional<Resolver> parentResolver,
@@ -317,35 +316,33 @@ private final class Resolver {
317316
}
318317

319318
/**
320-
* Returns the bindings that satisfy a given dependency request.
319+
* Returns the bindings for the given {@link BindingKey}.
321320
*
322321
* <p>For {@link BindingKey.Kind#CONTRIBUTION} requests, returns all of:
322+
*
323323
* <ul>
324324
* <li>All explicit bindings for:
325325
* <ul>
326326
* <li>the requested key
327327
* <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>>}.
330330
* </ul>
331331
*
332332
* <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>>}.
336335
* <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>>}.
340338
* <li>An implicit {@link Inject @Inject}-annotated constructor binding if there is one and
341339
* there are no explicit bindings or synthetic bindings.
342340
* </ul>
343341
*
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.
346344
*/
347-
ResolvedBindings lookUpBindings(DependencyRequest request) {
348-
BindingKey bindingKey = request.bindingKey();
345+
ResolvedBindings lookUpBindings(BindingKey bindingKey) {
349346
Key requestKey = bindingKey.key();
350347
switch (bindingKey.kind()) {
351348
case CONTRIBUTION:
@@ -366,9 +363,10 @@ ResolvedBindings lookUpBindings(DependencyRequest request) {
366363
ImmutableSet<MultibindingDeclaration> multibindingDeclarations =
367364
multibindingDeclarationsBuilder.build();
368365

369-
contributionBindings.addAll(syntheticMapOfValuesBinding(request).asSet());
366+
contributionBindings.addAll(syntheticMapOfValuesBinding(bindingKey.key()).asSet());
370367
contributionBindings.addAll(
371-
syntheticMultibinding(request, multibindingContributions, multibindingDeclarations)
368+
syntheticMultibinding(
369+
bindingKey.key(), multibindingContributions, multibindingDeclarations)
372370
.asSet());
373371

374372
/* If there are no bindings, add the implicit @Inject-constructed binding if there is
@@ -381,7 +379,8 @@ ResolvedBindings lookUpBindings(DependencyRequest request) {
381379
return ResolvedBindings.forContributionBindings(
382380
bindingKey,
383381
componentDescriptor,
384-
indexBindingsByOwningComponent(request, ImmutableSet.copyOf(contributionBindings)),
382+
indexBindingsByOwningComponent(
383+
bindingKey, ImmutableSet.copyOf(contributionBindings)),
385384
multibindingDeclarations);
386385

387386
case MEMBERS_INJECTION:
@@ -408,32 +407,29 @@ private Iterable<Key> keysMatchingRequest(Key requestKey) {
408407
}
409408

410409
/**
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}.
416414
*
417415
* <p>The returned binding has the same {@link BindingType} as the underlying synthetic
418416
* multibinding.
419417
*/
420-
private Optional<ContributionBinding> syntheticMapOfValuesBinding(
421-
final DependencyRequest request) {
418+
private Optional<ContributionBinding> syntheticMapOfValuesBinding(final Key key) {
422419
return syntheticMultibinding(
423-
request,
424-
multibindingContributionsForValueMap(request.key()),
425-
multibindingDeclarationsForValueMap(request.key()))
420+
key,
421+
multibindingContributionsForValueMap(key),
422+
multibindingDeclarationsForValueMap(key))
426423
.transform(
427424
new Function<ContributionBinding, ContributionBinding>() {
428425
@Override
429426
public ContributionBinding apply(ContributionBinding syntheticMultibinding) {
430427
switch (syntheticMultibinding.bindingType()) {
431428
case PROVISION:
432-
return provisionBindingFactory.syntheticMapOfValuesBinding(request);
429+
return provisionBindingFactory.syntheticMapOfValuesBinding(key);
433430

434431
case PRODUCTION:
435-
return productionBindingFactory.syntheticMapOfValuesOrProducedBinding(
436-
request);
432+
return productionBindingFactory.syntheticMapOfValuesOrProducedBinding(key);
437433

438434
default:
439435
throw new VerifyException(syntheticMultibinding.toString());
@@ -488,9 +484,6 @@ public Iterable<MultibindingDeclaration> apply(Key key) {
488484
* the following types, returns a {@link ProductionBinding}.
489485
*
490486
* <ul>
491-
* <li>{@link Producer Producer<SetOrMap>}
492-
* <li>{@link Produced Produced<SetOrMap>}
493-
* <li>{@link ListenableFuture ListenableFuture<SetOrMap>}
494487
* <li>{@code Set<Produced<T>>}
495488
* <li>{@code Map<K, Producer<V>>}
496489
* <li>{@code Map<K, Produced<V>>}
@@ -499,48 +492,32 @@ public Iterable<MultibindingDeclaration> apply(Key key) {
499492
* Otherwise, returns a {@link ProvisionBinding}.
500493
*/
501494
private Optional<? extends ContributionBinding> syntheticMultibinding(
502-
DependencyRequest request,
495+
Key key,
503496
Iterable<ContributionBinding> multibindingContributions,
504497
Iterable<MultibindingDeclaration> multibindingDeclarations) {
505498
if (isEmpty(multibindingContributions) && isEmpty(multibindingDeclarations)) {
506499
return Optional.absent();
507-
} else if (multibindingsRequireProduction(multibindingContributions, request)) {
500+
} else if (multibindingsRequireProduction(multibindingContributions, key)) {
508501
return Optional.of(
509-
productionBindingFactory.syntheticMultibinding(request, multibindingContributions));
502+
productionBindingFactory.syntheticMultibinding(key, multibindingContributions));
510503
} else {
511504
return Optional.of(
512-
provisionBindingFactory.syntheticMultibinding(request, multibindingContributions));
505+
provisionBindingFactory.syntheticMultibinding(key, multibindingContributions));
513506
}
514507
}
515508

516509
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)) {
522514
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;
543519
}
520+
return Iterables.any(multibindingContributions, isOfType(BindingType.PRODUCTION));
544521
}
545522

546523
private ImmutableSet<ContributionBinding> createDelegateBindings(
@@ -559,7 +536,8 @@ private ImmutableSet<ContributionBinding> createDelegateBindings(
559536
* delegate key.
560537
*/
561538
private ContributionBinding createDelegateBinding(DelegateDeclaration delegateDeclaration) {
562-
ResolvedBindings resolvedDelegate = lookUpBindings(delegateDeclaration.delegateRequest());
539+
ResolvedBindings resolvedDelegate =
540+
lookUpBindings(delegateDeclaration.delegateRequest().bindingKey());
563541
if (resolvedDelegate.contributionBindings().isEmpty()) {
564542
// This is guaranteed to result in a missing binding error, so it doesn't matter if the
565543
// binding is a Provision or Production, except if it is a @IntoMap method, in which
@@ -592,11 +570,11 @@ private ContributionBinding createDelegateBinding(DelegateDeclaration delegateDe
592570

593571
private ImmutableSetMultimap<ComponentDescriptor, ContributionBinding>
594572
indexBindingsByOwningComponent(
595-
DependencyRequest request, Iterable<? extends ContributionBinding> bindings) {
573+
BindingKey bindingKey, Iterable<? extends ContributionBinding> bindings) {
596574
ImmutableSetMultimap.Builder<ComponentDescriptor, ContributionBinding> index =
597575
ImmutableSetMultimap.builder();
598576
for (ContributionBinding binding : bindings) {
599-
index.put(getOwningComponent(request, binding), binding);
577+
index.put(getOwningComponent(bindingKey, binding), binding);
600578
}
601579
return index.build();
602580
}
@@ -608,31 +586,31 @@ private ContributionBinding createDelegateBinding(DelegateDeclaration delegateDe
608586
* multibinding contributions in this component, returns this component.
609587
*
610588
* <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)}.
613591
*/
614592
private ComponentDescriptor getOwningComponent(
615-
DependencyRequest request, ContributionBinding binding) {
616-
if (isResolvedInParent(request, binding)
593+
BindingKey bindingKey, ContributionBinding binding) {
594+
if (isResolvedInParent(bindingKey, binding)
617595
&& !new MultibindingDependencies().dependsOnLocalMultibindings(binding)) {
618596
ResolvedBindings parentResolvedBindings =
619-
parentResolver.get().resolvedBindings.get(request.bindingKey());
597+
parentResolver.get().resolvedBindings.get(bindingKey);
620598
return parentResolvedBindings.owningComponent(binding);
621599
} else {
622600
return componentDescriptor;
623601
}
624602
}
625603

626604
/**
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.
631609
*/
632-
private boolean isResolvedInParent(DependencyRequest request, ContributionBinding binding) {
610+
private boolean isResolvedInParent(BindingKey bindingKey, ContributionBinding binding) {
633611
Optional<Resolver> owningResolver = getOwningResolver(binding);
634612
if (owningResolver.isPresent() && !owningResolver.get().equals(this)) {
635-
parentResolver.get().resolve(request);
613+
parentResolver.get().resolve(bindingKey);
636614
return true;
637615
} else {
638616
return false;
@@ -744,9 +722,7 @@ private Optional<ResolvedBindings> getPreviouslyResolvedBindings(
744722
}
745723
}
746724

747-
void resolve(DependencyRequest request) {
748-
BindingKey bindingKey = request.bindingKey();
749-
725+
void resolve(BindingKey bindingKey) {
750726
// If we find a cycle, stop resolving. The original request will add it with all of the
751727
// other resolved deps.
752728
if (cycleStack.contains(bindingKey)) {
@@ -770,7 +746,7 @@ void resolve(DependencyRequest request) {
770746
if (getPreviouslyResolvedBindings(bindingKey).isPresent()) {
771747
/* Resolve in the parent in case there are multibinding contributions or conflicts in some
772748
* component between this one and the previously-resolved one. */
773-
parentResolver.get().resolve(request);
749+
parentResolver.get().resolve(bindingKey);
774750
if (!new MultibindingDependencies().dependsOnLocalMultibindings(bindingKey)
775751
&& getExplicitBindings(bindingKey.key()).isEmpty()) {
776752
/* Cache the inherited parent component's bindings in case resolving at the parent found
@@ -784,10 +760,10 @@ && getExplicitBindings(bindingKey.key()).isEmpty()) {
784760

785761
cycleStack.push(bindingKey);
786762
try {
787-
ResolvedBindings bindings = lookUpBindings(request);
763+
ResolvedBindings bindings = lookUpBindings(bindingKey);
788764
for (Binding binding : bindings.ownedBindings()) {
789765
for (DependencyRequest dependency : binding.implicitDependencies()) {
790-
resolve(dependency);
766+
resolve(dependency.bindingKey());
791767
}
792768
}
793769
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
}

0 commit comments

Comments
 (0)