Merge "Fix XType#superClass when there's no explicit superclass." into androidx-main
diff --git a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt
index 1081550..2807df12 100644
--- a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt
+++ b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt
@@ -98,7 +98,7 @@
override val filer: XFiler = KspFiler(codeGenerator, messager)
val commonTypes
- get() = CommonTypes(resolver)
+ get() = CommonTypes()
val voidType
get() = KspVoidType(
@@ -308,15 +308,7 @@
return returnType(type1).isSameType(returnType(type2))
}
- class CommonTypes(resolver: Resolver) {
- val nullableInt by lazy {
- resolver.builtIns.intType.makeNullable()
- }
- val nullableLong by lazy {
- resolver.builtIns.longType.makeNullable()
- }
- val nullableByte by lazy {
- resolver.builtIns.byteType.makeNullable()
- }
+ inner class CommonTypes() {
+ val anyType: XType = requireType("kotlin.Any")
}
}
diff --git a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspTypeElement.kt b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspTypeElement.kt
index 54e64ab..2e724a5 100644
--- a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspTypeElement.kt
+++ b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspTypeElement.kt
@@ -94,17 +94,19 @@
if (isInterface()) {
// interfaces don't have super classes (they do have super types)
null
+ } else if (this == env.commonTypes.anyType.typeElement) {
+ null
} else {
- declaration.superTypes.firstOrNull {
- val type =
- it.resolve().declaration as? KSClassDeclaration ?: return@firstOrNull false
- type.classKind == ClassKind.CLASS
- }?.let {
- env.wrap(
- ksType = it.resolve(),
- allowPrimitives = false
- )
- }
+ declaration.superTypes
+ .map { it.resolve() }
+ .singleOrNull {
+ (it.declaration as? KSClassDeclaration)?.classKind == ClassKind.CLASS
+ }?.let {
+ env.wrap(
+ ksType = it,
+ allowPrimitives = false
+ )
+ } ?: env.commonTypes.anyType
}
}
diff --git a/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeElementTest.kt b/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeElementTest.kt
index d5dd62f..d2822e3 100644
--- a/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeElementTest.kt
+++ b/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeElementTest.kt
@@ -232,6 +232,55 @@
}
@Test
+ fun superTypeWithNoSuperClass() {
+ runTest(
+ sources = listOf(
+ Source.kotlin(
+ "foo.bar.KotlinClass.kt",
+ """
+ package foo.bar
+ class KotlinClass
+ class KotlinClassWithInterface : KotlinInterface
+ interface KotlinInterface
+ """.trimIndent()
+ ),
+ Source.java(
+ "foo.bar.JavaClass",
+ """
+ package foo.bar;
+ class JavaClass {}
+ class JavaClassWithInterface implements JavaInterface {}
+ interface JavaInterface {}
+ """.trimIndent()
+ )
+ )
+ ) { invocation ->
+ invocation.processingEnv.requireTypeElement("foo.bar.KotlinClass").let {
+ assertThat(it.superClass?.asTypeName()).isEqualTo(XTypeName.ANY_OBJECT)
+ }
+ invocation.processingEnv.requireTypeElement("foo.bar.KotlinClassWithInterface").let {
+ assertThat(it.superClass?.asTypeName()).isEqualTo(XTypeName.ANY_OBJECT)
+ }
+ invocation.processingEnv.requireTypeElement("foo.bar.JavaClass").let {
+ assertThat(it.superClass?.asTypeName()).isEqualTo(XTypeName.ANY_OBJECT)
+ }
+ invocation.processingEnv.requireTypeElement("foo.bar.JavaClassWithInterface").let {
+ assertThat(it.superClass?.asTypeName()).isEqualTo(XTypeName.ANY_OBJECT)
+ }
+ }
+ }
+
+ @Test
+ fun superTypeOfAny() {
+ runTest(sources = listOf()) { invocation ->
+ val any = invocation.processingEnv.requireTypeElement(Any::class)
+ val obj = invocation.processingEnv.requireTypeElement(Object::class)
+ assertThat(any.superClass).isNull()
+ assertThat(obj.superClass).isNull()
+ }
+ }
+
+ @Test
fun superInterfaces() {
val src = Source.kotlin(
"foo.kt",