如何确定哪种类型具有通用模板参数?

package tutorial.test.reflection

import kotlin.reflect.KClass
import kotlin.reflect.KType

interface ModelElement
interface Parentable<T : ModelElement>

interface Parent : ModelElement

interface Interface1<PARENT_TEMPLATE : ModelElement> : Parentable<PARENT_TEMPLATE>
interface Interface2 : Interface1<Parent>

// Returns just all classifiers in the inheritance hierachy
fun getAllSupertypes(cls: KClass<*>): List<KType> = cls.supertypes.let {
    val types = it.toMutableList()
    cls.supertypes.forEach { types.addAll(getAllSupertypes(it.classifier as KClass<*>)) }
    return types.filter { it.classifier != Any::class }
}

fun main(args: Array<String>) {
    val t = getAllSupertypes(Interface2::class)
    t.forEach {
        println(it)
        if(it.classifier == Parentable::class) {
            it.arguments.forEach {
                println("\t-> ${it.type} ${(it.type?.classifier == Parent::class)}")
                // The template parameter must be "tutorial.test.reflection.Parent" but is PARENT_TEMPLATE
            }

        }
    }
}

输出

tutorial.test.reflection.Interface1

tutorial.test.reflection.Parentable
-> PARENT_TEMPLATE否
(it.type?.classifier == Parent::class)应该结果为true。如何从继承层次结构中将通用模板参数“PARENT_TEMPLATE”解析为接口(interface)父级?

最佳答案

那是我的解决方案,它的工作原理是:

import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.KTypeParameter
import kotlin.reflect.KTypeProjection


private data class SuperType(private val type: KType, val owner: Class) {
    val typeProjections: List<TypeProjection>
        get() = type.arguments.map { TypeProjection(it) }
    val asClass: Class
        get() = Class(type.classifier as KClass<*>, owner)

    fun isTypeOf(cls: KClass<*>) = type.classifier == cls
}

private data class TypeProjection(private val type: KTypeProjection) {
    val isClass: Boolean
        get() = type.type?.classifier is KClass<*>
    val isTemplate: Boolean
        get() = type.type?.classifier is KTypeParameter
    val asClass: KClass<*>
        get() = type.type?.classifier as KClass<*>
    val name: String?
        get() {
            var name: String? = null
            if (isClass)
                name = (type.type?.classifier as KClass<*>).simpleName
            else if (isTemplate)
                name = (type.type?.classifier as KTypeParameter).name
            return name
        }
}

private data class TypeParameter(private val type: KTypeParameter) {
    val name: String
        get() = type.name
}

private data class Class(private val type: KClass<*>, val root: Class? = null) {
    private val supertypes: List<SuperType>
        get() = type.supertypes.filter { it.classifier is KClass<*> && it.classifier != Any::class }
            .map { SuperType(it, this) }
    private val typeParameters: List<TypeParameter>
        get() = type.typeParameters.map { TypeParameter(it) }

    fun findInHierarchy(cls: KClass<*>): Class? {
        for (s in supertypes) {
            if (s.isTypeOf(cls)) {
                return s.asClass;
            } else {
                val s2 = s.asClass.findInHierarchy(cls)
                if (s2 != null)
                    return s2
            }
        }
        return null
    }

    private fun mapTemplateToClass(name: String): KClass<*>? {
        val tp = typeParameters.firstOrNull { it.name == name }
        if (tp != null && root != null) {
            val clsSuperType = root.supertypes.firstOrNull { it.isTypeOf(type) }
            if (clsSuperType != null) {
                val index = typeParameters.indexOf(tp)
                if (index >= 0) {
                    val projection = clsSuperType.typeProjections[index]
                    if (projection.isClass)
                        return projection.asClass
                    else if (projection.isTemplate) {
                        val projectionName = projection.name
                        if (projectionName != null) {
                            return clsSuperType.owner.mapTemplateToClass(projectionName)
                        }
                    }

                }
            }
        }
        return null
    }

    fun resolveAllTemplates(): Map<String, KClass<*>> {
        val paramNames = typeParameters.map { it.name }
        return paramNames.map { it to mapTemplateToClass(it) }.filter { it.second != null }
            .map { it.first to it.second!! }.toMap()
    }
}

fun KClass<*>.resolveTemplates(parent: KClass<*>): Map<String, KClass<*>>? =
    Class(this).findInHierarchy(parent)?.resolveAllTemplates()

interface P1<P1_T1, P1_T2>
interface P2<P2_T1, P2_T2>
interface L1<L_T1, L_T2> : P1<L_T1, L_T2>
interface L2<L_T1, L_T2> : P2<L_T1, L_T2>
interface L3 : L1<Long, String>, L2<String, Any>

fun main(args: Array<String>) {
    var map = L3::class.resolveTemplates(P1::class)
    println(map) // Output: {P1_T1=class kotlin.Long, P1_T2=class kotlin.String}
    map = L3::class.resolveTemplates(P2::class)
    println(map) // Output: {P2_T1=class kotlin.String, P2_T2=class kotlin.Any}
}

09-25 22:06