From 7c28a2e7323876b1556e1d800b3001087b50df11 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 25 Aug 2015 12:40:56 +0300 Subject: [PATCH] Support KClass.defaultType --- .../reflection/classes/defaultType.kt | 25 +++++++++++++++++++ ...lackBoxWithStdlibCodegenTestGenerated.java | 6 +++++ .../src/kotlin/reflect/KClassExtensions.kt | 9 +++++++ 3 files changed, 40 insertions(+) create mode 100644 compiler/testData/codegen/boxWithStdlib/reflection/classes/defaultType.kt diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/classes/defaultType.kt b/compiler/testData/codegen/boxWithStdlib/reflection/classes/defaultType.kt new file mode 100644 index 00000000000..e99f3463744 --- /dev/null +++ b/compiler/testData/codegen/boxWithStdlib/reflection/classes/defaultType.kt @@ -0,0 +1,25 @@ +import kotlin.reflect.defaultType +import kotlin.reflect.jvm.javaType +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals + +class Simple +class Generic { + fun thiz() = this +} + +fun simple() = Simple() +fun genericIntString() = Generic() + +fun box(): String { + assertEquals(javaClass(), Simple::class.defaultType.javaType) + assertEquals(::simple.returnType, Simple::class.defaultType) + + assertEquals(javaClass>(), Generic::class.defaultType.javaType) + assertEquals(Generic<*, *>::thiz.returnType, Generic::class.defaultType) + + // Generic != Generic + assertNotEquals(::genericIntString.returnType, Generic::class.defaultType) + + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java index a88db02fb82..010ffa0c704 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java @@ -3012,6 +3012,12 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode doTestWithStdlib(fileName); } + @TestMetadata("defaultType.kt") + public void testDefaultType() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/classes/defaultType.kt"); + doTestWithStdlib(fileName); + } + @TestMetadata("jvmName.kt") public void testJvmName() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/classes/jvmName.kt"); diff --git a/core/reflection.jvm/src/kotlin/reflect/KClassExtensions.kt b/core/reflection.jvm/src/kotlin/reflect/KClassExtensions.kt index f84643f8ec9..a4a2ec24650 100644 --- a/core/reflection.jvm/src/kotlin/reflect/KClassExtensions.kt +++ b/core/reflection.jvm/src/kotlin/reflect/KClassExtensions.kt @@ -19,6 +19,7 @@ package kotlin.reflect import org.jetbrains.kotlin.descriptors.ConstructorDescriptor import kotlin.reflect.jvm.internal.KClassImpl import kotlin.reflect.jvm.internal.KFunctionImpl +import kotlin.reflect.jvm.internal.KTypeImpl /** * Returns the primary constructor of this class, or `null` if this class has no primary constructor. @@ -30,6 +31,14 @@ public val KClass.primaryConstructor: KFunction? ((it as KFunctionImpl).descriptor as ConstructorDescriptor).isPrimary } +/** + * Returns a type corresponding to the given class with type parameters of that class substituted as the corresponding arguments. + * For example, for class `MyMap` [defaultType] would return the type `MyMap`. + */ +public val KClass<*>.defaultType: KType + get() = KTypeImpl((this as KClassImpl<*>).descriptor.defaultType) { jClass } + + /** * Returns static functions declared in this class. */