From ffbebaff691f966ecbdc1349599fcca75c074382 Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 4 Jul 2013 09:43:32 +1000 Subject: [PATCH] Remove old @Subscribe event handling --- .../md_5/bungee/api/plugin/PluginManager.java | 13 ++--- .../java/net/md_5/bungee/event/EventBus.java | 55 ++++++------------- .../net/md_5/bungee/event/EventBusTest.java | 4 +- 3 files changed, 23 insertions(+), 49 deletions(-) diff --git a/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java b/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java index 1ccc3cb76..5a537cc15 100644 --- a/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java +++ b/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java @@ -47,7 +47,7 @@ public class PluginManager public PluginManager(ProxyServer proxy) { this.proxy = proxy; - eventBus = new EventBus( proxy.getLogger(), Subscribe.class, EventHandler.class ); + eventBus = new EventBus( proxy.getLogger() ); } /** @@ -314,14 +314,9 @@ public class PluginManager { for ( Method method : listener.getClass().getDeclaredMethods() ) { - if ( method.isAnnotationPresent( Subscribe.class ) ) - { - proxy.getLogger().log( Level.WARNING, "Listener " + listener + " has registered using depreceated subscribe annotation!" - + " Please advice author to update to @EventHandler." - + " As a server owner you may safely ignore this.", new Exception() ); - } + Preconditions.checkArgument( !method.isAnnotationPresent( Subscribe.class ), + "Listener %s has registered using deprecated subscribe annotation! Please update to @EventHandler.", listener ); + eventBus.register( listener ); } - - eventBus.register( listener ); } } 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 94e154ea5..0c9a8005b 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,6 +1,5 @@ package net.md_5.bungee.event; -import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.MessageFormat; @@ -19,32 +18,15 @@ public class EventBus private final Map, Map> eventToHandler = new HashMap<>(); private final ReadWriteLock lock = new ReentrantReadWriteLock(); private final Logger logger; - private final Class[] annotations; public EventBus() { - this( null, (Class[]) null ); + this( null ); } public EventBus(Logger logger) - { - this( logger, (Class[]) null ); - } - - @SuppressWarnings("unchecked") - public EventBus(Class... annotations) - { - this( null, annotations ); - } - - @SuppressWarnings("unchecked") - public EventBus(Logger logger, Class... annotations) { this.logger = ( logger == null ) ? Logger.getGlobal() : logger; - this.annotations = ( annotations == null || annotations.length == 0 ) ? new Class[] - { - EventHandler.class - } : annotations; } public void post(Object event) @@ -86,29 +68,26 @@ public class EventBus Map, Set> handler = new HashMap<>(); for ( Method m : listener.getClass().getDeclaredMethods() ) { - for ( Class annotation : annotations ) + EventHandler annotation = m.getAnnotation( EventHandler.class ); + if ( annotation != null ) { - if ( m.isAnnotationPresent( annotation ) ) + Class[] params = m.getParameterTypes(); + if ( params.length != 1 ) { - Class[] params = m.getParameterTypes(); - if ( params.length != 1 ) + logger.log( Level.INFO, "Method {0} in class {1} annotated with {2} does not have single argument", new Object[] { - logger.log( Level.INFO, "Method {0} in class {1} annotated with {2} does not have single argument", new Object[] - { - m, listener.getClass(), annotation - } ); - continue; - } - - Set existing = handler.get( params[0] ); - if ( existing == null ) - { - existing = new HashSet<>(); - handler.put( params[0], existing ); - } - existing.add( m ); - break; + m, listener.getClass(), annotation + } ); + continue; } + + Set existing = handler.get( params[0] ); + if ( existing == null ) + { + existing = new HashSet<>(); + handler.put( params[0], existing ); + } + existing.add( m ); } } return handler; diff --git a/event/src/test/java/net/md_5/bungee/event/EventBusTest.java b/event/src/test/java/net/md_5/bungee/event/EventBusTest.java index 4a6f09343..2c7376751 100644 --- a/event/src/test/java/net/md_5/bungee/event/EventBusTest.java +++ b/event/src/test/java/net/md_5/bungee/event/EventBusTest.java @@ -15,14 +15,14 @@ public class EventBusTest { bus.register( this ); bus.post( new FirstEvent() ); - Assert.assertEquals( latch.getCount(), 0 ); + Assert.assertEquals( 0, latch.getCount() ); } @EventHandler public void firstListener(FirstEvent event) { bus.post( new SecondEvent() ); - Assert.assertEquals( latch.getCount(), 1 ); + Assert.assertEquals( 1, latch.getCount() ); latch.countDown(); }