diff --git a/build.gradle.kts b/build.gradle.kts index bb20375bc..17e6e1fe9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -66,6 +66,9 @@ allprojects { } subprojects { + if (this@subprojects.name == "java-test") { + return@subprojects + } afterEvaluate { apply(plugin = "com.github.johnrengelman.shadow") val kotlin = diff --git a/java-test/build.gradle.kts b/java-test/build.gradle.kts new file mode 100644 index 000000000..6ec4dfaa9 --- /dev/null +++ b/java-test/build.gradle.kts @@ -0,0 +1,40 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + + +plugins { + java +} + + +dependencies { + + implementation(project(":mirai-core")) + + implementation(project(":mirai-serialization")) + + testImplementation(group = "junit", name = "junit", version = "4.12") + + implementation(kotlin("stdlib", null)) + implementation(kotlin("serialization", null)) + implementation(kotlin("reflect", null)) + + + implementation(kotlinx("serialization-runtime-common", Versions.Kotlin.serialization)) + implementation(kotlinx("serialization-protobuf-common", Versions.Kotlin.serialization)) + implementation(kotlinx("io", Versions.Kotlin.io)) + implementation(kotlinx("coroutines-io", Versions.Kotlin.coroutinesIo)) + implementation(kotlinx("coroutines-core-common", Versions.Kotlin.coroutines)) + + implementation("org.jetbrains.kotlinx:atomicfu-common:${Versions.Kotlin.atomicFU}") + + implementation(ktor("client-cio", Versions.Kotlin.ktor)) + implementation(ktor("client-core", Versions.Kotlin.ktor)) + implementation(ktor("network", Versions.Kotlin.ktor)) + +} diff --git a/java-test/src/test/java/net/mamoe/mirai/javatest/SimpleListenerHostTest.java b/java-test/src/test/java/net/mamoe/mirai/javatest/SimpleListenerHostTest.java new file mode 100644 index 000000000..80efb08ae --- /dev/null +++ b/java-test/src/test/java/net/mamoe/mirai/javatest/SimpleListenerHostTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.javatest; + +import kotlin.coroutines.CoroutineContext; +import kotlin.coroutines.EmptyCoroutineContext; +import kotlinx.coroutines.CoroutineScope; +import kotlinx.coroutines.CoroutineScopeKt; +import net.mamoe.mirai.event.*; +import org.jetbrains.annotations.NotNull; +import org.junit.Test; + +public class SimpleListenerHostTest { + @Test + public void test() { + final SimpleListenerHost host = new SimpleListenerHost() { + @EventHandler + public void testListen( + AbstractEvent event + ) { + System.out.println(event); + } + + @Override + public void handleException(@NotNull CoroutineContext context, @NotNull Throwable exception) { + exception.printStackTrace(); + } + }; + CoroutineScope scope = CoroutineScopeKt.CoroutineScope(EmptyCoroutineContext.INSTANCE); + Events.registerEvents(scope, host); + EventKt.broadcast(new AbstractEvent() { + }); + } +} diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/JvmMethodListeners.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/JvmMethodListeners.kt index d5f715f6f..403710d35 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/JvmMethodListeners.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/JvmMethodListeners.kt @@ -331,9 +331,11 @@ private fun Method.registerEvent( } } else { // java methods - + check(this.parameterCount == 1) { + "Illegal method parameter. Only one parameter is required." + } val paramType = this.parameters[0].type - check(this.parameterCount == 1 && Event::class.java.isAssignableFrom(paramType)) { + check(Event::class.java.isAssignableFrom(paramType)) { "Illegal method parameter. Required one exact Event subclass. found $paramType" } when (this.returnType) { @@ -347,11 +349,11 @@ private fun Method.registerEvent( if (annotation.ignoreCancelled) { if ((this as? CancellableEvent)?.isCancelled != true) { withContext(Dispatchers.IO) { - this@registerEvent.invoke(owner, this) + this@registerEvent.invoke(owner, this@subscribeAlways) } } } else withContext(Dispatchers.IO) { - this@registerEvent.invoke(owner, this) + this@registerEvent.invoke(owner, this@subscribeAlways) } } } @@ -365,11 +367,11 @@ private fun Method.registerEvent( if (annotation.ignoreCancelled) { if ((this as? CancellableEvent)?.isCancelled != true) { withContext(Dispatchers.IO) { - this@registerEvent.invoke(owner, this) as ListeningStatus + this@registerEvent.invoke(owner, this@subscribe) as ListeningStatus } } else ListeningStatus.LISTENING } else withContext(Dispatchers.IO) { - this@registerEvent.invoke(owner, this) as ListeningStatus + this@registerEvent.invoke(owner, this@subscribe) as ListeningStatus } } diff --git a/settings.gradle b/settings.gradle index ea22bf2c5..79f3e977b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -24,6 +24,7 @@ rootProject.name = 'mirai' include(':mirai-core') include(':mirai-core-qqandroid') include(':mirai-serialization') +include(':java-test') //include(':compatibility-validator') // THIS WILL CAUSE A DEPENDENCY RESOLUTION BUG //include(':java-compatibility-validator')