Skip to content

Commit 22550ca

Browse files
authored
Create java test. Fix mamoe#443 (mamoe#446)
* Create java test. Fix mamoe#443 * Typo * gradle-kotlin-dsl
1 parent b71fc74 commit 22550ca

File tree

5 files changed

+92
-6
lines changed

5 files changed

+92
-6
lines changed

build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ allprojects {
6666
}
6767

6868
subprojects {
69+
if (this@subprojects.name == "java-test") {
70+
return@subprojects
71+
}
6972
afterEvaluate {
7073
apply(plugin = "com.github.johnrengelman.shadow")
7174
val kotlin =

java-test/build.gradle.kts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2020 Mamoe Technologies and contributors.
3+
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
4+
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
5+
*
6+
* https://github.com/mamoe/mirai/blob/master/LICENSE
7+
*/
8+
9+
10+
plugins {
11+
java
12+
}
13+
14+
15+
dependencies {
16+
17+
implementation(project(":mirai-core"))
18+
19+
implementation(project(":mirai-serialization"))
20+
21+
testImplementation(group = "junit", name = "junit", version = "4.12")
22+
23+
implementation(kotlin("stdlib", null))
24+
implementation(kotlin("serialization", null))
25+
implementation(kotlin("reflect", null))
26+
27+
28+
implementation(kotlinx("serialization-runtime-common", Versions.Kotlin.serialization))
29+
implementation(kotlinx("serialization-protobuf-common", Versions.Kotlin.serialization))
30+
implementation(kotlinx("io", Versions.Kotlin.io))
31+
implementation(kotlinx("coroutines-io", Versions.Kotlin.coroutinesIo))
32+
implementation(kotlinx("coroutines-core-common", Versions.Kotlin.coroutines))
33+
34+
implementation("org.jetbrains.kotlinx:atomicfu-common:${Versions.Kotlin.atomicFU}")
35+
36+
implementation(ktor("client-cio", Versions.Kotlin.ktor))
37+
implementation(ktor("client-core", Versions.Kotlin.ktor))
38+
implementation(ktor("network", Versions.Kotlin.ktor))
39+
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2020 Mamoe Technologies and contributors.
3+
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
4+
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
5+
*
6+
* https://github.com/mamoe/mirai/blob/master/LICENSE
7+
*/
8+
9+
package net.mamoe.mirai.javatest;
10+
11+
import kotlin.coroutines.CoroutineContext;
12+
import kotlin.coroutines.EmptyCoroutineContext;
13+
import kotlinx.coroutines.CoroutineScope;
14+
import kotlinx.coroutines.CoroutineScopeKt;
15+
import net.mamoe.mirai.event.*;
16+
import org.jetbrains.annotations.NotNull;
17+
import org.junit.Test;
18+
19+
public class SimpleListenerHostTest {
20+
@Test
21+
public void test() {
22+
final SimpleListenerHost host = new SimpleListenerHost() {
23+
@EventHandler
24+
public void testListen(
25+
AbstractEvent event
26+
) {
27+
System.out.println(event);
28+
}
29+
30+
@Override
31+
public void handleException(@NotNull CoroutineContext context, @NotNull Throwable exception) {
32+
exception.printStackTrace();
33+
}
34+
};
35+
CoroutineScope scope = CoroutineScopeKt.CoroutineScope(EmptyCoroutineContext.INSTANCE);
36+
Events.registerEvents(scope, host);
37+
EventKt.broadcast(new AbstractEvent() {
38+
});
39+
}
40+
}

mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/JvmMethodListeners.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,11 @@ private fun Method.registerEvent(
331331
}
332332
} else {
333333
// java methods
334-
334+
check(this.parameterCount == 1) {
335+
"Illegal method parameter. Only one parameter is required."
336+
}
335337
val paramType = this.parameters[0].type
336-
check(this.parameterCount == 1 && Event::class.java.isAssignableFrom(paramType)) {
338+
check(Event::class.java.isAssignableFrom(paramType)) {
337339
"Illegal method parameter. Required one exact Event subclass. found $paramType"
338340
}
339341
when (this.returnType) {
@@ -347,11 +349,11 @@ private fun Method.registerEvent(
347349
if (annotation.ignoreCancelled) {
348350
if ((this as? CancellableEvent)?.isCancelled != true) {
349351
withContext(Dispatchers.IO) {
350-
this@registerEvent.invoke(owner, this)
352+
this@registerEvent.invoke(owner, this@subscribeAlways)
351353
}
352354
}
353355
} else withContext(Dispatchers.IO) {
354-
this@registerEvent.invoke(owner, this)
356+
this@registerEvent.invoke(owner, this@subscribeAlways)
355357
}
356358
}
357359
}
@@ -365,11 +367,11 @@ private fun Method.registerEvent(
365367
if (annotation.ignoreCancelled) {
366368
if ((this as? CancellableEvent)?.isCancelled != true) {
367369
withContext(Dispatchers.IO) {
368-
this@registerEvent.invoke(owner, this) as ListeningStatus
370+
this@registerEvent.invoke(owner, this@subscribe) as ListeningStatus
369371
}
370372
} else ListeningStatus.LISTENING
371373
} else withContext(Dispatchers.IO) {
372-
this@registerEvent.invoke(owner, this) as ListeningStatus
374+
this@registerEvent.invoke(owner, this@subscribe) as ListeningStatus
373375
}
374376

375377
}

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ rootProject.name = 'mirai'
2424
include(':mirai-core')
2525
include(':mirai-core-qqandroid')
2626
include(':mirai-serialization')
27+
include(':java-test')
2728
//include(':compatibility-validator') // THIS WILL CAUSE A DEPENDENCY RESOLUTION BUG
2829
//include(':java-compatibility-validator')
2930

0 commit comments

Comments
 (0)