Skip to content

Commit

Permalink
Add a test for Kotlin nullable field injection.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 676595690
  • Loading branch information
java-team-github-bot authored and Dagger Team committed Sep 19, 2024
1 parent f8a09b2 commit 8d9f082
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
22 changes: 22 additions & 0 deletions javatests/dagger/internal/codegen/MembersInjectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});
}
}
Original file line number Diff line number Diff line change
@@ -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<MyClass> {
private final Provider<String> nullableStringProvider;

private final Provider<Object> nullableObjectProvider;

public MyClass_MembersInjector(Provider<String> nullableStringProvider,
Provider<Object> nullableObjectProvider) {
this.nullableStringProvider = nullableStringProvider;
this.nullableObjectProvider = nullableObjectProvider;
}

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

private final Provider<Object> nullableObjectProvider;

public MyClass_MembersInjector(Provider<String> nullableStringProvider,
Provider<Object> nullableObjectProvider) {
this.nullableStringProvider = nullableStringProvider;
this.nullableObjectProvider = nullableObjectProvider;
}

public static MembersInjector<MyClass> create(Provider<String> nullableStringProvider,
Provider<Object> 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;
}
}

0 comments on commit 8d9f082

Please sign in to comment.