Fix ListenerHost: inappropriate receiver for invoke for Java methods. (#499)

* Fix the abnormal behavior of ListenerHost.

* Rename test unit name
This commit is contained in:
Karlatemp 2020-08-17 21:18:37 +08:00 committed by GitHub
parent f8eed41d62
commit 2161b67775
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 12 deletions

View File

@ -8,33 +8,33 @@
package net.mamoe.mirai.javatest; package net.mamoe.mirai.javatest;
import kotlin.coroutines.CoroutineContext;
import kotlin.coroutines.EmptyCoroutineContext; import kotlin.coroutines.EmptyCoroutineContext;
import kotlinx.coroutines.CoroutineScope; import kotlinx.coroutines.CoroutineScope;
import kotlinx.coroutines.CoroutineScopeKt; import kotlinx.coroutines.CoroutineScopeKt;
import net.mamoe.mirai.event.*; import net.mamoe.mirai.event.*;
import org.jetbrains.annotations.NotNull;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.atomic.AtomicBoolean;
public class SimpleListenerHostTest { public class SimpleListenerHostTest {
@Test @Test
public void test() { public void testJavaSimpleListenerHostWork() {
AtomicBoolean called = new AtomicBoolean();
final SimpleListenerHost host = new SimpleListenerHost() { final SimpleListenerHost host = new SimpleListenerHost() {
@EventHandler @EventHandler
public void testListen( public void testListen(
AbstractEvent event AbstractEvent event
) { ) {
System.out.println(event); System.out.println(event);
} called.set(true);
@Override
public void handleException(@NotNull CoroutineContext context, @NotNull Throwable exception) {
exception.printStackTrace();
} }
}; };
CoroutineScope scope = CoroutineScopeKt.CoroutineScope(EmptyCoroutineContext.INSTANCE); CoroutineScope scope = CoroutineScopeKt.CoroutineScope(EmptyCoroutineContext.INSTANCE);
Events.registerEvents(scope, host); Events.registerEvents(scope, host);
EventKt.broadcast(new AbstractEvent() { EventKt.broadcast(new AbstractEvent() {
}); });
if (!called.get()) {
throw new AssertionError("JavaTest: SimpleListenerHost Failed.");
}
} }
} }

View File

@ -161,11 +161,11 @@ internal fun Method.registerEvent(
if (annotation.ignoreCancelled) { if (annotation.ignoreCancelled) {
if ((this as? CancellableEvent)?.isCancelled != true) { if ((this as? CancellableEvent)?.isCancelled != true) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
this@registerEvent.invoke(owner, this) this@registerEvent.invoke(owner, this@subscribeAlways)
} }
} }
} else withContext(Dispatchers.IO) { } else withContext(Dispatchers.IO) {
this@registerEvent.invoke(owner, this) this@registerEvent.invoke(owner, this@subscribeAlways)
} }
} }
} }
@ -179,11 +179,11 @@ internal fun Method.registerEvent(
if (annotation.ignoreCancelled) { if (annotation.ignoreCancelled) {
if ((this as? CancellableEvent)?.isCancelled != true) { if ((this as? CancellableEvent)?.isCancelled != true) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
this@registerEvent.invoke(owner, this) as ListeningStatus this@registerEvent.invoke(owner, this@subscribe) as ListeningStatus
} }
} else ListeningStatus.LISTENING } else ListeningStatus.LISTENING
} else withContext(Dispatchers.IO) { } else withContext(Dispatchers.IO) {
this@registerEvent.invoke(owner, this) as ListeningStatus this@registerEvent.invoke(owner, this@subscribe) as ListeningStatus
} }
} }