mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-06 13:54:40 +08:00
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:
parent
f8eed41d62
commit
2161b67775
@ -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.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user