diff --git a/javatests/dagger/internal/codegen/MembersInjectionTest.java b/javatests/dagger/internal/codegen/MembersInjectionTest.java index 4748853fb90..b5216849d33 100644 --- a/javatests/dagger/internal/codegen/MembersInjectionTest.java +++ b/javatests/dagger/internal/codegen/MembersInjectionTest.java @@ -32,6 +32,23 @@ @RunWith(Parameterized.class) public class MembersInjectionTest { + + private static final Source TYPE_USE_NULLABLE = + CompilerTests.javaSource( + "test.Nullable", // force one-string-per-line format + "package test;", + "import static java.lang.annotation.ElementType.TYPE_USE;", + "import java.lang.annotation.Target;", + "", + "@Target(TYPE_USE)", + "public @interface Nullable {}"); + private static final Source NON_TYPE_USE_NULLABLE = + CompilerTests.javaSource( + "test.Nullable", // force one-string-per-line format + "package test;", + "", + "public @interface Nullable {}"); + @Parameters(name = "{0}") public static ImmutableList parameters() { return CompilerMode.TEST_PARAMETERS; @@ -328,6 +345,54 @@ public void parentClass_injectedMembersInSupertype() throws Exception { }); } + @Test + public void typeUseNullableFieldInjection() { + Source file = + CompilerTests.javaSource( + "test.FieldInjection", + "package test;", + "", + "import dagger.Lazy;", + "import javax.inject.Inject;", + "import javax.inject.Provider;", + "", + "class FieldInjection {", + " @Inject @Nullable String string;", + "}"); + CompilerTests.daggerCompiler(file, TYPE_USE_NULLABLE) + .withProcessingOptions(compilerMode.processorOptions()) + .compile( + subject -> { + subject.hasErrorCount(0); + subject.generatedSource( + goldenFileRule.goldenSource("test/FieldInjection_MembersInjector")); + }); + } + + @Test + public void nonTypeUseNullableFieldInjection() { + Source file = + CompilerTests.javaSource( + "test.FieldInjection", + "package test;", + "", + "import dagger.Lazy;", + "import javax.inject.Inject;", + "import javax.inject.Provider;", + "", + "class FieldInjection {", + " @Inject @Nullable String string;", + "}"); + CompilerTests.daggerCompiler(file, NON_TYPE_USE_NULLABLE) + .withProcessingOptions(compilerMode.processorOptions()) + .compile( + subject -> { + subject.hasErrorCount(0); + subject.generatedSource( + goldenFileRule.goldenSource("test/FieldInjection_MembersInjector")); + }); + } + @Test public void fieldInjectionWithQualifier() { Source file = diff --git a/javatests/dagger/internal/codegen/ModuleFactoryGeneratorTest.java b/javatests/dagger/internal/codegen/ModuleFactoryGeneratorTest.java index cc8e3b453cc..0ff525f48b1 100644 --- a/javatests/dagger/internal/codegen/ModuleFactoryGeneratorTest.java +++ b/javatests/dagger/internal/codegen/ModuleFactoryGeneratorTest.java @@ -32,9 +32,22 @@ @RunWith(JUnit4.class) public class ModuleFactoryGeneratorTest { - private static final Source NULLABLE = - CompilerTests.javaSource( - "test.Nullable", "package test;", "public @interface Nullable {}"); + private static final Source TYPE_USE_NULLABLE = + CompilerTests.javaSource( + "test.Nullable", // force one-string-per-line format + "package test;", + "import static java.lang.annotation.ElementType.TYPE_USE;", + "import java.lang.annotation.Target;", + "", + "@Target(TYPE_USE)", + "public @interface Nullable {}"); + + private static final Source NON_TYPE_USE_NULLABLE = + CompilerTests.javaSource( + "test.Nullable", // force one-string-per-line format + "package test;", + "", + "public @interface Nullable {}"); @Rule public GoldenFileRule goldenFileRule = new GoldenFileRule(); @@ -277,7 +290,31 @@ public void providesMethodReturnsProduced() { }); } - @Test public void nullableProvides() { + @Test + public void typeUseNullableProvides() { + Source moduleFile = + CompilerTests.javaSource( + "test.TestModule", + "package test;", + "", + "import dagger.Module;", + "import dagger.Provides;", + "", + "@Module", + "final class TestModule {", + " @Provides @Nullable String provideString() { return null; }", + "}"); + CompilerTests.daggerCompiler(moduleFile, TYPE_USE_NULLABLE) + .compile( + subject -> { + subject.hasErrorCount(0); + subject.generatedSource( + goldenFileRule.goldenSource("test/TestModule_ProvideStringFactory")); + }); + } + + @Test + public void nonTypeUseNullableProvides() { Source moduleFile = CompilerTests.javaSource( "test.TestModule", @@ -290,7 +327,7 @@ public void providesMethodReturnsProduced() { "final class TestModule {", " @Provides @Nullable String provideString() { return null; }", "}"); - CompilerTests.daggerCompiler(moduleFile, NULLABLE) + CompilerTests.daggerCompiler(moduleFile, NON_TYPE_USE_NULLABLE) .compile( subject -> { subject.hasErrorCount(0); @@ -299,7 +336,8 @@ public void providesMethodReturnsProduced() { }); } - @Test public void multipleProvidesMethods() { + @Test + public void multipleProvidesMethods() { Source classXFile = CompilerTests.javaSource("test.X", "package test;", @@ -344,7 +382,8 @@ public void providesMethodReturnsProduced() { }); } - @Test public void providesSetElement() { + @Test + public void providesSetElement() { Source moduleFile = CompilerTests.javaSource( "test.TestModule", diff --git a/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_nonTypeUseNullableFieldInjection_DEFAULT_MODE_test.FieldInjection_MembersInjector b/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_nonTypeUseNullableFieldInjection_DEFAULT_MODE_test.FieldInjection_MembersInjector new file mode 100644 index 00000000000..823dc6a633a --- /dev/null +++ b/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_nonTypeUseNullableFieldInjection_DEFAULT_MODE_test.FieldInjection_MembersInjector @@ -0,0 +1,44 @@ +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 FieldInjection_MembersInjector implements MembersInjector { + private final Provider stringProvider; + + public FieldInjection_MembersInjector(Provider stringProvider) { + this.stringProvider = stringProvider; + } + + public static MembersInjector create(Provider stringProvider) { + return new FieldInjection_MembersInjector(stringProvider); + } + + @Override + public void injectMembers(FieldInjection instance) { + injectString(instance, stringProvider.get()); + } + + @InjectedFieldSignature("test.FieldInjection.string") + public static void injectString(Object instance, String string) { + ((FieldInjection) instance).string = string; + } +} diff --git a/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_nonTypeUseNullableFieldInjection_FAST_INIT_MODE_test.FieldInjection_MembersInjector b/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_nonTypeUseNullableFieldInjection_FAST_INIT_MODE_test.FieldInjection_MembersInjector new file mode 100644 index 00000000000..823dc6a633a --- /dev/null +++ b/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_nonTypeUseNullableFieldInjection_FAST_INIT_MODE_test.FieldInjection_MembersInjector @@ -0,0 +1,44 @@ +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 FieldInjection_MembersInjector implements MembersInjector { + private final Provider stringProvider; + + public FieldInjection_MembersInjector(Provider stringProvider) { + this.stringProvider = stringProvider; + } + + public static MembersInjector create(Provider stringProvider) { + return new FieldInjection_MembersInjector(stringProvider); + } + + @Override + public void injectMembers(FieldInjection instance) { + injectString(instance, stringProvider.get()); + } + + @InjectedFieldSignature("test.FieldInjection.string") + public static void injectString(Object instance, String string) { + ((FieldInjection) instance).string = string; + } +} diff --git a/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_typeUseNullableFieldInjection_DEFAULT_MODE_test.FieldInjection_MembersInjector b/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_typeUseNullableFieldInjection_DEFAULT_MODE_test.FieldInjection_MembersInjector new file mode 100644 index 00000000000..823dc6a633a --- /dev/null +++ b/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_typeUseNullableFieldInjection_DEFAULT_MODE_test.FieldInjection_MembersInjector @@ -0,0 +1,44 @@ +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 FieldInjection_MembersInjector implements MembersInjector { + private final Provider stringProvider; + + public FieldInjection_MembersInjector(Provider stringProvider) { + this.stringProvider = stringProvider; + } + + public static MembersInjector create(Provider stringProvider) { + return new FieldInjection_MembersInjector(stringProvider); + } + + @Override + public void injectMembers(FieldInjection instance) { + injectString(instance, stringProvider.get()); + } + + @InjectedFieldSignature("test.FieldInjection.string") + public static void injectString(Object instance, String string) { + ((FieldInjection) instance).string = string; + } +} diff --git a/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_typeUseNullableFieldInjection_FAST_INIT_MODE_test.FieldInjection_MembersInjector b/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_typeUseNullableFieldInjection_FAST_INIT_MODE_test.FieldInjection_MembersInjector new file mode 100644 index 00000000000..823dc6a633a --- /dev/null +++ b/javatests/dagger/internal/codegen/goldens/MembersInjectionTest_typeUseNullableFieldInjection_FAST_INIT_MODE_test.FieldInjection_MembersInjector @@ -0,0 +1,44 @@ +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 FieldInjection_MembersInjector implements MembersInjector { + private final Provider stringProvider; + + public FieldInjection_MembersInjector(Provider stringProvider) { + this.stringProvider = stringProvider; + } + + public static MembersInjector create(Provider stringProvider) { + return new FieldInjection_MembersInjector(stringProvider); + } + + @Override + public void injectMembers(FieldInjection instance) { + injectString(instance, stringProvider.get()); + } + + @InjectedFieldSignature("test.FieldInjection.string") + public static void injectString(Object instance, String string) { + ((FieldInjection) instance).string = string; + } +} diff --git a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_nullableProvides_test.TestModule_ProvideStringFactory b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_nonTypeUseNullableProvides_test.TestModule_ProvideStringFactory similarity index 99% rename from javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_nullableProvides_test.TestModule_ProvideStringFactory rename to javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_nonTypeUseNullableProvides_test.TestModule_ProvideStringFactory index 76435c1f6d1..b60206cd199 100644 --- a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_nullableProvides_test.TestModule_ProvideStringFactory +++ b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_nonTypeUseNullableProvides_test.TestModule_ProvideStringFactory @@ -42,5 +42,4 @@ public final class TestModule_ProvideStringFactory implements Factory { public static String provideString(TestModule instance) { return instance.provideString(); } -} - +} \ No newline at end of file diff --git a/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_typeUseNullableProvides_test.TestModule_ProvideStringFactory b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_typeUseNullableProvides_test.TestModule_ProvideStringFactory new file mode 100644 index 00000000000..14f0c6f36d1 --- /dev/null +++ b/javatests/dagger/internal/codegen/goldens/ModuleFactoryGeneratorTest_typeUseNullableProvides_test.TestModule_ProvideStringFactory @@ -0,0 +1,43 @@ +package test; + +import dagger.internal.DaggerGenerated; +import dagger.internal.Factory; +import dagger.internal.QualifierMetadata; +import dagger.internal.ScopeMetadata; +import javax.annotation.processing.Generated; + +@ScopeMetadata +@QualifierMetadata +@DaggerGenerated +@Generated( + value = "dagger.internal.codegen.ComponentProcessor", + comments = "https://dagger.dev" +) +@SuppressWarnings({ + "unchecked", + "rawtypes", + "KotlinInternal", + "KotlinInternalInJava", + "cast", + "deprecation" +}) +public final class TestModule_ProvideStringFactory implements Factory { + private final TestModule module; + + public TestModule_ProvideStringFactory(TestModule module) { + this.module = module; + } + + @Override + public @Nullable String get() { + return provideString(module); + } + + public static TestModule_ProvideStringFactory create(TestModule module) { + return new TestModule_ProvideStringFactory(module); + } + + public static @Nullable String provideString(TestModule instance) { + return instance.provideString(); + } +}