Add util functions getKPropertyType for both receivers and isNumberedKFunction

This commit is contained in:
Stanislav Erokhin
2016-10-19 21:14:30 +03:00
parent 22e667ad65
commit 53ccfcf195
@@ -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<Name>?,
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<KotlinType>, returnType: KotlinType, mutable: Boolean): KotlinType {
fun getKPropertyType(annotations: Annotations, receiverTypes: List<KotlinType>, 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)