Skip to content

Commit

Permalink
[Refactor] Remove duplicated logic in BindingGraphFactory.
Browse files Browse the repository at this point in the history
When searching for multibinding declarations, the key (e.g. `Map<K, Provider<V>>`) has to be unwrapped since the declaration keys have the form `Map<K, V>`. This CL consolidates the logic into a single function so that we only have to document these details once.

RELNOTES=N/A
PiperOrigin-RevId: 564374943
  • Loading branch information
bcorso authored and Dagger Team committed Sep 11, 2023
1 parent 9852b42 commit 47953aa
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions java/dagger/internal/codegen/binding/BindingGraphFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static dagger.internal.codegen.base.Util.reentrantComputeIfAbsent;
import static dagger.internal.codegen.binding.AssistedInjectionAnnotations.isAssistedFactoryType;
import static dagger.internal.codegen.binding.SourceFiles.generatedMonitoringModuleName;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.model.BindingKind.ASSISTED_INJECTION;
import static dagger.internal.codegen.model.BindingKind.DELEGATE;
import static dagger.internal.codegen.model.BindingKind.INJECTION;
Expand All @@ -47,7 +48,6 @@
import dagger.internal.codegen.base.ContributionType;
import dagger.internal.codegen.base.DaggerSuperficialValidation;
import dagger.internal.codegen.base.Keys;
import dagger.internal.codegen.base.MapType;
import dagger.internal.codegen.base.OptionalType;
import dagger.internal.codegen.compileroption.CompilerOptions;
import dagger.internal.codegen.javapoet.TypeNames;
Expand Down Expand Up @@ -521,12 +521,17 @@ private ImmutableSet<Key> keysMatchingRequestUncached(Key requestKey) {
}

private ImmutableSet<ContributionBinding> createDelegateBindings(
ImmutableSet<DelegateDeclaration> delegateDeclarations) {
ImmutableSet.Builder<ContributionBinding> builder = ImmutableSet.builder();
for (DelegateDeclaration delegateDeclaration : delegateDeclarations) {
builder.add(createDelegateBinding(delegateDeclaration));
}
return builder.build();
ImmutableSetMultimap<Key, DelegateDeclaration> delegateDeclarations, Key requestKey) {
return delegateDeclarations.get(
// @Binds @IntoMap declarations have key Map<K, V>, unlike @Provides @IntoMap or
// @Produces @IntoMap, which have Map<K, Provider/Producer<V>> keys. So unwrap the
// key's type's value type if it's a Map<K, Provider/Producer<V>> before looking
// in delegateDeclarations. createDelegateBindings() will create bindings with the
// properly wrapped key type.
keyFactory.unwrapMapValueType(requestKey))
.stream()
.map(this::createDelegateBinding)
.collect(toImmutableSet());
}

/**
Expand Down Expand Up @@ -699,15 +704,9 @@ private ImmutableList<Resolver> getResolverLineage() {
* resolver.
*/
private ImmutableSet<ContributionBinding> getLocalExplicitBindings(Key key) {
return new ImmutableSet.Builder<ContributionBinding>()
return ImmutableSet.<ContributionBinding>builder()
.addAll(explicitBindings.get(key))
// @Binds @IntoMap declarations have key Map<K, V>, unlike @Provides @IntoMap or @Produces
// @IntoMap, which have Map<K, Provider/Producer<V>> keys. So unwrap the key's type's
// value type if it's a Map<K, Provider/Producer<V>> before looking in
// delegateDeclarations. createDelegateBindings() will create bindings with the properly
// wrapped key type.
.addAll(
createDelegateBindings(delegateDeclarations.get(keyFactory.unwrapMapValueType(key))))
.addAll(createDelegateBindings(delegateDeclarations, key))
.build();
}

Expand All @@ -716,21 +715,10 @@ private ImmutableSet<ContributionBinding> getLocalExplicitBindings(Key key) {
* by {@code key} from this resolver.
*/
private ImmutableSet<ContributionBinding> getLocalExplicitMultibindings(Key key) {
ImmutableSet.Builder<ContributionBinding> multibindings = ImmutableSet.builder();
multibindings.addAll(explicitMultibindings.get(key));
if (!MapType.isMap(key)
|| MapType.from(key).isRawType()
|| MapType.from(key).valuesAreFrameworkType()) {
// @Binds @IntoMap declarations have key Map<K, V>, unlike @Provides @IntoMap or @Produces
// @IntoMap, which have Map<K, Provider/Producer<V>> keys. So unwrap the key's type's
// value type if it's a Map<K, Provider/Producer<V>> before looking in
// delegateMultibindingDeclarations. createDelegateBindings() will create bindings with the
// properly wrapped key type.
multibindings.addAll(
createDelegateBindings(
delegateMultibindingDeclarations.get(keyFactory.unwrapMapValueType(key))));
}
return multibindings.build();
return ImmutableSet.<ContributionBinding>builder()
.addAll(explicitMultibindings.get(key))
.addAll(createDelegateBindings(delegateMultibindingDeclarations, key))
.build();
}

/**
Expand Down

0 comments on commit 47953aa

Please sign in to comment.