dimanche 10 décembre 2017

How to get generics parameters types via reflection?

I need to get all function argument info with generics parameters. Is that possible?

As I see, it probably works with Array (outputs me java.lang.Integer[]) class, but not with Map. Why? I want to get K and V parameters of Map via reflection while iterate through parameters of method.

This code

class Cls {
    fun test(arg: Map<Int, Cls>) {
    }
    fun test2(arg: Array<Int>) {
    }
}

fun main(args: Array<String>) {
    val a = Cls()
    a.javaClass.methods.forEach {
        println(it.name)
        it.parameterTypes.forEach{ println("    " + it.canonicalName) }
    }
}

Outputs:

test java.util.Map

test2 java.lang.Integer[]





Aucun commentaire:

Enregistrer un commentaire