exception - Kotlin/Android – 带有 lambda 的数据类中的 KotlinReflectionInternalError

标签 exception reflection lambda kotlin kotlinx.coroutines

kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Introspecting local functions, lambdas, anonymous functions and local variables is not yet fully supported in Kotlin reflection

此异常来自数据类的toString()

数据类包含一个 lambda。

我无法在我的环境中重现它。

我需要重写 toString() 来排除 lambda 吗?或者数据类中根本不允许使用 lambda?

data class PersistJob(
        private val id: Int,
        private val delay: Long = 10_000L,
        private val maxDelay: Long = 60_000L,
        private val iteration: Int = 0,
        private val block: suspend (Int) -> Boolean) {

    fun getDelay() = minOf(delay, maxDelay)
    fun withDelayIncreased() = copy(
            delay = minOf(delay * 2, maxDelay),
            iteration = iteration + 1)

    suspend fun execute() = block(iteration)
}

产生错误的行:

val job: PersistJob = ...
log.debug("start job id($id): $job")`// job.toString()

堆栈跟踪:

at kotlin.reflect.jvm.internal.EmptyContainerForLocal.fail(SourceFile:41)
at kotlin.reflect.jvm.internal.EmptyContainerForLocal.getFunctions(SourceFile:37)
at kotlin.reflect.jvm.internal.KDeclarationContainerImpl.findFunctionDescriptor(SourceFile:145)
at kotlin.reflect.jvm.internal.KFunctionImpl$descriptor$2.invoke(SourceFile:54)
at kotlin.reflect.jvm.internal.KFunctionImpl$descriptor$2.invoke(SourceFile:34)
at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(SourceFile:93)
at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(SourceFile:32)
at kotlin.reflect.jvm.internal.KFunctionImpl.getDescriptor(SourceFile)
at kotlin.reflect.jvm.internal.ReflectionFactoryImpl.renderLambdaToString(SourceFile:59)
at kotlin.jvm.internal.Reflection.renderLambdaToString(SourceFile:80)
at kotlin.jvm.internal.Lambda.toString(SourceFile:22)
at java.lang.String.valueOf(String.java:2683)
at java.lang.StringBuilder.append(StringBuilder.java:129)

最佳答案

它看起来像是 Kotlin lambda 中的一个错误。

此代码足以重现异常:

 ({i: Int -> true}).toString()

我建议您在 youtrack.jetbrains.com/issues/KT 上发布问题,看看团队对此有何评论。

关于exception - Kotlin/Android – 带有 lambda 的数据类中的 KotlinReflectionInternalError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48021156/

相关文章:

.net - TypeOf子类的设计模式

c# - Visual Studio 异常窗口中的双换行符 (\r\n)

java - 使用反射打印列表

c# - 查找不[可序列化]的项目

amazon-web-services - 是否可以在与 cloudformation 帐户不同的帐户中调用 lambda?

c# - Lambda 和类型推断

c# - C# 函数是否有标准的 "never returns"属性?

exception - 在 Nestjs 服务中抛出的正确异常类型是什么?

java - 我在本学期的最后一个实验中无法正确读取文件,并且在读取实际文件时不断遇到问题

python-2.7 - 获取 Spark 中 RDD 中每个键的前 3 个值