Skip to content

Commit

Permalink
Fail fast with better error message when trying to use a type paramet…
Browse files Browse the repository at this point in the history
…er with the @AssistedFactory creator method.

We're considering supporting this feature in the future, but until then we're adding a better error message.

See #2279

RELNOTES=Issue #2279: Adds better error message when trying to use a type parameter with @AssistedFactory creator method.
PiperOrigin-RevId: 354389636
  • Loading branch information
bcorso authored and Dagger Team committed Jan 28, 2021
1 parent 89b334d commit 9a90151
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ ValidationReport<TypeElement> validate(TypeElement factory) {
methodType.getReturnType()),
method);
}
if (!method.getTypeParameters().isEmpty()) {
report.addError(
"@AssistedFactory does not currently support type parameters in the creator "
+ "method. See https://github.com/google/dagger/issues/2279",
method);
}
}

if (abstractFactoryMethods.size() > 1) {
Expand Down
30 changes: 30 additions & 0 deletions javatests/dagger/internal/codegen/AssistedFactoryErrorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -772,4 +772,34 @@ public void testInaccessibleFoo() {
assertThat(compilation).succeeded();
}
}

@Test
public void testAssistedFactoryMethodWithTypeParametersFails() {
JavaFileObject foo =
JavaFileObjects.forSourceLines(
"test.Foo",
"package test;",
"",
"import dagger.assisted.AssistedInject;",
"import dagger.assisted.AssistedFactory;",
"",
"class Foo<T> {",
" @AssistedInject",
" Foo() {}",
"",
" @AssistedFactory",
" interface FooFactory {",
" <T> Foo<T> create();",
" }",
"}");

Compilation compilation = compilerWithOptions(compilerMode.javacopts()).compile(foo);
assertThat(compilation).failed();
assertThat(compilation).hadErrorCount(1);
assertThat(compilation)
.hadErrorContaining(
"@AssistedFactory does not currently support type parameters in the creator method.")
.inFile(foo)
.onLine(12);
}
}

0 comments on commit 9a90151

Please sign in to comment.