Event4J
Event4J copied to clipboard
Have clients extend EventBus to change marking behavior, instead of using the overcomplex 'EventMarker' interface
Instead of having EventBus delegate marking logic to a 'EventMarker' passed in at creation time, we can switch to having two simple methods in the EventBus class, which clearly and simply implement the default logic:
public boolean isEventListener(Method method) {
return method.isAnnotationPresent(EventListener.class);
}
public int getEventListenerPriority(Method method) {
return method.getAnnotation(EventListener.class).priority();
}
In order to let clients override this marking logic we'll have to make EventBus a non-final class, to let sub-classes override the marking methods. This makes for a simpler, more efficient, and more readable implementation, for both the default system and for custom behavior:
Compare the current system:
EventBus event4JBus = EventBus.builder()
.eventMarker((m) -> m.isAnnotationPresent(EventHandler.class) ? m.getAnnotation(EventHandler.class)::priority : null)
.build();
to the new system:
EventBus event4JBus = new EventBus() {
@Override
public boolean isEventListener(Method method) {
return m.isAnnotationPresent(EventHandler.class);
}
@Override
public int getEventListenerPriority(Method method) {
return method.getAnnotation(EventListener.class).priority();
}
}