Support KClass.defaultType
This commit is contained in:
@@ -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<K, V> {
|
||||
fun thiz() = this
|
||||
}
|
||||
|
||||
fun simple() = Simple()
|
||||
fun genericIntString() = Generic<Int, String>()
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(javaClass<Simple>(), Simple::class.defaultType.javaType)
|
||||
assertEquals(::simple.returnType, Simple::class.defaultType)
|
||||
|
||||
assertEquals(javaClass<Generic<*, *>>(), Generic::class.defaultType.javaType)
|
||||
assertEquals(Generic<*, *>::thiz.returnType, Generic::class.defaultType)
|
||||
|
||||
// Generic<Int, String> != Generic<K, V>
|
||||
assertNotEquals(::genericIntString.returnType, Generic::class.defaultType)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+6
@@ -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");
|
||||
|
||||
@@ -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 <T : Any> KClass<T>.primaryConstructor: KFunction<T>?
|
||||
((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<K, V>` [defaultType] would return the type `MyMap<K, V>`.
|
||||
*/
|
||||
public val KClass<*>.defaultType: KType
|
||||
get() = KTypeImpl((this as KClassImpl<*>).descriptor.defaultType) { jClass }
|
||||
|
||||
|
||||
/**
|
||||
* Returns static functions declared in this class.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user