Browse Source

Merge pull request 'Remove USE_PARAMETER' (#35) from f/remove-use-parameter into develop

Reviewed-on: https://git.kske.dev/zdm/event-bus/pulls/35
Reviewed-by: delvh <leon@kske.dev>
develop
kske 2 weeks ago
parent
commit
a8d858e8c7
Signed by: KSKE Git GPG Key ID: 70F2F9206EDC1FCE
  1. 10
      core/src/main/java/dev/kske/eventbus/core/Event.java
  2. 8
      core/src/main/java/dev/kske/eventbus/core/handler/ReflectiveEventHandler.java
  3. 1
      core/src/test/java/dev/kske/eventbus/core/InheritanceTest.java
  4. 2
      core/src/test/java/dev/kske/eventbus/core/SimpleEventListenerBase.java
  5. 18
      proc/src/main/java/dev/kske/eventbus/proc/EventProcessor.java
  6. 2
      proc/src/main/java/module-info.java

10
core/src/main/java/dev/kske/eventbus/core/Event.java

@ -30,13 +30,5 @@ public @interface Event {
* @return the event type accepted by the handler
* @since 1.0.0
*/
Class<?> value() default USE_PARAMETER.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 {}
Class<?> value() default void.class;
}

8
core/src/main/java/dev/kske/eventbus/core/handler/ReflectiveEventHandler.java

@ -3,7 +3,6 @@ package dev.kske.eventbus.core.handler;
import java.lang.reflect.*;
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
@ -37,7 +36,7 @@ public final class ReflectiveEventHandler implements EventHandler {
boolean defPolymorphism, int defPriority) throws EventBusException {
this.listener = listener;
this.method = method;
useParameter = annotation.value() == USE_PARAMETER.class;
useParameter = annotation.value() == void.class;
// Check handler signature
if (method.getParameterCount() == 0 && useParameter)
@ -58,8 +57,9 @@ public final class ReflectiveEventHandler implements EventHandler {
? method.getAnnotation(Priority.class).value()
: defPriority;
// Allow access if the method is non-public
method.setAccessible(true);
// Try to allow access if the method is not accessible
if (!method.canAccess(Modifier.isStatic(method.getModifiers()) ? null : listener))
method.setAccessible(true);
}
@Override

1
core/src/test/java/dev/kske/eventbus/core/InheritanceTest.java

@ -35,6 +35,7 @@ class InheritanceTest extends SimpleEventListenerBase implements SimpleEventList
}
@Event
@Priority(250)
private void onSimpleEventPrivate(SimpleEvent event) {
assertSame(0, event.getCounter());
event.increment();

2
core/src/test/java/dev/kske/eventbus/core/SimpleEventListenerBase.java

@ -16,8 +16,8 @@ abstract class SimpleEventListenerBase {
fail("This handler should not be invoked");
}
@Priority(150)
@Event
@Priority(150)
private void onSimpleEventPrivate(SimpleEvent event) {
assertSame(1, event.getCounter());
event.increment();

18
proc/src/main/java/dev/kske/eventbus/proc/EventProcessor.java

@ -47,13 +47,12 @@ public class EventProcessor extends AbstractProcessor {
// Task failed successfully
eventType = e.getTypeMirror();
useParameter = processingEnv.getTypeUtils().isSameType(eventType,
getTypeMirror(Event.USE_PARAMETER.class));
useParameter = eventType.getKind() == TypeKind.VOID;
}
// Check handler signature
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");
else if (!useParameter && eventHandler.getParameters().size() == 1)
error(eventHandler,
@ -100,14 +99,15 @@ public class EventProcessor extends AbstractProcessor {
boolean hasListenerPriority = listenerPriority != null;
// Effective polymorphism
boolean polymorphic =
boolean polymorphic =
hasListenerPolymorphic ? listenerPolymorphic.value() : defPolymorphic;
boolean hasHandlerPolymorphic = eventHandler.getAnnotation(Polymorphic.class) != null;
if (hasHandlerPolymorphic)
polymorphic = eventHandler.getAnnotation(Polymorphic.class).value();
// Effective priority
int priority = hasListenerPriority ? listenerPriority.value() : defPriority;
int priority =
hasListenerPriority ? listenerPriority.value() : defPriority;
boolean hasHandlerPriority = eventHandler.getAnnotation(Priority.class) != null;
if (hasHandlerPriority)
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) {
processingEnv.getMessager().printMessage(Kind.WARNING, String.format(msg, args), e);
}

2
proc/src/main/java/module-info.java

@ -5,7 +5,7 @@
* @author Kai S. K. Engelbart
* @since 1.0.0
*/
module dev.kske.eventbus.ap {
module dev.kske.eventbus.proc {
requires java.compiler;
requires dev.kske.eventbus.core;

Loading…
Cancel
Save