Skip to content

Commit

Permalink
Make LazyClassKeyMap accept both MapFactory and MapProviderFactory
Browse files Browse the repository at this point in the history
Will support producer in a follow up cl.

Fixes #4254

RELNOTES=n/a
PiperOrigin-RevId: 612518867
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Mar 21, 2024
1 parent e6c2ac8 commit 96cf40a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
13 changes: 8 additions & 5 deletions java/dagger/internal/LazyClassKeyMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,24 @@ public void putAll(Map<? extends Class<?>, ? extends V> map) {
throw new UnsupportedOperationException("Dagger map bindings are immutable");
}

// TODO(wanyingd) Support @LazyClassKey on producer
/** A factory for {@code LazyClassKeyMap}. */
@SuppressWarnings("unchecked")
public static class Factory<V> implements Provider<Map<Class<?>, V>> {
MapFactory<String, V> delegate;
Provider<?> delegate;

public static <V> Factory<V> of(MapFactory<String, V> delegate) {
return new Factory<>(delegate);
// MapProviderFactory or MapFactory
public static <V> Factory<V> of(Provider<?> delegate) {
return new Factory<V>(delegate);
}

private Factory(MapFactory<String, V> delegate) {
private Factory(Provider<?> delegate) {
this.delegate = delegate;
}

@Override
public Map<Class<?>, V> get() {
return LazyClassKeyMap.of(delegate.get());
return LazyClassKeyMap.of((Map<String, V>) delegate.get());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,88 @@ public void lazyClassKeySimilarQualifiedName_doesNotConflict() throws Exception
.withProcessingOptions(compilerMode.processorOptions())
.compile(subject -> subject.hasErrorCount(0));
}

@Test
public void lazyClassKeyProvider_compilesSuccessfully() throws Exception {
Source fooBar =
CompilerTests.javaSource("test.Foo_Bar", "package test;", "", "interface Foo_Bar {}");
Source fooBar2 =
CompilerTests.javaSource(
"test.Foo", "package test;", "", "interface Foo { interface Bar {} }");
Source mapKeyBindingsModule =
CompilerTests.javaSource(
"test.MapKeyBindingsModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"import dagger.multibindings.LazyClassKey;",
"import dagger.multibindings.IntoMap;",
"",
"@Module",
"public interface MapKeyBindingsModule {",
" @Provides @IntoMap @LazyClassKey(test.Foo_Bar.class)",
" static int classKey() { return 1; }",
"}");

Source componentFile =
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
"import dagger.Component;",
"import javax.inject.Provider;",
"import java.util.Map;",
"",
"@Component(modules = MapKeyBindingsModule.class)",
"interface TestComponent {",
" Map<Class<?>, Provider<Integer>> classKey();",
"}");
CompilerTests.daggerCompiler(fooBar, fooBar2, mapKeyBindingsModule, componentFile)
.withProcessingOptions(compilerMode.processorOptions())
.compile(subject -> subject.hasErrorCount(0));
}

@Test
public void scopedLazyClassKeyProvider_compilesSuccessfully() throws Exception {
Source fooBar =
CompilerTests.javaSource("test.Foo_Bar", "package test;", "", "interface Foo_Bar {}");
Source fooBar2 =
CompilerTests.javaSource(
"test.Foo", "package test;", "", "interface Foo { interface Bar {} }");
Source mapKeyBindingsModule =
CompilerTests.javaSource(
"test.MapKeyBindingsModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"import dagger.multibindings.LazyClassKey;",
"import dagger.multibindings.IntoMap;",
"",
"@Module",
"public interface MapKeyBindingsModule {",
" @Provides @IntoMap @LazyClassKey(test.Foo_Bar.class)",
" static int classKey() { return 1; }",
"}");

Source componentFile =
CompilerTests.javaSource(
"test.TestComponent",
"package test;",
"",
"import dagger.Component;",
"import javax.inject.Singleton;",
"import javax.inject.Provider;",
"import java.util.Map;",
"",
"@Component(modules = MapKeyBindingsModule.class)",
"@Singleton",
"interface TestComponent {",
" Provider<Map<Class<?>, Provider<Integer>>> classKey();",
"}");
CompilerTests.daggerCompiler(fooBar, fooBar2, mapKeyBindingsModule, componentFile)
.withProcessingOptions(compilerMode.processorOptions())
.compile(subject -> subject.hasErrorCount(0));
}
}

0 comments on commit 96cf40a

Please sign in to comment.