From 32b56a35c6f782f0409a4543ef779baa1d9e090c Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Sat, 5 Jul 2025 13:11:14 +0200 Subject: [PATCH] Short-circuit matching algorithm in InstanceFilter In commit 97522cfa36a0281929cb3ba6f8404dc8cd938695, I implemented a short-circuiting matching algorithm in DefaultRetryPolicy for includes and excludes, which was later copied to MethodRetrySpec. After we switched to using ExceptionTypeFilter, I realized that the matching algorithm in InstanceFilter (the superclass of ExceptionTypeFilter) does not exhibit the same short-circuiting characteristics. In light of that, this commit revises the matching algorithm in InstanceFilter to mirror the original short-circuiting algorithm in DefaultRetryPolicy. See gh-35058 See gh-35109 See gh-35160 Closes gh-35161 --- .../springframework/util/InstanceFilter.java | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/InstanceFilter.java b/spring-core/src/main/java/org/springframework/util/InstanceFilter.java index 4765837f174..75dfec80256 100644 --- a/spring-core/src/main/java/org/springframework/util/InstanceFilter.java +++ b/spring-core/src/main/java/org/springframework/util/InstanceFilter.java @@ -86,21 +86,16 @@ public class InstanceFilter { public boolean match(T instance) { Assert.notNull(instance, "Instance to match must not be null"); - boolean includesSet = !this.includes.isEmpty(); - boolean excludesSet = !this.excludes.isEmpty(); - if (!includesSet && !excludesSet) { - return this.matchIfEmpty; - } + boolean emptyIncludes = this.includes.isEmpty(); + boolean emptyExcludes = this.excludes.isEmpty(); - boolean matchIncludes = match(instance, this.includes); - boolean matchExcludes = match(instance, this.excludes); - if (!includesSet) { - return !matchExcludes; + if (emptyIncludes && emptyExcludes) { + return this.matchIfEmpty; } - if (!excludesSet) { - return matchIncludes; + if (!emptyExcludes && match(instance, this.excludes)) { + return false; } - return matchIncludes && !matchExcludes; + return (emptyIncludes || match(instance, this.includes)); } /**