Skip to content

Commit 86b764d

Browse files
committed
Expose public shouldHandle(ApplicationEvent) method
Closes gh-31295
1 parent 38a0e17 commit 86b764d

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,14 @@ protected String getDefaultListenerId() {
219219
for (Class<?> paramType : method.getParameterTypes()) {
220220
sj.add(paramType.getName());
221221
}
222-
return ClassUtils.getQualifiedMethodName(method) + sj.toString();
222+
return ClassUtils.getQualifiedMethodName(method) + sj;
223223
}
224224

225225

226226
/**
227227
* Process the specified {@link ApplicationEvent}, checking if the condition
228228
* matches and handling a non-null result, if any.
229+
* @param event the event to process through the listener method
229230
*/
230231
public void processEvent(ApplicationEvent event) {
231232
Object[] args = resolveArguments(event);
@@ -240,6 +241,29 @@ public void processEvent(ApplicationEvent event) {
240241
}
241242
}
242243

244+
/**
245+
* Determine whether the listener method would actually handle the given
246+
* event, checking if the condition matches.
247+
* @param event the event to process through the listener method
248+
* @since 6.1
249+
*/
250+
public boolean shouldHandle(ApplicationEvent event) {
251+
return shouldHandle(event, resolveArguments(event));
252+
}
253+
254+
private boolean shouldHandle(ApplicationEvent event, @Nullable Object[] args) {
255+
if (args == null) {
256+
return false;
257+
}
258+
String condition = getCondition();
259+
if (StringUtils.hasText(condition)) {
260+
Assert.notNull(this.evaluator, "EventExpressionEvaluator must not be null");
261+
return this.evaluator.condition(
262+
condition, event, this.targetMethod, this.methodKey, args, this.applicationContext);
263+
}
264+
return true;
265+
}
266+
243267
/**
244268
* Resolve the method arguments to use for the specified {@link ApplicationEvent}.
245269
* <p>These arguments will be used to invoke the method handled by this instance.
@@ -319,19 +343,6 @@ protected void handleAsyncError(Throwable t) {
319343
logger.error("Unexpected error occurred in asynchronous listener", t);
320344
}
321345

322-
private boolean shouldHandle(ApplicationEvent event, @Nullable Object[] args) {
323-
if (args == null) {
324-
return false;
325-
}
326-
String condition = getCondition();
327-
if (StringUtils.hasText(condition)) {
328-
Assert.notNull(this.evaluator, "EventExpressionEvaluator must not be null");
329-
return this.evaluator.condition(
330-
condition, event, this.targetMethod, this.methodKey, args, this.applicationContext);
331-
}
332-
return true;
333-
}
334-
335346
/**
336347
* Invoke the event listener method with the given argument values.
337348
*/

0 commit comments

Comments
 (0)