【问题标题】:Kotlin/Android – KotlinReflectionInternalError in Data Class with a lambdaKotlin/Android – 带有 lambda 的数据类中的 KotlinReflectionInternalError
【发布时间】:2017-12-29 10:29:58
【问题描述】:

kotlin.reflect.jvm.internal.KotlinReflectionInternalError: 内省局部函数、lambda、匿名函数和局部函数 Kotlin 反射尚不完全支持变量

此异常来自数据类的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)

【问题讨论】:

  • I can't reproduce it in my environment - 怎么会?
  • 通过崩溃报告。
  • 我相信当 android 设备低于 6.0 时会显示此错误。有同样的问题应用程序在设备 6.0 及更高版本上工作,但在 5.0 上没有。我相信这是 Kotlin 的错误

标签: exception reflection lambda kotlin kotlinx.coroutines


【解决方案1】:

这看起来像是 Kotlin lambdas 中的一个错误。

这段代码足以重现异常:

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

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

【讨论】:

  • 你的环境是什么?它适用于我的Kotlin version 1.1.51 (JRE 1.8.0_144-b01)
  • 是的。 System.getProperty("java.version") 返回 1.8.0_45。所以好像不是Java版本的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
相关资源
最近更新 更多