lundi 9 janvier 2017

Kotlin: isAssignableFrom and reflection type checks

In Kotlin (1.0.6), through reflection I need to iterate over the members of a class (let's call it Foo), and do something based on the return type. I can write the following, which works:

Foo::class.members{ m ->
    if(Integer.TYPE.isAssignableFrom(m.returnType.javaType as Class<*>)){
        //do something here
    } else if ...
}

the problem is that the if statement (to handle kotlin.Int) is quite ugly. Is there any better way in Kotlin to achieve the same result without having to rely directly on the Java API?





Aucun commentaire:

Enregistrer un commentaire