Skip to content

Commit

Permalink
Add tests for nullable field injection and nullable provides methods.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 675332372
  • Loading branch information
java-team-github-bot authored and Dagger Team committed Sep 18, 2024
1 parent 5f76186 commit 397fdbf
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 9 deletions.
65 changes: 65 additions & 0 deletions javatests/dagger/internal/codegen/MembersInjectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object[]> parameters() {
return CompilerMode.TEST_PARAMETERS;
Expand Down Expand Up @@ -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 =
Expand Down
53 changes: 46 additions & 7 deletions javatests/dagger/internal/codegen/ModuleFactoryGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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",
Expand All @@ -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);
Expand All @@ -299,7 +336,8 @@ public void providesMethodReturnsProduced() {
});
}

@Test public void multipleProvidesMethods() {
@Test
public void multipleProvidesMethods() {
Source classXFile =
CompilerTests.javaSource("test.X",
"package test;",
Expand Down Expand Up @@ -344,7 +382,8 @@ public void providesMethodReturnsProduced() {
});
}

@Test public void providesSetElement() {
@Test
public void providesSetElement() {
Source moduleFile =
CompilerTests.javaSource(
"test.TestModule",
Expand Down
Original file line number Diff line number Diff line change
@@ -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<FieldInjection> {
private final Provider<String> stringProvider;

public FieldInjection_MembersInjector(Provider<String> stringProvider) {
this.stringProvider = stringProvider;
}

public static MembersInjector<FieldInjection> create(Provider<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<FieldInjection> {
private final Provider<String> stringProvider;

public FieldInjection_MembersInjector(Provider<String> stringProvider) {
this.stringProvider = stringProvider;
}

public static MembersInjector<FieldInjection> create(Provider<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<FieldInjection> {
private final Provider<String> stringProvider;

public FieldInjection_MembersInjector(Provider<String> stringProvider) {
this.stringProvider = stringProvider;
}

public static MembersInjector<FieldInjection> create(Provider<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<FieldInjection> {
private final Provider<String> stringProvider;

public FieldInjection_MembersInjector(Provider<String> stringProvider) {
this.stringProvider = stringProvider;
}

public static MembersInjector<FieldInjection> create(Provider<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ public final class TestModule_ProvideStringFactory implements Factory<String> {
public static String provideString(TestModule instance) {
return instance.provideString();
}
}

}
Original file line number Diff line number Diff line change
@@ -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<String> {
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();
}
}

0 comments on commit 397fdbf

Please sign in to comment.