lundi 9 juillet 2018

Kotlin reflection on inbuilt classes

I am trying to read the "value" field of a String in Kotlin. I am not very familiar with reflection in Kotlin, so I can't get it to work. This is what I have:

var str: String = "Some string"

val field = String::class.java.getDeclaredField("value")
println(field) // This prints "private final char[] java.lang.String.value"

println(field.get(str)) // This prints [C@66d3c617

When trying to cast char[] to Array, I get this exception:

java.lang.ClassCastException: [C cannot be cast to [Ljava.lang.Character;

What am I doing wrong?





Aucun commentaire:

Enregistrer un commentaire