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.
pull/8/head
Kai S. K. Engelbart 2021-02-18 22:18:13 +01:00
parent 4a5b94a9b7
commit b56f08e441
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
1 changed files with 11 additions and 5 deletions

View File

@ -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");
}
}