Use void.class to determine how the event handler type is defined
Some checks failed
zdm/event-bus/pipeline/head There was a failure building this commit

The dummy class USE_PARAMETER was necessary when the IEvent interface
still existed, as void.class could not be used as a Class<? extends
IEvent>. As no explicit reference to USE_PARAMETER should be present
anywhere, a proper deprecation would've made little sense.
This commit is contained in:
Kai S. K. Engelbart 2022-01-12 19:41:53 +01:00
parent 999a172e72
commit 36ed55fd71
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
3 changed files with 7 additions and 24 deletions

View File

@ -30,13 +30,5 @@ public @interface Event {
* @return the event type accepted by the handler * @return the event type accepted by the handler
* @since 1.0.0 * @since 1.0.0
*/ */
Class<?> value() default USE_PARAMETER.class; Class<?> value() default void.class;
/**
* Signifies that the event type the handler listens to is determined by the type of its only
* parameter.
*
* @since 0.0.3
*/
static final class USE_PARAMETER {}
} }

View File

@ -3,7 +3,6 @@ package dev.kske.eventbus.core.handler;
import java.lang.reflect.*; import java.lang.reflect.*;
import dev.kske.eventbus.core.*; import dev.kske.eventbus.core.*;
import dev.kske.eventbus.core.Event.USE_PARAMETER;
/** /**
* An event handler wrapping a method annotated with {@link Event} and executing it using * An event handler wrapping a method annotated with {@link Event} and executing it using
@ -37,7 +36,7 @@ public final class ReflectiveEventHandler implements EventHandler {
boolean defPolymorphism, int defPriority) throws EventBusException { boolean defPolymorphism, int defPriority) throws EventBusException {
this.listener = listener; this.listener = listener;
this.method = method; this.method = method;
useParameter = annotation.value() == USE_PARAMETER.class; useParameter = annotation.value() == void.class;
// Check handler signature // Check handler signature
if (method.getParameterCount() == 0 && useParameter) if (method.getParameterCount() == 0 && useParameter)

View File

@ -47,13 +47,12 @@ public class EventProcessor extends AbstractProcessor {
// Task failed successfully // Task failed successfully
eventType = e.getTypeMirror(); eventType = e.getTypeMirror();
useParameter = processingEnv.getTypeUtils().isSameType(eventType, useParameter = eventType.getKind() == TypeKind.VOID;
getTypeMirror(Event.USE_PARAMETER.class));
} }
// Check handler signature // Check handler signature
boolean pass = false; boolean pass = false;
if (useParameter && eventHandler.getParameters().size() == 0) if (useParameter && eventHandler.getParameters().isEmpty())
error(eventHandler, "The method or the annotation must define the event type"); error(eventHandler, "The method or the annotation must define the event type");
else if (!useParameter && eventHandler.getParameters().size() == 1) else if (!useParameter && eventHandler.getParameters().size() == 1)
error(eventHandler, error(eventHandler,
@ -107,7 +106,8 @@ public class EventProcessor extends AbstractProcessor {
polymorphic = eventHandler.getAnnotation(Polymorphic.class).value(); polymorphic = eventHandler.getAnnotation(Polymorphic.class).value();
// Effective priority // Effective priority
int priority = hasListenerPriority ? listenerPriority.value() : defPriority; int priority =
hasListenerPriority ? listenerPriority.value() : defPriority;
boolean hasHandlerPriority = eventHandler.getAnnotation(Priority.class) != null; boolean hasHandlerPriority = eventHandler.getAnnotation(Priority.class) != null;
if (hasHandlerPriority) if (hasHandlerPriority)
priority = eventHandler.getAnnotation(Priority.class).value(); priority = eventHandler.getAnnotation(Priority.class).value();
@ -137,14 +137,6 @@ public class EventProcessor extends AbstractProcessor {
} }
} }
private TypeMirror getTypeMirror(Class<?> clazz) {
return getTypeElement(clazz).asType();
}
private TypeElement getTypeElement(Class<?> clazz) {
return processingEnv.getElementUtils().getTypeElement(clazz.getCanonicalName());
}
private void warning(Element e, String msg, Object... args) { private void warning(Element e, String msg, Object... args) {
processingEnv.getMessager().printMessage(Kind.WARNING, String.format(msg, args), e); processingEnv.getMessager().printMessage(Kind.WARNING, String.format(msg, args), e);
} }