Kotlin Reflection operator get 实现

Kotlin Reflection operator get implementation

我正在学习 Kotlin,目前使用 Fedora 25 OpenJDK 8 和 Kotlin 1.1。

我从 Kotlin 网站复制了示例:https://kotlinlang.org/docs/reference/delegated-properties.html 并更改了 get 运算符。

class Example {
var p: String by Delegate()
}

class Delegate {
    operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
        // My implementation
        return property.getter.call(thisRef) as String
    }

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
        println("$value has been assigned to '${property.name} in $thisRef.'")
    }
}

阅读反射文档 getter 需要对象实例而不需要其他参数,但我只遇到了以下错误。 (错误是缩写,因为它太大了,它在递归中。)

.
.
.
at info.malk.Example.getP(Delegation.kt)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at kotlin.reflect.jvm.internal.FunctionCaller$Method.callMethod(FunctionCaller.kt:98)
at kotlin.reflect.jvm.internal.FunctionCaller$InstanceMethod.call(FunctionCaller.kt:115)
at kotlin.reflect.jvm.internal.KCallableImpl.call(KCallableImpl.kt:107)
at info.malk.Delegate.getValue(Delegation.kt:32)
at info.malk.Example.getP(Delegation.kt)
.
.
.
Caused by: java.lang.reflect.InvocationTargetException
    ... 1024 more
Caused by: java.lang.WhosebugError
    ... 1024 more

Process finished with exit code 1

求助。

Translation Rule 说:

For instance, for the property prop the hidden property prop$delegate is generated, and the code of the accessors(getter/setter) simply delegates to this additional property.

因此 kotlin 属性 会将 getter/setter 分派给 delegator。当您 get/set 委托处理程序中的 属性 上的值时(getValue/setValue) 将导致递归调用。

你的 Delegate 应该更像这样:

class Delegate<T> {

    private var value: T? = null;
    //           ^--- store the proeprty value internal

    operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
        return value ?: throw UninitializedPropertyAccessException();
    }

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        this.value = value;
    }
}