Skip to content

Commit

Permalink
Merge pull request #26725 from Jinhui-Z
Browse files Browse the repository at this point in the history
* pr/26725:
  Polish "Introduce reverse on ClassFilter and MethodFilter"
  Introduce reverse on ClassFilter and MethodFilter

Closes gh-26725
  • Loading branch information
snicoll committed Aug 28, 2023
2 parents c918418 + 1f6fd16 commit 2d37715
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 2 deletions.
Expand Up @@ -18,6 +18,7 @@

import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;

import org.springframework.aop.ClassFilter;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -85,6 +86,18 @@ public static ClassFilter intersection(ClassFilter[] classFilters) {
return new IntersectionClassFilter(classFilters);
}

/**
* Return a class filter that represents the logical negation of the specified
* filter instance.
* @param classFilter the {@link ClassFilter} to negate
* @return a filter that represents the logical negation of the specified filter
* @since 6.1
*/
public static ClassFilter negate(ClassFilter classFilter) {
Assert.notNull(classFilter, "ClassFilter must not be null");
return new NegateClassFilter(classFilter);
}


/**
* ClassFilter implementation for a union of the given ClassFilters.
Expand Down Expand Up @@ -167,4 +180,40 @@ public String toString() {

}


/**
* ClassFilter implementation for a logical negation of the given ClassFilter.
*/
@SuppressWarnings("serial")
private static class NegateClassFilter implements ClassFilter, Serializable {

private final ClassFilter original;

NegateClassFilter(ClassFilter original) {
this.original = original;
}

@Override
public boolean matches(Class<?> clazz) {
return !this.original.matches(clazz);
}

@Override
public boolean equals(Object other) {
return (this == other || (other instanceof NegateClassFilter that
&& this.original.equals(that.original)));
}

@Override
public int hashCode() {
return Objects.hash(getClass(), this.original);
}

@Override
public String toString() {
return "Negate " + this.original;
}

}

}
Expand Up @@ -18,6 +18,7 @@

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Objects;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.IntroductionAwareMethodMatcher;
Expand Down Expand Up @@ -81,6 +82,18 @@ public static MethodMatcher intersection(MethodMatcher mm1, MethodMatcher mm2) {
new IntersectionIntroductionAwareMethodMatcher(mm1, mm2) : new IntersectionMethodMatcher(mm1, mm2));
}

/**
* Return a method matcher that represents the logical negation of the specified
* matcher instance.
* @param methodMatcher the {@link MethodMatcher} to negate
* @return a matcher that represents the logical negation of the specified matcher
* @since 6.1
*/
public static MethodMatcher negate(MethodMatcher methodMatcher) {
Assert.notNull(methodMatcher, "MethodMatcher must not be null");
return new NegateMethodMatcher(methodMatcher);
}

