Skip to content

Commit

Permalink
Internal changes
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 653428725
  • Loading branch information
bcorso authored and Dagger Team committed Jul 18, 2024
1 parent 56e1eeb commit 982dab4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 239 deletions.
23 changes: 11 additions & 12 deletions java/dagger/internal/codegen/binding/BindingGraphFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,18 @@ ResolvedBindings lookUpBindings(Key requestKey) {
Set<SubcomponentDeclaration> subcomponentDeclarations = new LinkedHashSet<>();

// Gather all bindings, multibindings, optional, and subcomponent declarations/contributions.
ImmutableSet<Key> multibindingKeysMatchingRequest =
multibindingKeysMatchingRequest(requestKey);
ImmutableSet<Key> keysMatchingRequest = keysMatchingRequest(requestKey);
for (Resolver resolver : getResolverLineage()) {
bindings.addAll(resolver.getLocalExplicitBindings(requestKey));
subcomponentDeclarations.addAll(resolver.declarations.subcomponents(requestKey));
// The optional binding declarations are keyed by the unwrapped type.
keyFactory.unwrapOptional(requestKey)
.map(resolver.declarations::optionalBindings)
.ifPresent(optionalBindingDeclarations::addAll);

for (Key key : multibindingKeysMatchingRequest) {
for (Key key : keysMatchingRequest) {
multibindingContributions.addAll(resolver.getLocalMultibindingContributions(key));
multibindingDeclarations.addAll(resolver.declarations.multibindings(key));
subcomponentDeclarations.addAll(resolver.declarations.subcomponents(key));
// The optional binding declarations are keyed by the unwrapped type.
keyFactory.unwrapOptional(key)
.map(resolver.declarations::optionalBindings)
.ifPresent(optionalBindingDeclarations::addAll);
}
}

Expand Down Expand Up @@ -426,12 +425,12 @@ private void addSubcomponentToOwningResolver(ContributionBinding subcomponentCre
* javac users)
* </ul>
*/
private ImmutableSet<Key> multibindingKeysMatchingRequest(Key requestKey) {
private ImmutableSet<Key> keysMatchingRequest(Key requestKey) {
return keysMatchingRequestCache.computeIfAbsent(
requestKey, this::multibindingKeysMatchingRequestUncached);
requestKey, this::keysMatchingRequestUncached);
}

private ImmutableSet<Key> multibindingKeysMatchingRequestUncached(Key requestKey) {
private ImmutableSet<Key> keysMatchingRequestUncached(Key requestKey) {
ImmutableSet.Builder<Key> keys = ImmutableSet.builder();
keys.add(requestKey);
keyFactory.unwrapSetKey(requestKey, TypeNames.PRODUCED).ifPresent(keys::add);
Expand Down Expand Up @@ -868,7 +867,7 @@ private boolean hasLocalBindings(ResolvedBindings resolvedBindings) {
* this component's modules that matches the key.
*/
private boolean hasLocalMultibindingContributions(Key requestKey) {
return multibindingKeysMatchingRequest(requestKey)
return keysMatchingRequest(requestKey)
.stream()
.anyMatch(key -> !getLocalMultibindingContributions(key).isEmpty());
}
Expand Down
227 changes: 0 additions & 227 deletions javatests/dagger/internal/codegen/MultibindingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package dagger.internal.codegen;

import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.util.Source;
import dagger.testing.compile.CompilerTests;
import org.junit.Test;
Expand Down Expand Up @@ -300,230 +299,4 @@ public void provideExplicitSetInParent_AndMultibindingContributionInChild() {
.onLineContaining("interface Parent");
});
}

// Regression test for b/352142595.
@Test
public void testMultibindingMapWithKotlinSource() {
Source parent =
CompilerTests.kotlinSource(
"test.Parent.kt",
"package test",
"",
"import dagger.Component",
"",
"@Component(modules = [ParentModule::class])",
"interface Parent {",
" fun usage(): Usage",
"}");
Source usage =
CompilerTests.kotlinSource(
"test.Usage.kt",
"package test",
"",
"import javax.inject.Inject",
"",
"class Usage @Inject constructor(map: Map<String, MyInterface>)");
Source parentModule =
CompilerTests.kotlinSource(
"test.ParentModule.kt",
"@file:Suppress(\"INLINE_FROM_HIGHER_PLATFORM\")", // Required to use TODO()
"package test",
"",
"import dagger.Module",
"import dagger.Provides",
"import dagger.multibindings.IntoMap",
"import dagger.multibindings.StringKey",
"",
"@Module",
"class ParentModule {",
" @Provides",
" @IntoMap",
" @StringKey(\"key\")",
" fun provideMyInterface(): MyInterface = TODO()",
"}");
Source myInterface =
CompilerTests.kotlinSource(
"test.MyInterface.kt",
"package test",
"",
"interface MyInterface");

CompilerTests.daggerCompiler(parent, parentModule, myInterface, usage)
.compile(
subject -> {
// TODO(b/284207175): Due to a bug in our KSP implementation, KSP and Javac behave
// differently. Rather than cause a breaking change by fixing this bug directly, we've
// decided to wait until b/284207175 is implemented.
if (CompilerTests.backend(subject) == XProcessingEnv.Backend.KSP) {
subject.hasErrorCount(0);
} else {
subject.hasErrorCount(1);
subject.hasErrorContaining("Map<String,? extends MyInterface> cannot be provided");
}
});
}

// Regression test for b/352142595.
@Test
public void testMultibindingMapProviderWithKotlinSource() {
Source parent =
CompilerTests.kotlinSource(
"test.Parent.kt",
"package test",
"",
"import dagger.Component",
"",
"@Component(modules = [ParentModule::class])",
"interface Parent {",
" fun usage(): Usage",
"}");
Source usage =
CompilerTests.kotlinSource(
"test.Usage.kt",
"package test",
"",
"import javax.inject.Inject",
"import javax.inject.Provider",
"",
"class Usage @Inject constructor(map: Map<String, Provider<MyInterface>>)");
Source parentModule =
CompilerTests.kotlinSource(
"test.ParentModule.kt",
"@file:Suppress(\"INLINE_FROM_HIGHER_PLATFORM\")", // Required to use TODO()
"package test",
"",
"import dagger.Module",
"import dagger.Provides",
"import dagger.multibindings.IntoMap",
"import dagger.multibindings.StringKey",
"",
"@Module",
"class ParentModule {",
" @Provides",
" @IntoMap",
" @StringKey(\"key\")",
" fun provideMyInterface(): MyInterface = TODO()",
"}");
Source myInterface =
CompilerTests.kotlinSource(
"test.MyInterface.kt",
"package test",
"",
"interface MyInterface");

CompilerTests.daggerCompiler(parent, parentModule, myInterface, usage)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining(
"Map<String,? extends Provider<MyInterface>> cannot be provided");
});
}

// Regression test for b/352142595.
@Test
public void testMultibindingSetWithKotlinSource() {
Source parent =
CompilerTests.kotlinSource(
"test.Parent.kt",
"package test",
"",
"import dagger.Component",
"",
"@Component(modules = [ParentModule::class])",
"interface Parent {",
" fun usage(): Usage",
"}");
Source usage =
CompilerTests.kotlinSource(
"test.Usage.kt",
"package test",
"",
"import javax.inject.Inject",
"",
"class Usage @Inject constructor(set: Set<MyInterface>)");
Source parentModule =
CompilerTests.kotlinSource(
"test.ParentModule.kt",
"@file:Suppress(\"INLINE_FROM_HIGHER_PLATFORM\")", // Required to use TODO()
"package test",
"",
"import dagger.Module",
"import dagger.Provides",
"import dagger.multibindings.IntoSet",
"",
"@Module",
"class ParentModule {",
" @Provides",
" @IntoSet",
" fun provideMyInterface(): MyInterface = TODO()",
"}");
Source myInterface =
CompilerTests.kotlinSource(
"test.MyInterface.kt",
"package test",
"",
"interface MyInterface");

CompilerTests.daggerCompiler(parent, parentModule, myInterface, usage)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining("Set<? extends MyInterface> cannot be provided");
});
}

