isInitialized - Backing field of lateinit var is not accessible at this point

笑着哭i 提交于 2020-02-20 06:20:13

问题


I'm trying to check if a lateinit property has been initialized.
In Kotlin 1.2 we now have the isInitialized method for that. It works when I do that in the class where the lateinit property is declared. But when I try to call this from another class I get the following warning:

Backing field of 'lateinit var foo: Bar' is not accessible at this point

My model class (let's say Person) is written in Java
Two other classes (let's say Test1 and Test2) are written in Kotlin

Example:

class Test1 {
    lateinit var person: Person

    fun method() {
        if (::person.isInitialized) {
            // This works
        }
    }
}

-

class Test2 {
    lateinit var test1: Test1

    fun method() {
        if (test1::person.isInitialized) {
            // Error
        }
    }
}

Any chance to get this working?

My current workaround is to make a method in Test1 which returns isInitialized from the person property.

fun isPersonInitialized(): Boolean = ::person.isInitialized

//in Test2:
if (test1.isPersonInitialized()) {
    // Works
}

回答1:


According to the docs:

This check is only available for the properties that are lexically accessible, i.e. declared in the same type or in one of the outer types, or at top level in the same file.

Which is why you cannot check that in the main function.




回答2:


A really simple workaround to the constraints described by the accepted answer is the following:

class LateClass {
    lateinit var thing: Thing
    fun isThingInitialized() = ::thing.isInitialized
}

class Client {
    val lateClass = LateClass()
    ... things happen ...
    if (lateClass.isThingInitialized() {
        // do stuff with lateClass.thing, safely
    }
}



回答3:


You can check if a lateinit property has been initialized by catching the UninitializedPropertyAccessException like so:

class Person {
    lateinit var name: String
}

class Test {
    lateinit var person1: Person
    lateinit var person2: Person

    init {
        try {
            println(person1.name)
        }
        catch(e: UninitializedPropertyAccessException) { // is thrown because person1 has not been initialized
            println("ups")
        }

        person2 = Person()

        try {
            println(person2.name)
        }
        catch(e: UninitializedPropertyAccessException) { // is thrown because person2.name has not been initialized
            println("ups")
        }
    }
}

fun main(args: Array<String>) {
   Test() // ups ups
}


来源:https://stackoverflow.com/questions/47549015/isinitialized-backing-field-of-lateinit-var-is-not-accessible-at-this-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!