Skip to content

Commit

Permalink
Allow @AssistedFactory types to be injected as Provider<AssistedFacto…
Browse files Browse the repository at this point in the history
…ry>.

This is to allow situations where a JSR330-only library (like AutoFactory) may inject the factory as a Provider<> type. This is unnecessary since the factories are just shells around other providers, but if the library only codes to JSR330 then it may not be able to have knowledge of Dagger's AssistedFactory in order to not request a Provider of the factory. Other Dagger types like Lazy, Producer, Produced are still disallowed since they are Dagger types and so libraries using those types can reasonably add logic to request AssistedFactory types without the wrapper.

RELNOTES=n/a
PiperOrigin-RevId: 378450902
  • Loading branch information
Chang-Eric authored and Dagger Team committed Jun 9, 2021
1 parent d3fccc3 commit 38077ee
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
Expand Up @@ -150,9 +150,10 @@ private void checkType() {
+ ". Did you mean to inject its assisted factory type instead?",
requestElement);
}
if (requestKind != RequestKind.INSTANCE && isAssistedFactoryType(typeElement)) {
if (!(requestKind == RequestKind.INSTANCE || requestKind == RequestKind.PROVIDER)
&& isAssistedFactoryType(typeElement)) {
report.addError(
"Dagger does not support injecting Provider<T>, Lazy<T>, Producer<T>, "
"Dagger does not support injecting Lazy<T>, Producer<T>, "
+ "or Produced<T> when T is an @AssistedFactory-annotated type such as "
+ keyType,
requestElement);
Expand Down
25 changes: 25 additions & 0 deletions javatests/dagger/functional/assisted/AssistedFactoryTest.java
Expand Up @@ -35,6 +35,8 @@ interface ParentComponent {
// Simple factory using a nested factory.
SimpleFoo.Factory nestedSimpleFooFactory();

Provider<SimpleFoo.Factory> nestedSimpleFooFactoryProvider();

// Simple factory using a non-nested factory.
SimpleFooFactory nonNestedSimpleFooFactory();

Expand All @@ -58,6 +60,7 @@ interface ParentComponent {
static class SomeEntryPoint {
private final SimpleFoo.Factory nestedSimpleFooFactory;
private final SimpleFooFactory nonNestedSimpleFooFactory;
private final Provider<SimpleFoo.Factory> nestedSimpleFooFactoryProvider;
private final ExtendedSimpleFooFactory extendedSimpleFooFactory;
private final FooFactory fooFactory;
private final AbstractFooFactory abstractFooFactory;
Expand All @@ -66,12 +69,14 @@ static class SomeEntryPoint {
@Inject
SomeEntryPoint(
SimpleFoo.Factory nestedSimpleFooFactory,
Provider<SimpleFoo.Factory> nestedSimpleFooFactoryProvider,
SimpleFooFactory nonNestedSimpleFooFactory,
ExtendedSimpleFooFactory extendedSimpleFooFactory,
FooFactory fooFactory,
AbstractFooFactory abstractFooFactory,
NoAssistedParametersFooFactory noAssistedParametersFooFactory) {
this.nestedSimpleFooFactory = nestedSimpleFooFactory;
this.nestedSimpleFooFactoryProvider = nestedSimpleFooFactoryProvider;
this.nonNestedSimpleFooFactory = nonNestedSimpleFooFactory;
this.extendedSimpleFooFactory = extendedSimpleFooFactory;
this.fooFactory = fooFactory;
Expand Down Expand Up @@ -264,6 +269,25 @@ public void testNestedSimpleFooFactory() {
assertThat(simpleFoo2.assistedDep).isEqualTo(assistedDep2);
}

@Test
public void testNestedSimpleFooFactoryProvider() {
AssistedDep1 assistedDep1 = new AssistedDep1();
SimpleFoo simpleFoo1 =
DaggerAssistedFactoryTest_ParentComponent.create()
.nestedSimpleFooFactoryProvider()
.get()
.createSimpleFoo(assistedDep1);
assertThat(simpleFoo1.assistedDep).isEqualTo(assistedDep1);

AssistedDep2 assistedDep2 = new AssistedDep2();
SimpleFoo simpleFoo2 =
DaggerAssistedFactoryTest_ParentComponent.create()
.nestedSimpleFooFactoryProvider()
.get()
.createSimpleFoo(assistedDep2);
assertThat(simpleFoo2.assistedDep).isEqualTo(assistedDep2);
}

@Test
public void testNonNestedSimpleFooFactory() {
AssistedDep1 assistedDep1 = new AssistedDep1();
Expand Down Expand Up @@ -323,6 +347,7 @@ public void testAssistedFactoryFromSomeEntryPoint() {
SomeEntryPoint someEntryPoint =
DaggerAssistedFactoryTest_ParentComponent.create().someEntryPoint();
assertThat(someEntryPoint.nestedSimpleFooFactory).isNotNull();
assertThat(someEntryPoint.nestedSimpleFooFactoryProvider).isNotNull();
assertThat(someEntryPoint.nonNestedSimpleFooFactory).isNotNull();
assertThat(someEntryPoint.extendedSimpleFooFactory).isNotNull();
assertThat(someEntryPoint.fooFactory).isNotNull();
Expand Down
Expand Up @@ -620,7 +620,7 @@ public void testProvidesAssistedBindingsAsOptional() {
}

@Test
public void testInjectsProviderOfAssistedFactory() {
public void testInjectsLazyOfAssistedFactory() {
JavaFileObject foo =
JavaFileObjects.forSourceLines(
"test.Foo",
Expand All @@ -644,20 +644,20 @@ public void testInjectsProviderOfAssistedFactory() {
"test.Bar",
"package test;",
"",
"import dagger.Lazy;",
"import javax.inject.Inject;",
"import javax.inject.Provider;",
"",
"class Bar {",
" @Inject",
" Bar(Foo.Factory fooFactory, Provider<Foo.Factory> fooFactoryProvider) {}",
" Bar(Foo.Factory fooFactory, Lazy<Foo.Factory> fooFactoryLazy) {}",
"}");

Compilation compilation = compilerWithOptions(compilerMode.javacopts()).compile(foo, bar);
assertThat(compilation).failed();
assertThat(compilation).hadErrorCount(1);
assertThat(compilation)
.hadErrorContaining(
"Dagger does not support injecting Provider<T>, Lazy<T>, Producer<T>, or Produced<T> "
"Dagger does not support injecting Lazy<T>, Producer<T>, or Produced<T> "
+ "when T is an @AssistedFactory-annotated type such as test.Foo.Factory")
.inFile(bar)
.onLine(8);
Expand Down

0 comments on commit 38077ee

Please sign in to comment.