【问题标题】:UnsupportedOperationException while building a Kotlin project in Idea在 Idea 中构建 Kotlin 项目时出现 UnsupportedOperationException
【发布时间】:2017-03-03 06:08:28
【问题描述】:

当我尝试构建我的 Kotlin 项目时,我在 Idea 中得到以下 error

Error:Kotlin: [Internal Error] org.jetbrains.kotlin.util.KotlinFrontEndException: Exception while analyzing expression at (60,19) in E:/altruix-is/src/main/kotlin/com/mycompany/myproduct/capsulecrm/CapsuleCrmSubsystem.kt:
client.execute(req)

[...]

Caused by: java.lang.UnsupportedOperationException: doSubstitute with no original should not be called for synthetic extension
    at org.jetbrains.kotlin.synthetic.SamAdapterFunctionsScope$MyFunctionDescriptor.doSubstitute(SamAdapterFunctionsScope.kt:165)
    at org.jetbrains.kotlin.descriptors.impl.FunctionDescriptorImpl$CopyConfiguration.build(FunctionDescriptorImpl.java:553)
    at org.jetbrains.kotlin.load.java.ErasedOverridabilityCondition.isOverridable(ErasedOverridabilityCondition.kt:47)

错误似乎发生在调用中

res = client.execute(req)

client 是 Apache HttpClient。

可以在here找到发生这种情况的源文件。

我向 JetBrains 提交了错误报告,但我需要进一步处理该项目,因此需要解决。请注意,直到昨天一切正常。昨天我把 Kotlin 插件升级到最新版本,可能是这个问题。

如何避免上述错误?

更新 1(03.03.2017 14:46 MSK):

这不起作用:

open fun addNote(note: String, compId: Long): ValidationResult {
    val client = httpClient
    if (client == null) {
        return ValidationResult(false, "Internal error")
    }

    var res: CloseableHttpResponse? = null
    var req: HttpUriRequest?
    try {
        req = composeAddNoteRequest(note, compId)
        res = client.execute(req)
        if (res.statusLine.statusCode != 201) {
            logger.error("addNote(note='$note', compId=$compId): Wrong status code ${res.statusLine.statusCode}")
            return ValidationResult(false, "Wrong status code (CRM interaction)")
        }
        return ValidationResult(true, "")
    }
    catch (throwable: Throwable) {
        logger.error("addNote(note='$note', compId=$compId)", throwable)
        return ValidationResult(false, "Database error")
    } finally {
        close(res)
    }
    return ValidationResult(false, "Internal logic error")
}

这行得通(区别在从顶部开始的第二行):

open fun addNote(note: String, compId: Long): ValidationResult {
    val client = httpClient as CloseableHttpClient // Change
    if (client == null) {
        return ValidationResult(false, "Internal error")
    }

    var res: CloseableHttpResponse? = null
    var req: HttpUriRequest?
    try {
        req = composeAddNoteRequest(note, compId)
        res = client.execute(req)
        if (res.statusLine.statusCode != 201) {
            logger.error("addNote(note='$note', compId=$compId): Wrong status code ${res.statusLine.statusCode}")
            return ValidationResult(false, "Wrong status code (CRM interaction)")
        }
        return ValidationResult(true, "")
    }
    catch (throwable: Throwable) {
        logger.error("addNote(note='$note', compId=$compId)", throwable)
        return ValidationResult(false, "Database error")
    } finally {
        close(res)
    }
    return ValidationResult(false, "Internal logic error")
}

【问题讨论】:

  • 这就是为什么它是评论

标签: java intellij-idea kotlin


【解决方案1】:

在上面的示例中,client.execute(req) 返回HttpResponse,它不是CloseableHttpResponse 的子类型。所以,类型不匹配错误是正确的。您应该在这里使用CloseableHttpClient,或者将client.execute(req) 转换为CloseableHttpResponse

我无法从您的示例中复制 KotlinFrontEndException。从提供的堆栈跟踪中,我可以推断出“SAM 适配器”发生了问题——也就是说,当您在接受单个抽象方法 (SAM) 接口的 Java 方法调用中使用 Kotlin lambda 时。 如果问题仍然存在,请在此处提交错误:http://kotl.in/issue

【讨论】:

    猜你喜欢
    • 2018-05-27
    • 1970-01-01
    • 2017-07-17
    • 2019-08-07
    • 2014-07-12
    • 2019-11-29
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    相关资源
    最近更新 更多