From b56f08e4412bd84adc0062316183fa4e51421e3f Mon Sep 17 00:00:00 2001 From: kske Date: Thu, 18 Feb 2021 22:18:13 +0100 Subject: [PATCH 1/2] Warn about unnecessarily polymorphic event handlers When Event Bus Proc detects a handler for a final type that uses the @Polymorphic annotation, it issues a warning. --- .../dev/kske/eventbus/proc/EventProcessor.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/event-bus-proc/src/main/java/dev/kske/eventbus/proc/EventProcessor.java b/event-bus-proc/src/main/java/dev/kske/eventbus/proc/EventProcessor.java index 355cfd7..9d9122c 100644 --- a/event-bus-proc/src/main/java/dev/kske/eventbus/proc/EventProcessor.java +++ b/event-bus-proc/src/main/java/dev/kske/eventbus/proc/EventProcessor.java @@ -80,14 +80,20 @@ public class EventProcessor extends AbstractProcessor { } } + // Detect missing or useless @Polymorphic + boolean polymorphic = eventHandler.getAnnotation(Polymorphic.class) != null; + Element eventElement = ((DeclaredType) eventType).asElement(); + // Check for handlers for abstract types that aren't polymorphic - Element eventElement = ((DeclaredType) eventType).asElement(); - if (eventHandler.getAnnotation(Polymorphic.class) == null - && (eventElement.getKind() == ElementKind.INTERFACE - || eventElement.getModifiers().contains(Modifier.ABSTRACT))) { + if (!polymorphic && (eventElement.getKind() == ElementKind.INTERFACE + || eventElement.getModifiers().contains(Modifier.ABSTRACT))) warning(eventHandler, "Parameter should be instantiable or handler should use @Polymorphic"); - } + + // Check for handlers for final types that are polymorphic + else if (polymorphic && eventElement.getModifiers().contains(Modifier.FINAL)) + warning(eventHandler, + "@Polymorphic should be removed as parameter cannot be subclassed"); } } -- 2.45.2 From 8a30493c52cee87cf7e3f0f12c9ddbd2bfafced3 Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 19 Feb 2021 11:34:58 +0100 Subject: [PATCH 2/2] Warn about unused event handler return values If an event handler has a non-void return type, a warning is issued as the event bus cannot use the returned value. In rare cases this might be justified as the event handler could be invoked directly. --- .../src/main/java/dev/kske/eventbus/proc/EventProcessor.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/event-bus-proc/src/main/java/dev/kske/eventbus/proc/EventProcessor.java b/event-bus-proc/src/main/java/dev/kske/eventbus/proc/EventProcessor.java index 9d9122c..fed6f1c 100644 --- a/event-bus-proc/src/main/java/dev/kske/eventbus/proc/EventProcessor.java +++ b/event-bus-proc/src/main/java/dev/kske/eventbus/proc/EventProcessor.java @@ -63,6 +63,10 @@ public class EventProcessor extends AbstractProcessor { else pass = true; + // Warn the user about unused return values + if (useParameter && eventHandler.getReturnType().getKind() != TypeKind.VOID) + warning(eventHandler, "Unused return value"); + // Abort checking if the handler signature is incorrect if (!pass) continue; -- 2.45.2