diff --git a/README.md b/README.md index f6fa60c..c6e2888 100644 --- a/README.md +++ b/README.md @@ -92,13 +92,13 @@ In some cases an event handler is not interested in the dispatched event instanc To avoid declaring a useless parameter just to specify the event type of the handler, there is an alternative: ```java -@Event(eventType = SimpleEvent.class) +@Event(SimpleEvent.class) private void onSimpleEvent() { System.out.println("SimpleEvent received!"); } ``` -Make sure that you **do not** declare both a parameter and the `eventType` value of the annotation, as this would be ambiguous. +Make sure that you **do not** both declare a parameter and specify the event type in the annotation, as this would be ambiguous. ## Event Consumption @@ -106,13 +106,13 @@ In some cases it might be useful to stop the propagation of an event. Event Bus makes this possible with event consumption: ```java -@Event(eventType = SimpleEvent.class) +@Event(SimpleEvent.class) @Priority(100) private void onSimpleEvent() { EventBus.getInstance().cancel(); } -@Event(eventType = SimpleEvent.class) +@Event(SimpleEvent.class) @Priority(50) private void onSimpleEvent2() { System.out.println("Will not be printed!"); @@ -154,6 +154,12 @@ Then, require the Event Bus Core module in your `module-info.java`: requires dev.kske.eventbus.core; ``` +If you intend to use event handlers that are inaccessible to Event Bus by means of Java language access control, make sure to allow reflective access from your module: + +```java +opens my.module to dev.kske.eventbus.core; +``` + ## Compile-Time Error Checking with Event Bus AP To assist you with writing event listeners, the Event Bus AP (Annotation Processor) module enforces correct usage of the `@Event` annotation during compile time. diff --git a/event-bus-ap/src/main/java/dev/kske/eventbus/ap/EventProcessor.java b/event-bus-ap/src/main/java/dev/kske/eventbus/ap/EventProcessor.java index f38b997..c05f15f 100644 --- a/event-bus-ap/src/main/java/dev/kske/eventbus/ap/EventProcessor.java +++ b/event-bus-ap/src/main/java/dev/kske/eventbus/ap/EventProcessor.java @@ -40,7 +40,7 @@ public class EventProcessor extends AbstractProcessor { // Determine how the event type is defined boolean useParameter; try { - eventAnnotation.eventType(); + eventAnnotation.value(); throw new EventBusException( "Could not determine event type of handler " + eventHandler); } catch (MirroredTypeException e) { diff --git a/event-bus-core/src/main/java/dev/kske/eventbus/core/Event.java b/event-bus-core/src/main/java/dev/kske/eventbus/core/Event.java index 34be499..7a6b837 100644 --- a/event-bus-core/src/main/java/dev/kske/eventbus/core/Event.java +++ b/event-bus-core/src/main/java/dev/kske/eventbus/core/Event.java @@ -13,7 +13,7 @@ import java.lang.annotation.*; *
  • Specifying an event type by either * *
  • *
  • Return type of {@code void}
  • @@ -36,9 +36,9 @@ public @interface Event { * This is useful when the event handler does not utilize the event instance. * * @return the event type accepted by the handler - * @since 0.0.3 + * @since 1.0.0 */ - Class eventType() default USE_PARAMETER.class; + Class value() default USE_PARAMETER.class; /** * Signifies that the event type the handler listens to is determined by the type of its only diff --git a/event-bus-core/src/main/java/dev/kske/eventbus/core/EventHandler.java b/event-bus-core/src/main/java/dev/kske/eventbus/core/EventHandler.java index d36549f..16b4561 100644 --- a/event-bus-core/src/main/java/dev/kske/eventbus/core/EventHandler.java +++ b/event-bus-core/src/main/java/dev/kske/eventbus/core/EventHandler.java @@ -23,8 +23,8 @@ final class EventHandler implements Comparable { private final EventListener listener; private final Method method; - private final Event annotation; private final Class eventType; + private final boolean useParameter; private final boolean polymorphic; private final int priority; @@ -42,13 +42,13 @@ final class EventHandler implements Comparable { EventHandler(EventListener listener, Method method, Event annotation) throws EventBusException { this.listener = listener; this.method = method; - this.annotation = annotation; + useParameter = annotation.value() == USE_PARAMETER.class; // Check for correct method signature and return type - if (method.getParameterCount() == 0 && annotation.eventType().equals(USE_PARAMETER.class)) + if (method.getParameterCount() == 0 && useParameter) throw new EventBusException(method + " does not define an event type!"); - if (method.getParameterCount() == 1 && !annotation.eventType().equals(USE_PARAMETER.class)) + if (method.getParameterCount() == 1 && !useParameter) throw new EventBusException(method + " defines an ambiguous event type!"); if (method.getParameterCount() > 1) @@ -58,16 +58,18 @@ final class EventHandler implements Comparable { throw new EventBusException(method + " does not have a return type of void!"); // Determine the event type - Class eventType = annotation.eventType(); - if (eventType.equals(USE_PARAMETER.class)) { + if (useParameter) { var param = method.getParameterTypes()[0]; if (!IEvent.class.isAssignableFrom(param)) throw new EventBusException(param + " is not of type IEvent!"); eventType = (Class) param; + } else { + eventType = annotation.value(); } - this.eventType = eventType; - polymorphic = method.isAnnotationPresent(Polymorphic.class); - priority = method.isAnnotationPresent(Priority.class) + + // Determine additional handler properties + polymorphic = method.isAnnotationPresent(Polymorphic.class); + priority = method.isAnnotationPresent(Priority.class) ? method.getAnnotation(Priority.class).value() : DEFAULT_PRIORITY; @@ -93,8 +95,9 @@ final class EventHandler implements Comparable { @Override public String toString() { - return String.format("EventHandler[method=%s, eventType=%s, polymorphic=%b, priority=%d]", - method, annotation.eventType(), polymorphic, priority); + return String.format( + "EventHandler[method=%s, eventType=%s, useParameter=%b, polymorphic=%b, priority=%d]", + method, eventType, useParameter, polymorphic, priority); } /** @@ -106,7 +109,7 @@ final class EventHandler implements Comparable { */ void execute(IEvent event) throws EventBusException { try { - if (annotation.eventType() == USE_PARAMETER.class) + if (useParameter) method.invoke(listener, event); else method.invoke(listener); @@ -122,7 +125,7 @@ final class EventHandler implements Comparable { EventListener getListener() { return listener; } /** - * @return the event type this handler listens to + * @return the event type this handler listens for * @since 0.0.3 */ Class getEventType() { return eventType; } diff --git a/event-bus-core/src/test/java/dev/kske/eventbus/core/CancelTest.java b/event-bus-core/src/test/java/dev/kske/eventbus/core/CancelTest.java index a2da0bf..cde0128 100644 --- a/event-bus-core/src/test/java/dev/kske/eventbus/core/CancelTest.java +++ b/event-bus-core/src/test/java/dev/kske/eventbus/core/CancelTest.java @@ -39,14 +39,14 @@ class CancelTest implements EventListener { assertEquals(1, hits); } - @Event(eventType = SimpleEvent.class) + @Event(SimpleEvent.class) @Priority(100) void onSimpleFirst() { ++hits; bus.cancel(); } - @Event(eventType = SimpleEvent.class) + @Event(SimpleEvent.class) @Priority(50) void onSimpleSecond() { ++hits; diff --git a/event-bus-core/src/test/java/dev/kske/eventbus/core/DispatchTest.java b/event-bus-core/src/test/java/dev/kske/eventbus/core/DispatchTest.java index c3a5a04..3fb1a3b 100644 --- a/event-bus-core/src/test/java/dev/kske/eventbus/core/DispatchTest.java +++ b/event-bus-core/src/test/java/dev/kske/eventbus/core/DispatchTest.java @@ -38,7 +38,7 @@ class DispatchTest implements EventListener { bus.dispatch(new SimpleEvent()); } - @Event(eventType = SimpleEvent.class) + @Event(SimpleEvent.class) @Priority(200) @Polymorphic void onSimpleEventFirst() { @@ -46,7 +46,7 @@ class DispatchTest implements EventListener { assertTrue(hits == 1 || hits == 2); } - @Event(eventType = SimpleEvent.class) + @Event(SimpleEvent.class) @Priority(150) static void onSimpleEventSecond() { ++hits;