From 36ed55fd711f1e68b3b51f40cd2fe87f249f1040 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 12 Jan 2022 19:41:53 +0100 Subject: [PATCH 1/4] Use void.class to determine how the event handler type is defined The dummy class USE_PARAMETER was necessary when the IEvent interface still existed, as void.class could not be used as a Class. As no explicit reference to USE_PARAMETER should be present anywhere, a proper deprecation would've made little sense. --- .../java/dev/kske/eventbus/core/Event.java | 10 +--------- .../core/handler/ReflectiveEventHandler.java | 3 +-- .../dev/kske/eventbus/proc/EventProcessor.java | 18 +++++------------- 3 files changed, 7 insertions(+), 24 deletions(-) diff --git a/core/src/main/java/dev/kske/eventbus/core/Event.java b/core/src/main/java/dev/kske/eventbus/core/Event.java index f194c56..392604a 100644 --- a/core/src/main/java/dev/kske/eventbus/core/Event.java +++ b/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; } diff --git a/core/src/main/java/dev/kske/eventbus/core/handler/ReflectiveEventHandler.java b/core/src/main/java/dev/kske/eventbus/core/handler/ReflectiveEventHandler.java index 22ecea5..3eeb455 100644 --- a/core/src/main/java/dev/kske/eventbus/core/handler/ReflectiveEventHandler.java +++ b/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) diff --git a/proc/src/main/java/dev/kske/eventbus/proc/EventProcessor.java b/proc/src/main/java/dev/kske/eventbus/proc/EventProcessor.java index fd0567b..1238fd2 100644 --- a/proc/src/main/java/dev/kske/eventbus/proc/EventProcessor.java +++ b/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.45.2 From 3aef7d5bcb3c5599e4bec94a63c9708e8b84f3a5 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 12 Jan 2022 20:04:02 +0100 Subject: [PATCH 2/4] Only update handler accessibility if necessary --- .../kske/eventbus/core/handler/ReflectiveEventHandler.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/dev/kske/eventbus/core/handler/ReflectiveEventHandler.java b/core/src/main/java/dev/kske/eventbus/core/handler/ReflectiveEventHandler.java index 3eeb455..dcfa572 100644 --- a/core/src/main/java/dev/kske/eventbus/core/handler/ReflectiveEventHandler.java +++ b/core/src/main/java/dev/kske/eventbus/core/handler/ReflectiveEventHandler.java @@ -57,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 -- 2.45.2 From 8615a0e021ed1223b2e81140ede7248d6527a185 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 12 Jan 2022 20:10:44 +0100 Subject: [PATCH 3/4] Fix Event Bus Proc module name --- proc/src/main/java/module-info.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proc/src/main/java/module-info.java b/proc/src/main/java/module-info.java index e09c4be..1876af0 100644 --- a/proc/src/main/java/module-info.java +++ b/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; -- 2.45.2 From 6ee4e111618799401b300310831b5e6e4a833a85 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 12 Jan 2022 20:17:24 +0100 Subject: [PATCH 4/4] Fix inconsistent test --- core/src/test/java/dev/kske/eventbus/core/InheritanceTest.java | 1 + .../java/dev/kske/eventbus/core/SimpleEventListenerBase.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/test/java/dev/kske/eventbus/core/InheritanceTest.java b/core/src/test/java/dev/kske/eventbus/core/InheritanceTest.java index b72438d..2f3ac6c 100644 --- a/core/src/test/java/dev/kske/eventbus/core/InheritanceTest.java +++ b/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(); diff --git a/core/src/test/java/dev/kske/eventbus/core/SimpleEventListenerBase.java b/core/src/test/java/dev/kske/eventbus/core/SimpleEventListenerBase.java index 1540655..3123652 100644 --- a/core/src/test/java/dev/kske/eventbus/core/SimpleEventListenerBase.java +++ b/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(); -- 2.45.2