/**
* Apply the given MethodMatcher to the given Method, supporting an
* {@link org.springframework.aop.IntroductionAwareMethodMatcher}
Expand Down Expand Up @@ -338,4 +351,47 @@ public boolean matches(Method method, Class<?> targetClass, boolean hasIntroduct
}
}


@SuppressWarnings("serial")
private static class NegateMethodMatcher implements MethodMatcher, Serializable {

private final MethodMatcher original;

NegateMethodMatcher(MethodMatcher original) {
this.original = original;
}

@Override
public boolean matches(Method method, Class<?> targetClass) {
return !this.original.matches(method, targetClass);
}

@Override
public boolean isRuntime() {
return this.original.isRuntime();
}

@Override
public boolean matches(Method method, Class<?> targetClass, Object... args) {
return !this.original.matches(method, targetClass, args);
}

@Override
public boolean equals(Object other) {
return (this == other || (other instanceof NegateMethodMatcher that
&& this.original.equals(that.original)));
}

@Override
public int hashCode() {
return Objects.hash(getClass(), this.original);
}

@Override
public String toString() {
return "Negate " + this.original;
}

}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,9 @@
import org.springframework.core.NestedRuntimeException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
* Unit tests for {@link ClassFilters}.
Expand Down Expand Up @@ -66,4 +69,65 @@ void intersection() {
.matches("^.+IntersectionClassFilter: \\[.+RootClassFilter: .+Exception, .+RootClassFilter: .+NestedRuntimeException\\]$");
}

@Test
void negateClassFilter() {
ClassFilter filter = mock(ClassFilter.class);
given(filter.matches(String.class)).willReturn(true);
ClassFilter negate = ClassFilters.negate(filter);
assertThat(negate.matches(String.class)).isFalse();
verify(filter).matches(String.class);
}

@Test
void negateTrueClassFilter() {
ClassFilter negate = ClassFilters.negate(ClassFilter.TRUE);
assertThat(negate.matches(String.class)).isFalse();
assertThat(negate.matches(Object.class)).isFalse();
assertThat(negate.matches(Integer.class)).isFalse();
}

@Test
void negateTrueClassFilterAppliedTwice() {
ClassFilter negate = ClassFilters.negate(ClassFilters.negate(ClassFilter.TRUE));
assertThat(negate.matches(String.class)).isTrue();
assertThat(negate.matches(Object.class)).isTrue();
assertThat(negate.matches(Integer.class)).isTrue();
}

@Test
void negateIsNotEqualsToOriginalFilter() {
ClassFilter original = ClassFilter.TRUE;
ClassFilter negate = ClassFilters.negate(original);
assertThat(original).isNotEqualTo(negate);
}

@Test
void negateOnSameFilterIsEquals() {
ClassFilter original = ClassFilter.TRUE;
ClassFilter first = ClassFilters.negate(original);
ClassFilter second = ClassFilters.negate(original);
assertThat(first).isEqualTo(second);
}

@Test
void negateHasNotSameHashCodeAsOriginalFilter() {
ClassFilter original = ClassFilter.TRUE;
ClassFilter negate = ClassFilters.negate(original);
assertThat(original).doesNotHaveSameHashCodeAs(negate);
}

@Test
void negateOnSameFilterHasSameHashCode() {
ClassFilter original = ClassFilter.TRUE;
ClassFilter first = ClassFilters.negate(original);
ClassFilter second = ClassFilters.negate(original);
assertThat(first).hasSameHashCodeAs(second);
}

@Test
void toStringIncludesRepresentationOfOriginalFilter() {
ClassFilter original = ClassFilter.TRUE;
assertThat(ClassFilters.negate(original)).hasToString("Negate " + original);
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,13 +28,16 @@
import org.springframework.lang.Nullable;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

/**
* @author Juergen Hoeller
* @author Chris Beams
*/
public class MethodMatchersTests {

private final static Method TEST_METHOD = mock(Method.class);

private final Method EXCEPTION_GETMESSAGE;

private final Method ITESTBEAN_SETAGE;
Expand Down Expand Up @@ -111,6 +114,65 @@ public void testUnionEquals() {
assertThat(second.equals(first)).isTrue();
}

@Test
void negateMethodMatcher() {
MethodMatcher getterMatcher = new StartsWithMatcher("get");
MethodMatcher negate = MethodMatchers.negate(getterMatcher);
assertThat(negate.matches(ITESTBEAN_SETAGE, int.class)).isTrue();
}

@Test
void negateTrueMethodMatcher() {
MethodMatcher negate = MethodMatchers.negate(MethodMatcher.TRUE);
assertThat(negate.matches(TEST_METHOD, String.class)).isFalse();
assertThat(negate.matches(TEST_METHOD, Object.class)).isFalse();
assertThat(negate.matches(TEST_METHOD, Integer.class)).isFalse();
}

@Test
void negateTrueMethodMatcherAppliedTwice() {
MethodMatcher negate = MethodMatchers.negate(MethodMatchers.negate(MethodMatcher.TRUE));
assertThat(negate.matches(TEST_METHOD, String.class)).isTrue();
assertThat(negate.matches(TEST_METHOD, Object.class)).isTrue();
assertThat(negate.matches(TEST_METHOD, Integer.class)).isTrue();
}

@Test
void negateIsNotEqualsToOriginalMatcher() {
MethodMatcher original = MethodMatcher.TRUE;
MethodMatcher negate = MethodMatchers.negate(original);
assertThat(original).isNotEqualTo(negate);
}

@Test
void negateOnSameMatcherIsEquals() {
MethodMatcher original = MethodMatcher.TRUE;
MethodMatcher first = MethodMatchers.negate(original);
MethodMatcher second = MethodMatchers.negate(original);
assertThat(first).isEqualTo(second);
}

@Test
void negateHasNotSameHashCodeAsOriginalMatcher() {
MethodMatcher original = MethodMatcher.TRUE;
MethodMatcher negate = MethodMatchers.negate(original);
assertThat(original).doesNotHaveSameHashCodeAs(negate);
}

@Test
void negateOnSameMatcherHasSameHashCode() {
MethodMatcher original = MethodMatcher.TRUE;
MethodMatcher first = MethodMatchers.negate(original);
MethodMatcher second = MethodMatchers.negate(original);
assertThat(first).hasSameHashCodeAs(second);
}

@Test
void toStringIncludesRepresentationOfOriginalMatcher() {
MethodMatcher original = MethodMatcher.TRUE;
assertThat(MethodMatchers.negate(original)).hasToString("Negate " + original);
}


public static class StartsWithMatcher extends StaticMethodMatcher {

Expand Down

0 comments on commit 2d37715

Please sign in to comment.