From 9a9015190ca3a4832dcbe8cb104491d4c496e209 Mon Sep 17 00:00:00 2001 From: Brad Corso Date: Thu, 28 Jan 2021 14:05:13 -0800 Subject: [PATCH] Fail fast with better error message when trying to use a type parameter 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 https://github.com/google/dagger/issues/2279 RELNOTES=Issue #2279: Adds better error message when trying to use a type parameter with @AssistedFactory creator method. PiperOrigin-RevId: 354389636 --- .../AssistedFactoryProcessingStep.java | 6 ++++ .../codegen/AssistedFactoryErrorsTest.java | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/java/dagger/internal/codegen/AssistedFactoryProcessingStep.java b/java/dagger/internal/codegen/AssistedFactoryProcessingStep.java index 32b1019366c..b687b6b18d4 100644 --- a/java/dagger/internal/codegen/AssistedFactoryProcessingStep.java +++ b/java/dagger/internal/codegen/AssistedFactoryProcessingStep.java @@ -160,6 +160,12 @@ ValidationReport 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) { diff --git a/javatests/dagger/internal/codegen/AssistedFactoryErrorsTest.java b/javatests/dagger/internal/codegen/AssistedFactoryErrorsTest.java index 0c22875342a..ec18cada26e 100644 --- a/javatests/dagger/internal/codegen/AssistedFactoryErrorsTest.java +++ b/javatests/dagger/internal/codegen/AssistedFactoryErrorsTest.java @@ -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 {", + " @AssistedInject", + " Foo() {}", + "", + " @AssistedFactory", + " interface FooFactory {", + " Foo 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); + } }