简体   繁体   中英

Kotlin set lazy field through Java reflection

I'm trying to play around with Java reflection in Kotlin, and I have the following field in my Kotlin class:

val tree: Tree by lazy { 
    getTree(hashGroup, service) 
}

I'd like to set this field through Java reflection, and so far I got to this point:

val tField = transaction::class.java.getDeclaredField("tree\$delegate")
tField.isAccessible = true
tField.set(transaction, newTree)

Obviously this is not going to work, because the tree field has a delegate ( tree$delegate ) and it wants me to set it to a Lazy class type and not Tree type.

I know the easiest would be to actually add a setter for this field in the main class but I can't really modify the API and I need this for a resilience test case. Is this even possible to do?

Just create a Lazy<T> instance like you normally would!

If you want to set it to the constant value newTree :

tField.set(transaction, lazyOf(newTree))

You can also set a new block of code for it to be evaluated lazily:

tField.set(transaction, lazy {
    Tree().apply {
        // do something lazily to this tree...
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM