From 17df4b4c383a6ac626b572c208f8d0f9a472ffb2 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Sat, 5 Jul 2025 12:41:24 +0200 Subject: [PATCH] Support direct matching against exceptions in ExceptionTypeFilter Prior to this commit, ExceptionTypeFilter only supported matching against an exception type. However, most use cases involve matching against an exception instance. Moreover, every use case within the core Spring Framework uses ExceptionTypeFilter to match against concrete exception instances. This commit therefore introduces an overloaded match(Throwable) method in ExceptionTypeFilter in order to provide support for the most common use cases. See gh-35109 Closes gh-35160 --- .../interceptor/CachePutInterceptor.java | 2 +- .../CacheRemoveAllInterceptor.java | 2 +- .../CacheRemoveEntryInterceptor.java | 8 +- .../interceptor/CacheResultInterceptor.java | 2 +- .../resilience/retry/MethodRetrySpec.java | 2 +- .../core/retry/DefaultRetryPolicy.java | 2 +- .../util/ExceptionTypeFilter.java | 10 +++ .../util/ExceptionTypeFilterTests.java | 74 ++++++++++--------- 8 files changed, 57 insertions(+), 45 deletions(-) diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CachePutInterceptor.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CachePutInterceptor.java index 44d37389710..15b52095e49 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CachePutInterceptor.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CachePutInterceptor.java @@ -62,7 +62,7 @@ class CachePutInterceptor extends AbstractKeyCacheInterceptor exceptionFilter.match(throwable.getClass()) && + return (method, throwable) -> exceptionFilter.match(throwable) && this.predicate.shouldRetry(method, throwable); } diff --git a/spring-core/src/main/java/org/springframework/core/retry/DefaultRetryPolicy.java b/spring-core/src/main/java/org/springframework/core/retry/DefaultRetryPolicy.java index 34a3e30c648..c9f073a96e0 100644 --- a/spring-core/src/main/java/org/springframework/core/retry/DefaultRetryPolicy.java +++ b/spring-core/src/main/java/org/springframework/core/retry/DefaultRetryPolicy.java @@ -59,7 +59,7 @@ class DefaultRetryPolicy implements RetryPolicy { @Override public boolean shouldRetry(Throwable throwable) { - return this.exceptionFilter.match(throwable.getClass()) && + return this.exceptionFilter.match(throwable) && (this.predicate == null || this.predicate.test(throwable)); } diff --git a/spring-core/src/main/java/org/springframework/util/ExceptionTypeFilter.java b/spring-core/src/main/java/org/springframework/util/ExceptionTypeFilter.java index 3bb0c4295ff..715084475aa 100644 --- a/spring-core/src/main/java/org/springframework/util/ExceptionTypeFilter.java +++ b/spring-core/src/main/java/org/springframework/util/ExceptionTypeFilter.java @@ -64,6 +64,16 @@ public class ExceptionTypeFilter extends InstanceFilter candidate) { - assertThat(filter.match(candidate)) - .as("filter '" + filter + "' should match " + candidate.getSimpleName()) + private void assertMatches(Throwable candidate) { + assertThat(this.filter.match(candidate)) + .as("filter '" + this.filter + "' should match " + candidate.getClass().getSimpleName()) .isTrue(); } - private static void assertDoesNotMatch(ExceptionTypeFilter filter, Class candidate) { - assertThat(filter.match(candidate)) - .as("filter '" + filter + "' should not match " + candidate.getSimpleName()) + private void assertDoesNotMatch(Throwable candidate) { + assertThat(this.filter.match(candidate)) + .as("filter '" + this.filter + "' should not match " + candidate.getClass().getSimpleName()) .isFalse(); }