Rename Event#eventType to Event#value for more concise usage

This commit is contained in:
Kai S. K. Engelbart 2021-02-15 13:42:20 +01:00
parent 9b1c708514
commit cd2e7ad023
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
6 changed files with 34 additions and 25 deletions

View File

@ -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: To avoid declaring a useless parameter just to specify the event type of the handler, there is an alternative:
```java ```java
@Event(eventType = SimpleEvent.class) @Event(SimpleEvent.class)
private void onSimpleEvent() { private void onSimpleEvent() {
System.out.println("SimpleEvent received!"); 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 ## 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: Event Bus makes this possible with event consumption:
```java ```java
@Event(eventType = SimpleEvent.class) @Event(SimpleEvent.class)
@Priority(100) @Priority(100)
private void onSimpleEvent() { private void onSimpleEvent() {
EventBus.getInstance().cancel(); EventBus.getInstance().cancel();
} }
@Event(eventType = SimpleEvent.class) @Event(SimpleEvent.class)
@Priority(50) @Priority(50)
private void onSimpleEvent2() { private void onSimpleEvent2() {
System.out.println("Will not be printed!"); 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; 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 ## 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. To assist you with writing event listeners, the Event Bus AP (Annotation Processor) module enforces correct usage of the `@Event` annotation during compile time.

View File

@ -40,7 +40,7 @@ public class EventProcessor extends AbstractProcessor {
// Determine how the event type is defined // Determine how the event type is defined
boolean useParameter; boolean useParameter;
try { try {
eventAnnotation.eventType(); eventAnnotation.value();
throw new EventBusException( throw new EventBusException(
"Could not determine event type of handler " + eventHandler); "Could not determine event type of handler " + eventHandler);
} catch (MirroredTypeException e) { } catch (MirroredTypeException e) {

View File

@ -13,7 +13,7 @@ import java.lang.annotation.*;
* <li>Specifying an event type by either * <li>Specifying an event type by either
* <ul> * <ul>
* <li>Declaring one parameter of a type that implements {@link IEvent}</li> * <li>Declaring one parameter of a type that implements {@link IEvent}</li>
* <li>Defining the class of the event using the {@link Event#eventType()} value</li> * <li>Defining the class of the event using the annotation value</li>
* </ul> * </ul>
* </li> * </li>
* <li>Return type of {@code void}</li> * <li>Return type of {@code void}</li>
@ -36,9 +36,9 @@ public @interface Event {
* This is useful when the event handler does not utilize the event instance. * This is useful when the event handler does not utilize the event instance.
* *
* @return the event type accepted by the handler * @return the event type accepted by the handler
* @since 0.0.3 * @since 1.0.0
*/ */
Class<? extends IEvent> eventType() default USE_PARAMETER.class; Class<? extends IEvent> value() default USE_PARAMETER.class;
/** /**
* Signifies that the event type the handler listens to is determined by the type of its only * Signifies that the event type the handler listens to is determined by the type of its only

View File

@ -23,8 +23,8 @@ final class EventHandler implements Comparable<EventHandler> {
private final EventListener listener; private final EventListener listener;
private final Method method; private final Method method;
private final Event annotation;
private final Class<? extends IEvent> eventType; private final Class<? extends IEvent> eventType;
private final boolean useParameter;
private final boolean polymorphic; private final boolean polymorphic;
private final int priority; private final int priority;
@ -42,13 +42,13 @@ final class EventHandler implements Comparable<EventHandler> {
EventHandler(EventListener listener, Method method, Event annotation) throws EventBusException { EventHandler(EventListener listener, Method method, Event annotation) throws EventBusException {
this.listener = listener; this.listener = listener;
this.method = method; this.method = method;
this.annotation = annotation; useParameter = annotation.value() == USE_PARAMETER.class;
// Check for correct method signature and return type // 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!"); 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!"); throw new EventBusException(method + " defines an ambiguous event type!");
if (method.getParameterCount() > 1) if (method.getParameterCount() > 1)
@ -58,14 +58,16 @@ final class EventHandler implements Comparable<EventHandler> {
throw new EventBusException(method + " does not have a return type of void!"); throw new EventBusException(method + " does not have a return type of void!");
// Determine the event type // Determine the event type
Class<? extends IEvent> eventType = annotation.eventType(); if (useParameter) {
if (eventType.equals(USE_PARAMETER.class)) {
var param = method.getParameterTypes()[0]; var param = method.getParameterTypes()[0];
if (!IEvent.class.isAssignableFrom(param)) if (!IEvent.class.isAssignableFrom(param))
throw new EventBusException(param + " is not of type IEvent!"); throw new EventBusException(param + " is not of type IEvent!");
eventType = (Class<? extends IEvent>) param; eventType = (Class<? extends IEvent>) param;
} else {
eventType = annotation.value();
} }
this.eventType = eventType;
// Determine additional handler properties
polymorphic = method.isAnnotationPresent(Polymorphic.class); polymorphic = method.isAnnotationPresent(Polymorphic.class);
priority = method.isAnnotationPresent(Priority.class) priority = method.isAnnotationPresent(Priority.class)
? method.getAnnotation(Priority.class).value() ? method.getAnnotation(Priority.class).value()
@ -93,8 +95,9 @@ final class EventHandler implements Comparable<EventHandler> {
@Override @Override
public String toString() { public String toString() {
return String.format("EventHandler[method=%s, eventType=%s, polymorphic=%b, priority=%d]", return String.format(
method, annotation.eventType(), polymorphic, priority); "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<EventHandler> {
*/ */
void execute(IEvent event) throws EventBusException { void execute(IEvent event) throws EventBusException {
try { try {
if (annotation.eventType() == USE_PARAMETER.class) if (useParameter)
method.invoke(listener, event); method.invoke(listener, event);
else else
method.invoke(listener); method.invoke(listener);
@ -122,7 +125,7 @@ final class EventHandler implements Comparable<EventHandler> {
EventListener getListener() { return listener; } EventListener getListener() { return listener; }
/** /**
* @return the event type this handler listens to * @return the event type this handler listens for
* @since 0.0.3 * @since 0.0.3
*/ */
Class<? extends IEvent> getEventType() { return eventType; } Class<? extends IEvent> getEventType() { return eventType; }

View File

@ -39,14 +39,14 @@ class CancelTest implements EventListener {
assertEquals(1, hits); assertEquals(1, hits);
} }
@Event(eventType = SimpleEvent.class) @Event(SimpleEvent.class)
@Priority(100) @Priority(100)
void onSimpleFirst() { void onSimpleFirst() {
++hits; ++hits;
bus.cancel(); bus.cancel();
} }
@Event(eventType = SimpleEvent.class) @Event(SimpleEvent.class)
@Priority(50) @Priority(50)
void onSimpleSecond() { void onSimpleSecond() {
++hits; ++hits;

View File

@ -38,7 +38,7 @@ class DispatchTest implements EventListener {
bus.dispatch(new SimpleEvent()); bus.dispatch(new SimpleEvent());
} }
@Event(eventType = SimpleEvent.class) @Event(SimpleEvent.class)
@Priority(200) @Priority(200)
@Polymorphic @Polymorphic
void onSimpleEventFirst() { void onSimpleEventFirst() {
@ -46,7 +46,7 @@ class DispatchTest implements EventListener {
assertTrue(hits == 1 || hits == 2); assertTrue(hits == 1 || hits == 2);
} }
@Event(eventType = SimpleEvent.class) @Event(SimpleEvent.class)
@Priority(150) @Priority(150)
static void onSimpleEventSecond() { static void onSimpleEventSecond() {
++hits; ++hits;