Fix KSTypeVarianceResolver for raw types in type arguments.
This CL fixes KSTypeVarianceResolver to properly handle raw types in
type arguments. Previously, it would replace a raw type `Foo` with
its non-raw type `Foo<Object>`. This CL fixes that.
Test: XTypeTest.kt
Change-Id: Ic99d679bbc4ad6ebf0582e9f3b1a0d897ef0b114
diff --git a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSTypeVarianceResolver.kt b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSTypeVarianceResolver.kt
index 338d041..cb245a9 100644
--- a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSTypeVarianceResolver.kt
+++ b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSTypeVarianceResolver.kt
@@ -367,7 +367,11 @@
) {
val declaration = newType.declaration
+ @OptIn(KspExperimental::class)
val arguments: List<KSTypeArgumentWrapper> by lazy {
+ if (resolver.isJavaRawType(newType)) {
+ return@lazy emptyList()
+ }
val arguments = newTypeArguments ?: newType.arguments.indices.map { i ->
KSTypeArgumentWrapper(
originalTypeArg = newType.arguments[i],
diff --git a/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeTest.kt b/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeTest.kt
index babbadec..27d4d87 100644
--- a/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeTest.kt
+++ b/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeTest.kt
@@ -1563,6 +1563,68 @@
}
@Test
+ fun rawTypeNames() {
+ val src = Source.java(
+ "test.Subject",
+ """
+ package test;
+ import java.util.Set;
+ @SuppressWarnings("rawtypes")
+ class Subject {
+ Foo foo;
+ Foo<Foo> fooFoo;
+ Foo<Foo<Foo>> fooFooFoo;
+ Bar<Foo, Foo> barFooFoo;
+ }
+ class Foo<T> {}
+ class Bar<T1, T2> {}
+ """.trimIndent()
+ )
+ runProcessorTest(sources = listOf(src)) { invocation ->
+ fun assertHasTypeName(type: XType, expectedTypeName: String) {
+ assertThat(type.asTypeName().java.toString()).isEqualTo(expectedTypeName)
+ if (invocation.isKsp) {
+ assertThat(type.asTypeName().kotlin.toString()).isEqualTo(expectedTypeName)
+ }
+ }
+
+ val subject = invocation.processingEnv.requireTypeElement("test.Subject")
+ assertHasTypeName(subject.getDeclaredField("foo").type, "test.Foo")
+ assertHasTypeName(subject.getDeclaredField("fooFoo").type, "test.Foo<test.Foo>")
+ assertHasTypeName(
+ subject.getDeclaredField("fooFooFoo").type, "test.Foo<test.Foo<test.Foo>>")
+ assertHasTypeName(
+ subject.getDeclaredField("barFooFoo").type, "test.Bar<test.Foo, test.Foo>")
+
+ // Test manually wrapping raw type using XProcessingEnv#getDeclaredType()
+ subject.getDeclaredField("foo").type.let { foo ->
+ val fooTypeElement = invocation.processingEnv.requireTypeElement("test.Foo")
+ val fooFoo: XType = invocation.processingEnv.getDeclaredType(fooTypeElement, foo)
+ assertHasTypeName(fooFoo, "test.Foo<test.Foo>")
+
+ val fooFooFoo: XType =
+ invocation.processingEnv.getDeclaredType(fooTypeElement, fooFoo)
+ assertHasTypeName(fooFooFoo, "test.Foo<test.Foo<test.Foo>>")
+
+ val barTypeElement = invocation.processingEnv.requireTypeElement("test.Bar")
+ val barFooFoo: XType =
+ invocation.processingEnv.getDeclaredType(barTypeElement, foo, foo)
+ assertHasTypeName(barFooFoo, "test.Bar<test.Foo, test.Foo>")
+ }
+
+ // Test manually unwrapping a type with a raw type argument:
+ subject.getDeclaredField("fooFoo").type.let { fooFoo ->
+ assertHasTypeName(fooFoo.typeArguments.single(), "test.Foo")
+ }
+ subject.getDeclaredField("barFooFoo").type.let { barFooFoo ->
+ assertThat(barFooFoo.typeArguments).hasSize(2)
+ assertHasTypeName(barFooFoo.typeArguments[0], "test.Foo")
+ assertHasTypeName(barFooFoo.typeArguments[1], "test.Foo")
+ }
+ }
+ }
+
+ @Test
fun hasAnnotationWithPackage() {
val kotlinSrc = Source.kotlin(
"KotlinClass.kt",