From 86b764d4d2588b89ae9ca10b019ce268521db9de Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 28 Sep 2023 14:33:26 +0200 Subject: [PATCH] Expose public shouldHandle(ApplicationEvent) method Closes gh-31295 --- .../ApplicationListenerMethodAdapter.java | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java b/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java index 927231aab7c..9790d7070c8 100644 --- a/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java +++ b/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java @@ -219,13 +219,14 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe for (Class paramType : method.getParameterTypes()) { sj.add(paramType.getName()); } - return ClassUtils.getQualifiedMethodName(method) + sj.toString(); + return ClassUtils.getQualifiedMethodName(method) + sj; } /** * Process the specified {@link ApplicationEvent}, checking if the condition * matches and handling a non-null result, if any. + * @param event the event to process through the listener method */ public void processEvent(ApplicationEvent event) { Object[] args = resolveArguments(event); @@ -240,6 +241,29 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe } } + /** + * Determine whether the listener method would actually handle the given + * event, checking if the condition matches. + * @param event the event to process through the listener method + * @since 6.1 + */ + public boolean shouldHandle(ApplicationEvent event) { + return shouldHandle(event, resolveArguments(event)); + } + + private boolean shouldHandle(ApplicationEvent event, @Nullable Object[] args) { + if (args == null) { + return false; + } + String condition = getCondition(); + if (StringUtils.hasText(condition)) { + Assert.notNull(this.evaluator, "EventExpressionEvaluator must not be null"); + return this.evaluator.condition( + condition, event, this.targetMethod, this.methodKey, args, this.applicationContext); + } + return true; + } + /** * Resolve the method arguments to use for the specified {@link ApplicationEvent}. *

These arguments will be used to invoke the method handled by this instance. @@ -319,19 +343,6 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe logger.error("Unexpected error occurred in asynchronous listener", t); } - private boolean shouldHandle(ApplicationEvent event, @Nullable Object[] args) { - if (args == null) { - return false; - } - String condition = getCondition(); - if (StringUtils.hasText(condition)) { - Assert.notNull(this.evaluator, "EventExpressionEvaluator must not be null"); - return this.evaluator.condition( - condition, event, this.targetMethod, this.methodKey, args, this.applicationContext); - } - return true; - } - /** * Invoke the event listener method with the given argument values. */