From 5823f47467cc1ad981adbcec30c3db0200ee2409 Mon Sep 17 00:00:00 2001 From: md_5 Date: Wed, 19 May 2021 18:41:07 +1000 Subject: [PATCH] #3090: Register events in parent classes --- .../java/net/md_5/bungee/event/EventBus.java | 4 ++- .../net/md_5/bungee/event/SubclassTest.java | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 event/src/test/java/net/md_5/bungee/event/SubclassTest.java diff --git a/event/src/main/java/net/md_5/bungee/event/EventBus.java b/event/src/main/java/net/md_5/bungee/event/EventBus.java index f5bd1f98..8da5f275 100644 --- a/event/src/main/java/net/md_5/bungee/event/EventBus.java +++ b/event/src/main/java/net/md_5/bungee/event/EventBus.java @@ -1,5 +1,6 @@ package net.md_5.bungee.event; +import com.google.common.collect.ImmutableSet; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.MessageFormat; @@ -61,7 +62,8 @@ public class EventBus private Map, Map>> findHandlers(Object listener) { Map, Map>> handler = new HashMap<>(); - for ( Method m : listener.getClass().getDeclaredMethods() ) + Set methods = ImmutableSet.builder().add( listener.getClass().getMethods() ).add( listener.getClass().getDeclaredMethods() ).build(); + for ( final Method m : methods ) { EventHandler annotation = m.getAnnotation( EventHandler.class ); if ( annotation != null ) diff --git a/event/src/test/java/net/md_5/bungee/event/SubclassTest.java b/event/src/test/java/net/md_5/bungee/event/SubclassTest.java new file mode 100644 index 00000000..be4b3804 --- /dev/null +++ b/event/src/test/java/net/md_5/bungee/event/SubclassTest.java @@ -0,0 +1,26 @@ +package net.md_5.bungee.event; + +import java.util.concurrent.CountDownLatch; +import org.junit.Assert; +import org.junit.Test; + +public class SubclassTest extends EventBusTest +{ + + private final CountDownLatch latch = new CountDownLatch( 1 ); + + @Test + @Override + public void testNestedEvents() + { + super.testNestedEvents(); + Assert.assertEquals( 0, latch.getCount() ); + } + + @EventHandler + protected void extraListener(FirstEvent event) + { + Assert.assertEquals( 1, latch.getCount() ); + latch.countDown(); + } +}