samedi 17 juin 2017

Kotlin Reflection operator get implementation

I'm learning Kotlin, current using Fedora 25 OpenJDK 8 and Kotlin 1.1.

I copied the example from the Kotlin website: http://ift.tt/1Tw1I9J and changed the get operator.

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. (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.





Aucun commentaire:

Enregistrer un commentaire