简体   繁体   English

Kotlin Reflection运算符获得实施

[英]Kotlin Reflection operator get implementation

I'm learning Kotlin, current using Fedora 25 OpenJDK 8 and Kotlin 1.1. 我正在学习Kotlin,当前正在使用Fedora 25 OpenJDK 8和Kotlin 1.1。

I copied the example from the Kotlin website: https://kotlinlang.org/docs/reference/delegated-properties.html and changed the get operator. 我从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.'")
    }
}

Reading the Reflection documentation the getter expects the object instance and no other parameter, but I only achieved the following error. 阅读反射文档,getter会期望对象实例,而没有其他参数,但是我仅遇到以下错误。 (Error is abbreviate because it's too big, it's in recursion.) (错误是缩写,因为它太大,正在递归中。)

.
.
.
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.StackOverflowError
    ... 1024 more

Process finished with exit code 1

Help. 救命。

Translation Rule says: 翻译规则说:

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. 例如,对于属性propprop$delegate生成隐藏的属性prop$delegate ,而accessors( getter / setter )的代码仅委托给此附加属性。

so the kotlin property will dispatch the getter/setter to the delegator . 因此kotlin 属性会将getter / setter发送给delegator when you get/set the value on a property around in the delegate handlers ( getValue/setValue ) will result in recursive call. 当您在委托处理程序中获取/设置 属性值时( getValue / setValue ),将导致递归调用。

your Delegate should more like this: 您的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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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