samedi 30 octobre 2021

Cannot cast reflection field into Map

I was trying to get cast reflection field to Map, but it gives me this error:

java.lang.ClassCastException: java.lang.reflect.Field cannot be cast to java.util.Map

How do I fix this?

Minimal Reproducible Example:


open class Base {
    var inherited: Map<String, String> = mapOf(Pair("testing", "testing"))
}

class Child: Base() {
    var notInherited: Int = 100
}


fun main() {
    val c = Child()
    var inheritedMap: Map<String, String> = c.javaClass.superclass.getDeclaredField("inherited") as Map<String, String> 
    println(inheritedMap)
}

link to code playground: https://pl.kotl.in/xW8g4pNsX





Aucun commentaire:

Enregistrer un commentaire