IR/Android: initialize the findViewById cache before super()

In general, calling open methods in open class constructors is unsafe
because their overrides will see an uninitialized instance. This change
makes it at least possible to use the view cache in such cases.

^KT-50627 Fixed
This commit is contained in:
pyos
2022-01-04 11:32:05 +01:00
committed by Alexander Udalov
parent 08d062dcf9
commit 8314b7d3c9
2 changed files with 43 additions and 36 deletions
@@ -19,7 +19,7 @@ class R {
}
}
class MyActivity(): Activity() {
open class MyActivity(): Activity() {
val textViewWidget = TextView(this)
val editTextWidget = EditText(this)
val buttonWidget = Button(this)
@@ -33,20 +33,23 @@ class MyActivity(): Activity() {
} as T?
}
private val textViewString = textView1.toString()
open fun findPasswordWidget(): View = null!!
public fun box(): String {
val result = when {
textViewString == "TextView" && password.toString() == "EditText" && login.toString() == "Button" -> "OK"
else -> ""
}
private val textViewReadInInit = textView1
private val passwordReadThroughOverride = findPasswordWidget()
clearFindViewByIdCache()
private fun check(expect: String, actual: String) =
if (expect != actual) "'$actual' != '$expect'" else null
return result
}
public fun box(): String =
check("Button", login.toString())
?: check("TextView", textViewReadInInit.toString())
?: check("EditText", passwordReadThroughOverride.toString())
?: "OK".also { clearFindViewByIdCache() }
}
fun box(): String {
return MyActivity().box()
class MyActivity2 : MyActivity() {
override fun findPasswordWidget() = password
}
fun box(): String = MyActivity2().box()