gpt4 book ai didi

kotlin - 从 Kotlin 创建的 Java SAM 接口(interface)给出 ClassCastException

转载 作者:行者123 更新时间:2023-12-02 13:39:18 24 4
gpt4 key购买 nike

我有一个java方法:

addHandler(HttpServiceHandler handler)
HttpServiceHandler
interface HttpServiceHandler extends Consumer<HttpHandlerContext>

关键是要避免 Consumer<HttpHandlerContext>在整个项目中复制粘贴,所以它是一种类型别名。

在 Java 代码中,这可以正常工作:
addHandler({ context -> context.blah(); })

现在,在 Kotlin 中,我有这个生成处理程序的方法:
private companion object {
fun newHandler(notimportant: Long): HttpServiceHandler {
return HttpServiceHandler { context -> context.blah() }
}
}
HttpServiceHandler {}很重要,如果我不指定 HttpServiceHandler 则不会编译对于 lambda。

这编译:
addHandler(newHandler(1L))

但在运行时,抛出:
java.lang.ClassCastException: blah.BlahTest$Companion$newHandler$1 cannot be cast to kotlin.jvm.functions.Function1

at blah.BlahTest.test(BlahTest.kt:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

我不知道为什么。请帮忙?

更新:如果我这样写,类转换异常就消失了:
addHandler(
object : HttpServiceHandler {
override fun accept(c: HttpHandlerContext) {
c.complete()
}
}
)

但是这样写还是会抛出异常:
fun newHandler(blah: Long): HttpServiceHandler {
return object : HttpServiceHandler {
override fun accept(c: HttpHandlerContext) {
c.complete()
}
}
}

addHandler(newHandler(1L))

我不知道为什么。

更新 2:测试代码 https://github.com/wilem82/testcases/tree/master/kotlinsam1 .遗憾的是,无法重现该问题。

最佳答案

该错误已经准确地说:

java.lang.ClassCastException: blah.BlahTest$Companion$newHandler$1 cannot be cast to kotlin.jvm.functions.Function1



你尝试投一个 Consumer<HttpHandlerContext>Function1addHandler方法或某处,例如:
fun addHandler(handler: Consumer<HttpHandlerContext>) {
val it: Function1<*, *> = handler as Function1<*, *>
// ^
// ClassCastException was thrown since it is not a Function1
}

您应该使用 java 中的方法引用表达式/kotlin 中的函数引用表达式将 SAM 接口(interface)转换为另一个 SAM 接口(interface),例如:
fun addHandler(handler: Consumer<HttpHandlerContext>) {
val it: Function1<HttpHandlerContext, Unit> = handler::accept
// using function reference expression here ---^
//...
}

中频你调用java方法 addHandler在 kotlin 中,你不需要创建这样的桥接方法 newHandler ,你可以在 kotlin 中用 lambda 调用它,例如:
addHandler{context->
context.blah()
// additional works ...
}

关于kotlin - 从 Kotlin 创建的 Java SAM 接口(interface)给出 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44971228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com