The same code,it works fine before . Now throw NullPointerException when use kotlin 1.5.30
open class Father {
private val bundle = mutableMapOf<String, Any>()
fun <V : Any> Class<V>.getMapValue(key: String): V? = when {
this == java.lang.Integer::class.java || this == Int::class.java -> (bundle[key]?.toString()
?.toIntOrNull() ?: 0) as? V
this == java.lang.String::class.java -> (bundle[key]?.toString() ?: "") as V
this == java.lang.Boolean::class.java || this == Boolean::class.java -> (bundle[key]?.toString()
?.toBooleanStrictOrNull() ?: false) as? V
else -> bundle[key] as? V
}
fun <V : Any> setValue(key: String, value: V): V? {
return bundle.put(key, value) as? V
}
protected inline operator fun <reified V : Any> Father.setValue(
thisRef: Any?,
property: KProperty<*>,
value: V?
): V? {
return value?.let {
setValue(property.name, it)
}
}
protected inline operator fun <reified V : Any> Father.getValue(
thisRef: Any?,
property: KProperty<*>
): V {
val v = V::class.java.getMapValue(property.name)
return v ?: throw NullPointerException("no key ${property.name} found in map")
}
}
class Child : Father() {
var age: Int by this
var sex: Boolean by this
var addr: String by this
override fun toString(): String {
return "{age:$age,sex:$sex,addr:$addr}"
}
}
then i decompiler the child class,it shows that KProperty object allways null