// Regression test for b/352142595.
@Test
public void testMultibindingSetProviderWithKotlinSource() {
Source parent =
CompilerTests.kotlinSource(
"test.Parent.kt",
"package test",
"",
"import dagger.Component",
"",
"@Component(modules = [ParentModule::class])",
"interface Parent {",
" fun usage(): Usage",
"}");
Source usage =
CompilerTests.kotlinSource(
"test.Usage.kt",
"package test",
"",
"import javax.inject.Inject",
"import javax.inject.Provider",
"",
"class Usage @Inject constructor(set: Set<Provider<MyInterface>>)");
Source parentModule =
CompilerTests.kotlinSource(
"test.ParentModule.kt",
"@file:Suppress(\"INLINE_FROM_HIGHER_PLATFORM\")", // Required to use TODO()
"package test",
"",
"import dagger.Module",
"import dagger.Provides",
"import dagger.multibindings.IntoSet",
"",
"@Module",
"class ParentModule {",
" @Provides",
" @IntoSet",
" fun provideMyInterface(): MyInterface = TODO()",
"}");
Source myInterface =
CompilerTests.kotlinSource(
"test.MyInterface.kt",
"package test",
"",
"interface MyInterface");

CompilerTests.daggerCompiler(parent, parentModule, myInterface, usage)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining("Set<? extends Provider<MyInterface>> cannot be provided");
});
}
}

0 comments on commit 982dab4

Please sign in to comment.