Add util functions getKPropertyType for both receivers and isNumberedKFunction
This commit is contained in:
@@ -30,10 +30,10 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
|||||||
import org.jetbrains.kotlin.serialization.deserialization.NotFoundClasses
|
import org.jetbrains.kotlin.serialization.deserialization.NotFoundClasses
|
||||||
import org.jetbrains.kotlin.serialization.deserialization.findClassAcrossModuleDependencies
|
import org.jetbrains.kotlin.serialization.deserialization.findClassAcrossModuleDependencies
|
||||||
import org.jetbrains.kotlin.types.*
|
import org.jetbrains.kotlin.types.*
|
||||||
import java.util.*
|
|
||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
val KOTLIN_REFLECT_FQ_NAME = FqName("kotlin.reflect")
|
val KOTLIN_REFLECT_FQ_NAME = FqName("kotlin.reflect")
|
||||||
|
val K_FUNCTION_PREFIX = "KFunction"
|
||||||
|
|
||||||
class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: NotFoundClasses) {
|
class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: NotFoundClasses) {
|
||||||
private val kotlinReflectScope: MemberScope by lazy(LazyThreadSafetyMode.PUBLICATION) {
|
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 kClass: ClassDescriptor by ClassLookup(1)
|
||||||
val kProperty0: 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>?,
|
parameterNames: List<Name>?,
|
||||||
returnType: KotlinType,
|
returnType: KotlinType,
|
||||||
builtIns: KotlinBuiltIns
|
builtIns: KotlinBuiltIns
|
||||||
): KotlinType {
|
): SimpleType {
|
||||||
val arguments = getFunctionTypeArgumentProjections(receiverType, parameterTypes, parameterNames, returnType, builtIns)
|
val arguments = getFunctionTypeArgumentProjections(receiverType, parameterTypes, parameterNames, returnType, builtIns)
|
||||||
val classDescriptor = getKFunction(arguments.size - 1 /* return type */)
|
val classDescriptor = getKFunction(arguments.size - 1 /* return type */)
|
||||||
return KotlinTypeFactory.simpleNotNullType(annotations, classDescriptor, arguments)
|
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) {
|
val classDescriptor = when (receiverTypes.size) {
|
||||||
0 -> when {
|
0 -> when {
|
||||||
mutable -> kMutableProperty0
|
mutable -> kMutableProperty0
|
||||||
@@ -130,6 +130,15 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not
|
|||||||
hasFqName(descriptor, KotlinBuiltIns.FQ_NAMES.kProperty2)
|
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 {
|
private fun hasFqName(typeConstructor: TypeConstructor, fqName: FqNameUnsafe): Boolean {
|
||||||
val descriptor = typeConstructor.declarationDescriptor
|
val descriptor = typeConstructor.declarationDescriptor
|
||||||
return descriptor is ClassDescriptor && hasFqName(descriptor, fqName)
|
return descriptor is ClassDescriptor && hasFqName(descriptor, fqName)
|
||||||
|
|||||||
Reference in New Issue
Block a user