From cda6e32f1ef67156c783bdcc454bbeafd45b3228 Mon Sep 17 00:00:00 2001 From: Brad Corso Date: Fri, 29 Jan 2021 07:55:41 -0800 Subject: [PATCH] Fix type inference issues with generated assisted factory in Java 7. To test this, I've updated our gradle use sourceCompatibility = VERSION_1_7. Fixes https://github.com/google/dagger/issues/2309 RELNOTES=Fixes #2309: Fixes type inference issues with generated assisted factory in Java 7. PiperOrigin-RevId: 354533035 --- .../AssistedFactoryProcessingStep.java | 11 +++- .../artifacts/dagger/simple/build.gradle | 11 ++-- .../java/dagger/simple/AssistedInjects.java | 66 +++++++++++++++++++ .../dagger/simple}/SimpleApplication.java | 2 +- 4 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 javatests/artifacts/dagger/simple/src/main/java/dagger/simple/AssistedInjects.java rename javatests/artifacts/dagger/simple/{ => src/main/java/dagger/simple}/SimpleApplication.java (97%) diff --git a/java/dagger/internal/codegen/AssistedFactoryProcessingStep.java b/java/dagger/internal/codegen/AssistedFactoryProcessingStep.java index 798a97579a3..67090ab0f84 100644 --- a/java/dagger/internal/codegen/AssistedFactoryProcessingStep.java +++ b/java/dagger/internal/codegen/AssistedFactoryProcessingStep.java @@ -305,8 +305,9 @@ public Element originatingElement(ProvisionBinding binding) { public Optional write(ProvisionBinding binding) { TypeElement factory = asType(binding.bindingElement().get()); + ClassName name = nameGeneratedType(binding); TypeSpec.Builder builder = - TypeSpec.classBuilder(nameGeneratedType(binding)) + TypeSpec.classBuilder(name) .addModifiers(PUBLIC, FINAL) .addTypeVariables( factory.getTypeParameters().stream() @@ -357,9 +358,13 @@ public Optional write(ProvisionBinding binding) { .collect(toImmutableList())) .returns(providerOf(TypeName.get(factory.asType()))) .addStatement( - "return $T.create(new $T($N))", + "return $T.$Lcreate(new $T($N))", INSTANCE_FACTORY, - nameGeneratedType(binding), + // Java 7 type inference requires the method call provide the exact type here. + sourceVersion.compareTo(SourceVersion.RELEASE_7) <= 0 + ? CodeBlock.of("<$T>", types.accessibleType(factoryType, name)) + : CodeBlock.of(""), + name, delegateFactoryParam) .build()); return Optional.of(builder); diff --git a/javatests/artifacts/dagger/simple/build.gradle b/javatests/artifacts/dagger/simple/build.gradle index 711701786ff..97c966e50e5 100644 --- a/javatests/artifacts/dagger/simple/build.gradle +++ b/javatests/artifacts/dagger/simple/build.gradle @@ -24,12 +24,9 @@ repositories { mavenLocal() } -sourceSets { - main { - java { - srcDir '.' - } - } +java { + // Make sure the generated source is compatible with Java 7. + sourceCompatibility = JavaVersion.VERSION_1_7 } dependencies { @@ -37,4 +34,4 @@ dependencies { annotationProcessor 'com.google.dagger:dagger-compiler:LOCAL-SNAPSHOT' } -mainClassName = 'dagger.example.gradle.simple.SimpleApplication' \ No newline at end of file +mainClassName = 'dagger.simple.SimpleApplication' \ No newline at end of file diff --git a/javatests/artifacts/dagger/simple/src/main/java/dagger/simple/AssistedInjects.java b/javatests/artifacts/dagger/simple/src/main/java/dagger/simple/AssistedInjects.java new file mode 100644 index 00000000000..eb4946fe305 --- /dev/null +++ b/javatests/artifacts/dagger/simple/src/main/java/dagger/simple/AssistedInjects.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2021 The Dagger Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dagger.simple; + +import dagger.Component; +import dagger.assisted.Assisted; +import dagger.assisted.AssistedFactory; +import dagger.assisted.AssistedInject; +import javax.inject.Inject; + +// This is a regression test for https://github.com/google/dagger/issues/2309 +/** A simple, skeletal application that defines an assisted inject binding. */ +public class AssistedInjects { + @Component + interface MyComponent { + FooFactory fooFactory(); + + ParameterizedFooFactory parameterizedFooFactory(); + } + + static final class Bar { + @Inject + Bar() {} + } + + static class Foo { + @AssistedInject + Foo(Bar bar, @Assisted String str) {} + } + + @AssistedFactory + interface FooFactory { + Foo create(String str); + } + + static class ParameterizedFoo { + @AssistedInject + ParameterizedFoo(T1 t1, @Assisted T2 t2) {} + } + + @AssistedFactory + interface ParameterizedFooFactory { + ParameterizedFoo create(T2 t2); + } + + public static void main(String[] args) { + Foo foo = DaggerAssistedInjects_MyComponent.create().fooFactory().create(""); + + ParameterizedFoo parameterizedFoo = + DaggerAssistedInjects_MyComponent.create().parameterizedFooFactory().create(""); + } +} diff --git a/javatests/artifacts/dagger/simple/SimpleApplication.java b/javatests/artifacts/dagger/simple/src/main/java/dagger/simple/SimpleApplication.java similarity index 97% rename from javatests/artifacts/dagger/simple/SimpleApplication.java rename to javatests/artifacts/dagger/simple/src/main/java/dagger/simple/SimpleApplication.java index 6e39cec22d6..f13610101d0 100644 --- a/javatests/artifacts/dagger/simple/SimpleApplication.java +++ b/javatests/artifacts/dagger/simple/src/main/java/dagger/simple/SimpleApplication.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package dagger.example.gradle.simple; +package dagger.simple; import dagger.Component; import dagger.Module;