diff --git a/compiler/testData/codegen/box/reflection/supertypes/builtInClassSupertypes.kt b/compiler/testData/codegen/box/reflection/supertypes/builtInClassSupertypes.kt index 0fe836de76b..c357f42d24e 100644 --- a/compiler/testData/codegen/box/reflection/supertypes/builtInClassSupertypes.kt +++ b/compiler/testData/codegen/box/reflection/supertypes/builtInClassSupertypes.kt @@ -34,7 +34,7 @@ fun box(): String { check() checkAll() - check(::comparableOfString, ::charSequence, ::serializable) + check(::comparableOfString, ::charSequence, ::serializable, ::any) checkAll(::comparableOfString, ::charSequence, ::serializable, ::any) check(::number, ::comparableOfInt, ::serializable) diff --git a/compiler/testData/codegen/box/reflection/supertypes/simpleSupertypes.kt b/compiler/testData/codegen/box/reflection/supertypes/simpleSupertypes.kt index c17e7ce9529..b019ab3059e 100644 --- a/compiler/testData/codegen/box/reflection/supertypes/simpleSupertypes.kt +++ b/compiler/testData/codegen/box/reflection/supertypes/simpleSupertypes.kt @@ -13,10 +13,15 @@ interface Interface interface Interface2 class ClassAndTwoInterfaces : Interface, Simple(), Interface2 +class ClassWithSuperInterfaceOnly : Interface + +annotation class AnnotationClass + fun any(): Any = null!! fun simple(): Simple = null!! fun interface_(): Interface = null!! fun interface2(): Interface2 = null!! +fun annotation(): Annotation = null!! fun box(): String { with(Simple::class) { @@ -48,5 +53,19 @@ fun box(): String { assertEquals(setOf(Interface::class, Simple::class, Interface2::class, Any::class), allSuperclasses.toSet()) } + with (ClassWithSuperInterfaceOnly::class) { + assertEquals(listOf(::interface_.returnType, ::any.returnType), supertypes) + assertEquals(listOf(Interface::class, Any::class), superclasses) + assertEquals(setOf(::interface_.returnType, ::any.returnType), allSupertypes.toSet()) + assertEquals(setOf(Interface::class, Any::class), allSuperclasses.toSet()) + } + + with (AnnotationClass::class) { + assertEquals(listOf(::annotation.returnType, ::any.returnType), supertypes) + assertEquals(listOf(Annotation::class, Any::class), superclasses) + assertEquals(listOf(::annotation.returnType, ::any.returnType), allSupertypes) + assertEquals(listOf(Annotation::class, Any::class), allSuperclasses) + } + return "OK" } diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt index 7fdcf89fdbc..bf185f61d37 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KClassImpl.kt @@ -17,6 +17,7 @@ package kotlin.reflect.jvm.internal import org.jetbrains.kotlin.builtins.CompanionObjectMapping +import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.load.java.JvmAbi @@ -27,7 +28,9 @@ import org.jetbrains.kotlin.load.kotlin.reflect.ReflectKotlinClass import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.resolve.scopes.MemberScope +import org.jetbrains.kotlin.utils.compactIfPossible import kotlin.jvm.internal.TypeIntrinsics import kotlin.reflect.* import kotlin.reflect.jvm.internal.KDeclarationContainerImpl.MemberBelonginess.DECLARED @@ -113,7 +116,9 @@ internal class KClassImpl(override val jClass: Class) : KDeclaration } val supertypes: List by ReflectProperties.lazySoft { - descriptor.typeConstructor.supertypes.map { kotlinType -> + val kotlinTypes = descriptor.typeConstructor.supertypes + val result = ArrayList(kotlinTypes.size) + kotlinTypes.mapTo(result) { kotlinType -> KTypeImpl(kotlinType) { val superClass = kotlinType.constructor.declarationDescriptor if (superClass !is ClassDescriptor) throw KotlinReflectionInternalError("Supertype not a class: $superClass") @@ -131,6 +136,13 @@ internal class KClassImpl(override val jClass: Class) : KDeclaration } } } + if (!KotlinBuiltIns.isSpecialClassWithNoSupertypes(descriptor) && result.all { + val classKind = DescriptorUtils.getClassDescriptorForType(it.type).kind + classKind == ClassKind.INTERFACE || classKind == ClassKind.ANNOTATION_CLASS + }) { + result += KTypeImpl(descriptor.builtIns.anyType) { Any::class.java } + } + result.compactIfPossible() } val declaredNonStaticMembers: Collection>