协程的类强制转换异常

时间:2018-09-13 13:55:21

标签: kotlin intellij-plugin kotlinx.coroutines

我正在为Intellij Idea编写插件,并且正在使用kotlin。 我想使用协程,但出现异常 让我们考虑一个对象(该对象已简化,但仍会发生错误):

object UpdateTaskHolder {
    fun process() {
        runBlocking {
            print(listOf(1, 2, 3, 4, 5, 6))
        }
    }
}

在测试中调用它时,一切正常。结果:

[1, 2, 3, 4, 5, 6]

但是当我在 intellij gradle

的gradle任务 runIde 中调用它时

插件,我收到

java.lang.ClassCastException: kotlinx.coroutines.experimental.BlockingCoroutine cannot be cast to kotlin.coroutines.experimental.Continuation
    at UpdateTaskHolder$process$1.invoke(UpdateTaskHolder.kt:7)
    at kotlin.coroutines.experimental.intrinsics.IntrinsicsKt__IntrinsicsJvmKt$createCoroutineUnchecked$$inlined$buildContinuationByInvokeCall$IntrinsicsKt__IntrinsicsJvmKt$2.resume(IntrinsicsJvm.kt:122)
    at kotlin.coroutines.experimental.intrinsics.IntrinsicsKt__IntrinsicsJvmKt$createCoroutineUnchecked$$inlined$buildContinuationByInvokeCall$IntrinsicsKt__IntrinsicsJvmKt$2.resume(IntrinsicsJvm.kt:97)
    at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:161)
    at kotlinx.coroutines.experimental.DispatchedContinuation.run(Dispatched.kt:25)
    at kotlinx.coroutines.experimental.EventLoopBase.processNextEvent(EventLoop.kt:147)
    at kotlinx.coroutines.experimental.BlockingCoroutine.joinBlocking(Builders.kt:244)
    at kotlinx.coroutines.experimental.BuildersKt.runBlocking(Builders.kt:185)
    at kotlinx.coroutines.experimental.BuildersKt.runBlocking$default(Builders.kt:175)
    at UpdateTaskHolder.process(UpdateTaskHolder.kt:9)
    at MainWindow$createLoginPanel$2.actionPerformed(MainWindow.kt:58)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6548)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3325)
    at java.awt.Component.processEvent(Component.java:6313)
    at java.awt.Container.processEvent(Container.java:2237)
    at java.awt.Component.dispatchEventImpl(Component.java:4903)
    at java.awt.Container.dispatchEventImpl(Container.java:2295)
    at java.awt.Component.dispatchEvent(Component.java:4725)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)
    at java.awt.Container.dispatchEventImpl(Container.java:2281)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4725)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:764)
    at java.awt.EventQueue.access$500(EventQueue.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:715)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:737)
    at java.awt.EventQueue$4.run(EventQueue.java:735)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:734)
    at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:781)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:718)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:382)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我的gradle脚本:

plugins {
    id 'org.jetbrains.intellij' version '0.3.5'
    id 'org.jetbrains.kotlin.jvm' version '1.2.51'
}

group 'no.name'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    compile 'com.palominolabs.http:url-builder:1.1.1'
    compile "com.fasterxml.jackson.core:jackson-databind:2.6.5"
    compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.6.5"
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
intellij {
    version '2018.2'
    plugins 'git4idea'
}

kotlin {
    experimental {
        coroutines "enable"
    }
}

您能解释一下我在做什么错吗?

0 个答案:

没有答案