StragaSevera
11/06/2020, 5:36 PMclass MyDelegatedProperty {
var innerStr: String = ""
operator fun getValue(thisRef: Any, property: KProperty<*>): String {
return innerStr
}
operator fun setValue(thisRef: Any, property: KProperty<*>, value: String) {
innerStr = value
}
fun doSomething() {
println(innerStr)
}
}
class Example {
val str: String by MyDelegatedProperty()
fun main() {
// use the doSomething function from str
}
}
Is this possible somehow? Can I access the object that is created by a delegated property?mapoulin
11/06/2020, 6:07 PMStragaSevera
11/06/2020, 6:10 PMyou can if your variable holds the delegateSorry, did not understand you, what do you mean? Something like:
val strDelegate = MyDelegatedProperty()
val str: String by strDelegate
Yes, it would work, but I want to shrink this declaration from two lines into one and somehow (using reflection?..) get the delegate objectmapoulin
11/06/2020, 6:12 PMStragaSevera
11/06/2020, 6:13 PMmapoulin
11/06/2020, 6:13 PMStragaSevera
11/06/2020, 6:14 PMmapoulin
11/06/2020, 6:17 PMStragaSevera
11/06/2020, 6:22 PMimport kotlin.reflect.KProperty
class MyDelegatedProperty {
var innerStr: String = ""
operator fun getValue(thisRef: Any, property: KProperty<*>): String {
return innerStr
}
operator fun setValue(thisRef: Any, property: KProperty<*>, value: String) {
innerStr = value
}
fun doSomething() {
println(innerStr)
}
}
class Example {
var str: String by MyDelegatedProperty()
}
fun main() {
val example = Example()
example.str = "test"
((example.str) as MyDelegatedProperty).doSomething() // java.lang.ClassCastException: class java.lang.String cannot be cast to class Line_1$MyDelegatedProperty (java.lang.String is in module java.base of loader 'bootstrap'; Line_1$MyDelegatedProperty is in unnamed module of loader org.jetbrains.kotlin.cli.common.repl.ReplClassLoader @3fe59f84)
at Line_3.main(Line_3.kts:4)
}
mapoulin
11/06/2020, 6:23 PMStragaSevera
11/06/2020, 6:23 PMStragaSevera
11/06/2020, 7:31 PMexample::str.getDelegate() as MyDelegatedProperty
. But it throws Exception in thread "main" kotlin.reflect.full.IllegalPropertyDelegateAccessException: Cannot obtain the delegate of a non-accessible property. Use "isAccessible = true" to make the property accessible
evem when I try to make it accessible:
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.reflect.jvm.javaField
import kotlin.reflect.jvm.javaGetter
import kotlin.reflect.jvm.javaSetter
class MyDelegatedProperty {
var innerStr: String = ""
operator fun getValue(thisRef: Any, property: KProperty<*>): String {
return innerStr
}
operator fun setValue(thisRef: Any, property: KProperty<*>, value: String) {
innerStr = value
}
fun doSomething() {
println(innerStr)
}
}
class Example {
var str: String by MyDelegatedProperty()
fun doSomething() {
Example::str.javaField!!.isAccessible = true
Example::str.javaGetter!!.isAccessible = true
Example::str.javaSetter!!.isAccessible = true
::str.isAccessible = true
val delegate = ::str.getDelegate() as MyDelegatedProperty
delegate.doSomething()
}
}
fun main() {
val example = Example()
example.str = "test"
example.doSomething()
}