From 5f65fb754e2f35cb7001c58c48af17b62d51fbd4 Mon Sep 17 00:00:00 2001 From: Lex Manos Date: Mon, 8 Sep 2014 17:54:41 -0700 Subject: [PATCH] Add IEventExceptionHandler for EventBus to allow special handeling exceptions that are fired while running an event. Events now track what 'phase' they are in during the execution process. Each EventPriority is a 'phase'. An exception is thrown if the event attempts to set its phase to a previous one. --- .../common/eventhandler/ASMEventHandler.java | 8 +++- .../mods/fml/common/eventhandler/Event.java | 20 +++++++++ .../fml/common/eventhandler/EventBus.java | 44 ++++++++++++++++--- .../common/eventhandler/EventPriority.java | 13 ++++-- .../eventhandler/IEventExceptionHandler.java | 16 +++++++ .../fml/common/eventhandler/ListenerList.java | 7 ++- 6 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 fml/src/main/java/cpw/mods/fml/common/eventhandler/IEventExceptionHandler.java diff --git a/fml/src/main/java/cpw/mods/fml/common/eventhandler/ASMEventHandler.java b/fml/src/main/java/cpw/mods/fml/common/eventhandler/ASMEventHandler.java index d8584f3441..7eeb64419e 100644 --- a/fml/src/main/java/cpw/mods/fml/common/eventhandler/ASMEventHandler.java +++ b/fml/src/main/java/cpw/mods/fml/common/eventhandler/ASMEventHandler.java @@ -5,7 +5,6 @@ import static org.objectweb.asm.Opcodes.*; import java.lang.reflect.Method; import java.util.HashMap; - import org.apache.logging.log4j.ThreadContext; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; @@ -27,11 +26,14 @@ public class ASMEventHandler implements IEventListener private final IEventListener handler; private final SubscribeEvent subInfo; private ModContainer owner; + private String readable; + public ASMEventHandler(Object target, Method method, ModContainer owner) throws Exception { this.owner = owner; handler = (IEventListener)createWrapper(method).getConstructor(Object.class).newInstance(target); subInfo = method.getAnnotation(SubscribeEvent.class); + readable = "ASM: " + target + " " + method.getName() + Type.getMethodDescriptor(method); } @Override @@ -142,4 +144,8 @@ public class ASMEventHandler implements IEventListener } } + public String toString() + { + return readable; + } } diff --git a/fml/src/main/java/cpw/mods/fml/common/eventhandler/Event.java b/fml/src/main/java/cpw/mods/fml/common/eventhandler/Event.java index d9c31ee7a9..80ac792406 100644 --- a/fml/src/main/java/cpw/mods/fml/common/eventhandler/Event.java +++ b/fml/src/main/java/cpw/mods/fml/common/eventhandler/Event.java @@ -9,6 +9,11 @@ import java.lang.annotation.Target; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import com.google.common.base.Preconditions; + /** * Base Event class that all other events are derived from @@ -31,6 +36,7 @@ public class Event private Result result = Result.DEFAULT; private final boolean hasResult; private static ListenerList listeners = new ListenerList(); + private EventPriority phase = null; private static final Map, Map, Boolean>> annotationMap = new ConcurrentHashMap, Map, Boolean>>(); @@ -153,4 +159,18 @@ public class Event { return listeners; } + + @Nullable + public EventPriority getPhase() + { + return this.phase; + } + + public void setPhase(@Nonnull EventPriority value) + { + Preconditions.checkArgument(value != null, "setPhase argument must not be null"); + int prev = phase == null ? -1 : phase.ordinal(); + Preconditions.checkArgument(prev < value.ordinal(), "Attempted to set event phase to %s when already %s", value, phase); + phase = value; + } } diff --git a/fml/src/main/java/cpw/mods/fml/common/eventhandler/EventBus.java b/fml/src/main/java/cpw/mods/fml/common/eventhandler/EventBus.java index 7d6e3078db..100ce9eff2 100644 --- a/fml/src/main/java/cpw/mods/fml/common/eventhandler/EventBus.java +++ b/fml/src/main/java/cpw/mods/fml/common/eventhandler/EventBus.java @@ -6,27 +6,41 @@ import java.util.ArrayList; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; + +import javax.annotation.Nonnull; + import org.apache.logging.log4j.Level; - - +import com.google.common.base.Preconditions; +import com.google.common.base.Throwables; import com.google.common.collect.MapMaker; import com.google.common.reflect.TypeToken; + import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.ModContainer; -public class EventBus +public class EventBus implements IEventExceptionHandler { private static int maxID = 0; private ConcurrentHashMap> listeners = new ConcurrentHashMap>(); private Map listenerOwners = new MapMaker().weakKeys().weakValues().makeMap(); private final int busID = maxID++; + private IEventExceptionHandler exceptionHandler; public EventBus() { ListenerList.resize(busID + 1); + exceptionHandler = this; + register(this); + } + + public EventBus(@Nonnull IEventExceptionHandler handler) + { + this(); + Preconditions.checkArgument(handler != null, "EventBus exception handler can not be null"); + exceptionHandler = handler; } public void register(Object target) @@ -117,10 +131,30 @@ public class EventBus public boolean post(Event event) { IEventListener[] listeners = event.getListenerList().getListeners(busID); - for (IEventListener listener : listeners) + int index = 0; + try { - listener.invoke(event); + for (; index < listeners.length; index++) + { + listeners[index].invoke(event); + } + } + catch (Throwable throwable) + { + exceptionHandler.handleException(this, event, listeners, index, throwable); + Throwables.propagate(throwable); } return (event.isCancelable() ? event.isCanceled() : false); } + + @Override + public void handleException(EventBus bus, Event event, IEventListener[] listeners, int index, Throwable throwable) + { + FMLLog.log(Level.ERROR, throwable, "Exception caught during firing event %s:", event); + FMLLog.log(Level.ERROR, "Index: %d Listeners:", index); + for (int x = 0; x < listeners.length; x++) + { + FMLLog.log(Level.ERROR, "%d: %s", x, listeners[x]); + } + } } diff --git a/fml/src/main/java/cpw/mods/fml/common/eventhandler/EventPriority.java b/fml/src/main/java/cpw/mods/fml/common/eventhandler/EventPriority.java index 6594db8eab..e9b54f61e5 100644 --- a/fml/src/main/java/cpw/mods/fml/common/eventhandler/EventPriority.java +++ b/fml/src/main/java/cpw/mods/fml/common/eventhandler/EventPriority.java @@ -1,16 +1,23 @@ package cpw.mods.fml.common.eventhandler; -public enum EventPriority +public enum EventPriority implements IEventListener { /*Priority of event listeners, listeners will be sorted with respect to this priority level. - * + * * Note: * Due to using a ArrayList in the ListenerList, - * these need to stay in a contiguous index starting at 0. {Default ordinal} + * these need to stay in a contiguous index starting at 0. {Default ordinal} */ HIGHEST, //First to execute HIGH, NORMAL, LOW, LOWEST //Last to execute +; + + @Override + public void invoke(Event event) + { + event.setPhase(this); + } } diff --git a/fml/src/main/java/cpw/mods/fml/common/eventhandler/IEventExceptionHandler.java b/fml/src/main/java/cpw/mods/fml/common/eventhandler/IEventExceptionHandler.java new file mode 100644 index 0000000000..7ad0929031 --- /dev/null +++ b/fml/src/main/java/cpw/mods/fml/common/eventhandler/IEventExceptionHandler.java @@ -0,0 +1,16 @@ +package cpw.mods.fml.common.eventhandler; + +public interface IEventExceptionHandler +{ + /** + * Fired when a EventListener throws an exception for the specified event on the event bus. + * After this function returns, the original Throwable will be propogated upwards. + * + * @param bus The bus the event is being fired on + * @param event The event that is being fired + * @param listeners All listeners that are listening for this event, in order + * @param index Index for the current listener being fired. + * @param throwable The throwable being thrown + */ + void handleException(EventBus bus, Event event, IEventListener[] listeners, int index, Throwable throwable); +} diff --git a/fml/src/main/java/cpw/mods/fml/common/eventhandler/ListenerList.java b/fml/src/main/java/cpw/mods/fml/common/eventhandler/ListenerList.java index 554a320d56..4aede806f8 100644 --- a/fml/src/main/java/cpw/mods/fml/common/eventhandler/ListenerList.java +++ b/fml/src/main/java/cpw/mods/fml/common/eventhandler/ListenerList.java @@ -202,7 +202,12 @@ public class ListenerList ArrayList ret = new ArrayList(); for (EventPriority value : EventPriority.values()) { - ret.addAll(getListeners(value)); + List listeners = getListeners(value); + if (listeners.size() > 0) + { + ret.add(value); //Add the priority to notify the event of it's current phase. + ret.addAll(listeners); + } } listeners = ret.toArray(new IEventListener[ret.size()]); rebuild = false;