method toString throws KotlinReflectionInternalError
Asked Answered
W

2

6

Why calling method toString() on function reference results in an error KotlinReflectionInternalError

fun main() {
    fun foo(){}
    ::foo.toString()
}

output:

Exception in thread "main" kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Function 'foo' (JVM signature: main$foo()V) not resolved in class kotlin.jvm.internal.Intrinsics$Kotlin: no members found
    at kotlin.reflect.jvm.internal.KDeclarationContainerImpl.findFunctionDescriptor(KDeclarationContainerImpl.kt:131)
    at kotlin.reflect.jvm.internal.KFunctionImpl$descriptor$2.invoke(KFunctionImpl.kt:56)
    at kotlin.reflect.jvm.internal.KFunctionImpl$descriptor$2.invoke(KFunctionImpl.kt:55)
    at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:93)
    at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:32)
    at kotlin.reflect.jvm.internal.KFunctionImpl.getDescriptor(KFunctionImpl.kt:55)
    at kotlin.reflect.jvm.internal.KFunctionImpl.toString(KFunctionImpl.kt:185)
    at kotlin.jvm.internal.FunctionReference.toString(FunctionReference.java:130)
    at MainKt.main(Main.kt:3)
    at MainKt.main(Main.kt)
Woundwort answered 18/11, 2022 at 8:54 Comment(1)
Note this error only seems to be thrown when kotlin-reflect is available.Credent
C
2

This seems to be a known bug with references to local functions: KT-64873 - Local function reference ::foo doesn't implement KCallable correctly.

import kotlin.reflect.KCallable

fun fail(k: KCallable<Unit>) {
    println(k.name)
    println(k.parameters)
}

fun main() {
    fun foo() { }
    fail(::foo)
}

prints:

foo
Exception in thread "main" kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Function 'foo' (JVM signature: main$foo()V) not resolved in class kotlin.jvm.internal.Intrinsics$Kotlin: no members found
  at kotlin.reflect.jvm.internal.KDeclarationContainerImpl.findFunctionDescriptor(KDeclarationContainerImpl.kt:131)
  at kotlin.reflect.jvm.internal.KFunctionImpl$descriptor$2.invoke(KFunctionImpl.kt:56)
  at kotlin.reflect.jvm.internal.KFunctionImpl$descriptor$2.invoke(KFunctionImpl.kt:55)
  at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:93)
  at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:32)
  at kotlin.reflect.jvm.internal.KFunctionImpl.getDescriptor(KFunctionImpl.kt:55)
  at kotlin.reflect.jvm.internal.KFunctionImpl.getDescriptor(KFunctionImpl.kt:36)
  at kotlin.reflect.jvm.internal.KCallableImpl$_parameters$1.invoke(KCallableImpl.kt:41)
  at kotlin.reflect.jvm.internal.KCallableImpl$_parameters$1.invoke(KCallableImpl.kt:40)
  at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:93)
  at kotlin.reflect.jvm.internal.KCallableImpl.getParameters(KCallableImpl.kt:73)
  at kotlin.jvm.internal.CallableReference.getParameters(CallableReference.java:140)

[...]

Credent answered 1/6, 2024 at 1:20 Comment(0)
A
1

Not an answer but a data point. Moving foo out of main avoids the exception:

fun foo() {}

fun main() {
    ::foo.toString()
}

but having foo reflect on a local (nested) function brings the exception back:

fun foo() {
    fun bar() {}
    ::bar.toString()
}

fun main() {
    ::foo.invoke()
}
Exception in thread "main" kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Function 'bar' (JVM signature: foo$bar()V) not resolved in class kotlin.jvm.internal.Intrinsics$Kotlin: no members found
    at kotlin.reflect.jvm.internal.KDeclarationContainerImpl.findFunctionDescriptor(KDeclarationContainerImpl.kt:131)
    at kotlin.reflect.jvm.internal.KFunctionImpl$descriptor$2.invoke(KFunctionImpl.kt:56)
    at kotlin.reflect.jvm.internal.KFunctionImpl$descriptor$2.invoke(KFunctionImpl.kt:55)
    at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:93)
    at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:32)
    at kotlin.reflect.jvm.internal.KFunctionImpl.getDescriptor(KFunctionImpl.kt:55)
    at kotlin.reflect.jvm.internal.KFunctionImpl.toString(KFunctionImpl.kt:185)
    at kotlin.jvm.internal.FunctionReference.toString(FunctionReference.java:130)
Archil answered 31/5, 2024 at 17:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.