Match if empty by default in InstanceFilter and ExceptionTypeFilter

Prior to this commit, the constructors for InstanceFilter and
ExceptionTypeFilter required one to supply the matchIfEmpty flag.
However, users will typically want that to be true. Moreover, we always
supply true for the matchIfEmpty flag within the Spring Framework.

This commit therefore makes the matchIfEmpty flag optional by
introducing overloaded constructors for InstanceFilter and
ExceptionTypeFilter that only accept the includes and excludes
collections.

In addition, this commit overhauls the Javadoc for InstanceFilter and
ExceptionTypeFilter, fixing several issues in the documentation.

Furthermore, this commit applies consistent @⁠Nullable declarations
in ExceptionTypeFilter.

Closes gh-35158
This commit is contained in:
Sam Brannen
2025-07-04 18:16:52 +02:00
parent 46c40e7b96
commit d510b738f4
7 changed files with 97 additions and 39 deletions
@@ -29,7 +29,7 @@ class ExceptionTypeFilterTests {
@Test
void subClassMatch() {
ExceptionTypeFilter filter = new ExceptionTypeFilter(List.of(RuntimeException.class), null, true);
ExceptionTypeFilter filter = new ExceptionTypeFilter(List.of(RuntimeException.class), null);
assertThat(filter.match(RuntimeException.class)).isTrue();
assertThat(filter.match(IllegalStateException.class)).isTrue();
}
@@ -20,7 +20,6 @@ import java.util.List;
import org.junit.jupiter.api.Test;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
/**
@@ -30,47 +29,44 @@ class InstanceFilterTests {
@Test
void emptyFilterApplyMatchIfEmpty() {
InstanceFilter<String> filter = new InstanceFilter<>(null, null, true);
InstanceFilter<String> filter = new InstanceFilter<>(null, null);
match(filter, "foo");
match(filter, "bar");
}
@Test
void includesFilter() {
InstanceFilter<String> filter = new InstanceFilter<>(
asList("First", "Second"), null, true);
InstanceFilter<String> filter = new InstanceFilter<>(List.of("First", "Second"), null);
match(filter, "Second");
doNotMatch(filter, "foo");
}
@Test
void excludesFilter() {
InstanceFilter<String> filter = new InstanceFilter<>(
null, asList("First", "Second"), true);
InstanceFilter<String> filter = new InstanceFilter<>(null, List.of("First", "Second"));
doNotMatch(filter, "Second");
match(filter, "foo");
}
@Test
void includesAndExcludesFilters() {
InstanceFilter<String> filter = new InstanceFilter<>(
asList("foo", "Bar"), asList("First", "Second"), true);
InstanceFilter<String> filter = new InstanceFilter<>(List.of("foo", "Bar"), List.of("First", "Second"));
doNotMatch(filter, "Second");
match(filter, "foo");
}
@Test
void includesAndExcludesFiltersConflict() {
InstanceFilter<String> filter = new InstanceFilter<>(
List.of("First"), List.of("First"), true);
InstanceFilter<String> filter = new InstanceFilter<>(List.of("First"), List.of("First"));
doNotMatch(filter, "First");
}
private <T> void match(InstanceFilter<T> filter, T candidate) {
private static <T> void match(InstanceFilter<T> filter, T candidate) {
assertThat(filter.match(candidate)).as("filter '" + filter + "' should match " + candidate).isTrue();
}
private <T> void doNotMatch(InstanceFilter<T> filter, T candidate) {
private static <T> void doNotMatch(InstanceFilter<T> filter, T candidate) {
assertThat(filter.match(candidate)).as("filter '" + filter + "' should not match " + candidate).isFalse();
}