Files
pyos 8314b7d3c9 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
2022-01-10 21:08:43 +01:00

56 lines
1.6 KiB
Kotlin
Vendored

package test
import android.app.Activity
import android.view.View
import android.widget.*
import kotlinx.android.synthetic.main.layout.*
import kotlinx.android.synthetic.clearFindViewByIdCache
class R {
class id {
companion object {
const val item_detail_container = 0
const val textView1 = 1
const val password = 2
const val textView2 = 3
const val passwordConfirmation = 4
const val login = 5
}
}
}
open class MyActivity(): Activity() {
val textViewWidget = TextView(this)
val editTextWidget = EditText(this)
val buttonWidget = Button(this)
override fun <T : View> findViewById(id: Int): T? {
return when (id) {
R.id.textView1 -> textViewWidget
R.id.password -> editTextWidget
R.id.login -> buttonWidget
else -> null
} as T?
}
open fun findPasswordWidget(): View = null!!
private val textViewReadInInit = textView1
private val passwordReadThroughOverride = findPasswordWidget()
private fun check(expect: String, actual: String) =
if (expect != actual) "'$actual' != '$expect'" else null
public fun box(): String =
check("Button", login.toString())
?: check("TextView", textViewReadInInit.toString())
?: check("EditText", passwordReadThroughOverride.toString())
?: "OK".also { clearFindViewByIdCache() }
}
class MyActivity2 : MyActivity() {
override fun findPasswordWidget() = password
}
fun box(): String = MyActivity2().box()