From 53ccfcf195b6788c6394dbe5ca20279a78cf8d45 Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Wed, 19 Oct 2016 21:14:30 +0300 Subject: [PATCH] Add util functions getKPropertyType for both receivers and isNumberedKFunction --- .../kotlin/builtins/ReflectionTypes.kt | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt index e9a147a4bc3..dfafb8aeb1d 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt @@ -30,10 +30,10 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.serialization.deserialization.NotFoundClasses import org.jetbrains.kotlin.serialization.deserialization.findClassAcrossModuleDependencies import org.jetbrains.kotlin.types.* -import java.util.* import kotlin.reflect.KProperty val KOTLIN_REFLECT_FQ_NAME = FqName("kotlin.reflect") +val K_FUNCTION_PREFIX = "KFunction" class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: NotFoundClasses) { private val kotlinReflectScope: MemberScope by lazy(LazyThreadSafetyMode.PUBLICATION) { @@ -52,7 +52,7 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not } } - fun getKFunction(n: Int): ClassDescriptor = find("KFunction$n", n + 1) + fun getKFunction(n: Int): ClassDescriptor = find("$K_FUNCTION_PREFIX$n", n + 1) val kClass: ClassDescriptor by ClassLookup(1) val kProperty0: ClassDescriptor by ClassLookup(1) @@ -72,13 +72,13 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not parameterNames: List?, returnType: KotlinType, builtIns: KotlinBuiltIns - ): KotlinType { + ): SimpleType { val arguments = getFunctionTypeArgumentProjections(receiverType, parameterTypes, parameterNames, returnType, builtIns) val classDescriptor = getKFunction(arguments.size - 1 /* return type */) return KotlinTypeFactory.simpleNotNullType(annotations, classDescriptor, arguments) } - fun getKPropertyType(annotations: Annotations, receiverTypes: List, returnType: KotlinType, mutable: Boolean): KotlinType { + fun getKPropertyType(annotations: Annotations, receiverTypes: List, returnType: KotlinType, mutable: Boolean): SimpleType { val classDescriptor = when (receiverTypes.size) { 0 -> when { mutable -> kMutableProperty0 @@ -130,6 +130,15 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not hasFqName(descriptor, KotlinBuiltIns.FQ_NAMES.kProperty2) } + fun isNumberedKFunction(type: KotlinType): Boolean { + val descriptor = type.constructor.declarationDescriptor as? ClassDescriptor ?: return false + val shortName = descriptor.name.asString() + + return shortName.length > K_FUNCTION_PREFIX.length && + shortName.startsWith(K_FUNCTION_PREFIX) && + DescriptorUtils.getFqName(descriptor).parent().toSafe() == KOTLIN_REFLECT_FQ_NAME + } + private fun hasFqName(typeConstructor: TypeConstructor, fqName: FqNameUnsafe): Boolean { val descriptor = typeConstructor.declarationDescriptor return descriptor is ClassDescriptor && hasFqName(descriptor, fqName)