From 8d9f0820bd0fc7d46e2bed813a330356a8077c66 Mon Sep 17 00:00:00 2001 From: Dagger Team Date: Thu, 19 Sep 2024 15:55:33 -0700 Subject: [PATCH] Add a test for Kotlin nullable field injection. PiperOrigin-RevId: 676595690 --- .../codegen/MembersInjectionTest.java | 22 ++++++++ ..._DEFAULT_MODE_test.MyClass_MembersInjector | 55 +++++++++++++++++++ ...AST_INIT_MODE_test.MyClass_MembersInjector | 55 +++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 javatests/dagger/internal/codegen/goldens/MembersInjectionTest_kotlinNullableFieldInjection_DEFAULT_MODE_test.MyClass_MembersInjector create mode 100644 javatests/dagger/internal/codegen/goldens/MembersInjectionTest_kotlinNullableFieldInjection_FAST_INIT_MODE_test.MyClass_MembersInjector diff --git a/javatests/dagger/internal/codegen/MembersInjectionTest.java b/javatests/dagger/internal/codegen/MembersInjectionTest.java index b5216849d33..e9c65785b71 100644 --- a/javatests/dagger/internal/codegen/MembersInjectionTest.java +++ b/javatests/dagger/internal/codegen/MembersInjectionTest.java @@ -1336,4 +1336,26 @@ public void testMembersInjectionBindingSharesInjectMethodsWithProvisionBinding() subject.generatedSource(goldenFileRule.goldenSource("test/DaggerMyComponent")); }); } + + @Test + public void kotlinNullableFieldInjection() { + Source file = + CompilerTests.kotlinSource( + "MyClass.kt", + "package test;", + "", + "import javax.inject.Inject;", + "", + "class MyClass @Inject constructor() {", + " @JvmField @Inject var nullableString: String? = null", + " @JvmField @Inject var nullableObject: Any? = null", + "}"); + CompilerTests.daggerCompiler(file) + .withProcessingOptions(compilerMode.processorOptions()) + .compile( + subject -> { + subject.hasErrorCount(0); + subject.generatedSource(goldenFileRule.goldenSource("test/MyClass_MembersInjector")); + }); + } } diff --git a/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_kotlinNullableFieldInjection_DEFAULT_MODE_test.MyClass_MembersInjector b/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_kotlinNullableFieldInjection_DEFAULT_MODE_test.MyClass_MembersInjector new file mode 100644 index 00000000000..f1600530234 --- /dev/null +++ b/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_kotlinNullableFieldInjection_DEFAULT_MODE_test.MyClass_MembersInjector @@ -0,0 +1,55 @@ +package test; + +import dagger.MembersInjector; +import dagger.internal.DaggerGenerated; +import dagger.internal.InjectedFieldSignature; +import dagger.internal.QualifierMetadata; +import javax.annotation.processing.Generated; +import javax.inject.Provider; + +@QualifierMetadata +@DaggerGenerated +@Generated( + value = "dagger.internal.codegen.ComponentProcessor", + comments = "https://dagger.dev" +) +@SuppressWarnings({ + "unchecked", + "rawtypes", + "KotlinInternal", + "KotlinInternalInJava", + "cast", + "deprecation" +}) +public final class MyClass_MembersInjector implements MembersInjector { + private final Provider nullableStringProvider; + + private final Provider nullableObjectProvider; + + public MyClass_MembersInjector(Provider nullableStringProvider, + Provider nullableObjectProvider) { + this.nullableStringProvider = nullableStringProvider; + this.nullableObjectProvider = nullableObjectProvider; + } + + public static MembersInjector create(Provider nullableStringProvider, + Provider nullableObjectProvider) { + return new MyClass_MembersInjector(nullableStringProvider, nullableObjectProvider); + } + + @Override + public void injectMembers(MyClass instance) { + injectNullableString(instance, nullableStringProvider.get()); + injectNullableObject(instance, nullableObjectProvider.get()); + } + + @InjectedFieldSignature("test.MyClass.nullableString") + public static void injectNullableString(MyClass instance, String nullableString) { + instance.nullableString = nullableString; + } + + @InjectedFieldSignature("test.MyClass.nullableObject") + public static void injectNullableObject(MyClass instance, Object nullableObject) { + instance.nullableObject = nullableObject; + } +} diff --git a/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_kotlinNullableFieldInjection_FAST_INIT_MODE_test.MyClass_MembersInjector b/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_kotlinNullableFieldInjection_FAST_INIT_MODE_test.MyClass_MembersInjector new file mode 100644 index 00000000000..f1600530234 --- /dev/null +++ b/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_kotlinNullableFieldInjection_FAST_INIT_MODE_test.MyClass_MembersInjector @@ -0,0 +1,55 @@ +package test; + +import dagger.MembersInjector; +import dagger.internal.DaggerGenerated; +import dagger.internal.InjectedFieldSignature; +import dagger.internal.QualifierMetadata; +import javax.annotation.processing.Generated; +import javax.inject.Provider; + +@QualifierMetadata +@DaggerGenerated +@Generated( + value = "dagger.internal.codegen.ComponentProcessor", + comments = "https://dagger.dev" +) +@SuppressWarnings({ + "unchecked", + "rawtypes", + "KotlinInternal", + "KotlinInternalInJava", + "cast", + "deprecation" +}) +public final class MyClass_MembersInjector implements MembersInjector { + private final Provider nullableStringProvider; + + private final Provider nullableObjectProvider; + + public MyClass_MembersInjector(Provider nullableStringProvider, + Provider nullableObjectProvider) { + this.nullableStringProvider = nullableStringProvider; + this.nullableObjectProvider = nullableObjectProvider; + } + + public static MembersInjector create(Provider nullableStringProvider, + Provider nullableObjectProvider) { + return new MyClass_MembersInjector(nullableStringProvider, nullableObjectProvider); + } + + @Override + public void injectMembers(MyClass instance) { + injectNullableString(instance, nullableStringProvider.get()); + injectNullableObject(instance, nullableObjectProvider.get()); + } + + @InjectedFieldSignature("test.MyClass.nullableString") + public static void injectNullableString(MyClass instance, String nullableString) { + instance.nullableString = nullableString; + } + + @InjectedFieldSignature("test.MyClass.nullableObject") + public static void injectNullableObject(MyClass instance, Object nullableObject) { + instance.nullableObject = nullableObject; + } +}