Expose public shouldHandle(ApplicationEvent) method

Closes gh-31295
This commit is contained in:
Juergen Hoeller 2023-09-28 14:33:26 +02:00
parent 38a0e17ede
commit 86b764d4d2
1 changed files with 25 additions and 14 deletions

View File

@ -219,13 +219,14 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
for (Class<?> paramType : method.getParameterTypes()) { for (Class<?> paramType : method.getParameterTypes()) {
sj.add(paramType.getName()); sj.add(paramType.getName());
} }
return ClassUtils.getQualifiedMethodName(method) + sj.toString(); return ClassUtils.getQualifiedMethodName(method) + sj;
} }
/** /**
* Process the specified {@link ApplicationEvent}, checking if the condition * Process the specified {@link ApplicationEvent}, checking if the condition
* matches and handling a non-null result, if any. * matches and handling a non-null result, if any.
* @param event the event to process through the listener method
*/ */
public void processEvent(ApplicationEvent event) { public void processEvent(ApplicationEvent event) {
Object[] args = resolveArguments(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}. * Resolve the method arguments to use for the specified {@link ApplicationEvent}.
* <p>These arguments will be used to invoke the method handled by this instance. * <p>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); 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. * Invoke the event listener method with the given argument values.
*/ */