Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

Kotlin: How to get a type of a method parameter

I know I can get the type of a method parameter by using "Method#parameters#name".

However, my parameters are all the subclass of A and I dont want to get the type A. I want to get the subclass name.

if (checkMethod(i)) {
    val type = i.parameters[0].simpleName
    if (!functions.containsKey(type)) {
        functions[type] = HashMap()
    }
    if (!functions[type]?.containsKey(identifier)!!) {
        functions[type]?.put(identifier, ArrayList())
    }
    functions[type]?.get(identifier)?.add(i)
}

Final Solution:

    private fun analysis(clazz: KClass<EventHandler>, identifier: String) {
        clazz.members.forEach {
            if(it is KFunction) {
                if(checkMethod(it)) {
                    val type = methodEventType(it)
                    if(!invokeMethods.containsKey(type)) invokeMethods[type] = HashMap()
                    if(!invokeMethods[type]!!.containsKey(identifier)) invokeMethods[type]!![identifier] = ArrayList()
                    invokeMethods[type]!![identifier]!!.add(it.javaMethod)
                }
            }
        }
    }

    private fun checkMethod(method: KFunction<*>): Boolean {
        method.annotations.forEach {
            if(it is EventSubscriber) {
                val type = method.parameters[1].type.classifier
                if(type is KClass<*>) {
                    if(method.parameters.size == 2 && type.superclasses.contains(Event::class)) {
                        return true
                    }
                }
            }
        }
        return false
    } 

And notice here. I dont know why the method`s first parameter is allways a instance of its class. So the real parameter is start from 1 instead of 0.

question from:https://stackoverflow.com/questions/65848380/kotlin-how-to-get-a-type-of-a-method-parameter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Maybe you'll find this example useful (works with kotlin-reflect:1.4.21)

import kotlin.reflect.full.createType
import kotlin.reflect.full.isSubtypeOf
import kotlin.reflect.jvm.reflect

open class A
val aType = A::class.createType()

class B: A()
class C: A()

val foo = { b: B, c: C ->
    println(b)
    println(c)
}

println(foo.reflect()!!.parameters[0].type.classifier == B::class) // true
println(foo.reflect()!!.parameters[1].type.classifier == C::class) // true
println(foo.reflect()!!.parameters[0].type.isSubtypeOf(aType)) // true

To get all subclasses

println((foo.reflect()!!.parameters[0].type.classifier as KClass<*>).allSuperclasses.contains(A::class)) // true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...