Kaynağa Gözat

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

* Fix the abnormal behavior of ListenerHost.

* Rename test unit name
Karlatemp 5 yıl önce
ebeveyn
işleme
2161b67775

+ 8 - 8
java-test/src/test/java/net/mamoe/mirai/javatest/SimpleListenerHostTest.java

@@ -8,33 +8,33 @@
 
 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;
 
+import java.util.concurrent.atomic.AtomicBoolean;
+
 public class SimpleListenerHostTest {
     @Test
-    public void test() {
+    public void testJavaSimpleListenerHostWork() {
+        AtomicBoolean called = new AtomicBoolean();
         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();
+                called.set(true);
             }
         };
         CoroutineScope scope = CoroutineScopeKt.CoroutineScope(EmptyCoroutineContext.INSTANCE);
         Events.registerEvents(scope, host);
         EventKt.broadcast(new AbstractEvent() {
         });
+        if (!called.get()) {
+            throw new AssertionError("JavaTest: SimpleListenerHost Failed.");
+        }
     }
 }

+ 4 - 4
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/internal/EventInternalJvm.kt

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