Polish "Introduce reverse on ClassFilter and MethodFilter"

See gh-26725
This commit is contained in:
Stephane Nicoll
2023-08-28 15:01:17 +02:00
parent 13e62d0b8e
commit 1f6fd16dae
4 changed files with 165 additions and 66 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.aop.support;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;
import org.springframework.aop.ClassFilter;
import org.springframework.lang.Nullable;
@@ -86,14 +87,15 @@ public abstract class ClassFilters {
}
/**
* reverse the given ClassFilter match.
* @param cf the ClassFilter
* @return a distinct ClassFilter that not matches classes
* of the given ClassFilter match
* 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 reversion(ClassFilter cf) {
Assert.notNull(cf, "ClassFilter must not be null");
return new ReversionClassFilter(cf);
public static ClassFilter negate(ClassFilter classFilter) {
Assert.notNull(classFilter, "ClassFilter must not be null");
return new NegateClassFilter(classFilter);
}
@@ -180,38 +182,38 @@ public abstract class ClassFilters {
/**
* ClassFilter implementation for an reversion of the given ClassFilter.
* ClassFilter implementation for a logical negation of the given ClassFilter.
*/
@SuppressWarnings("serial")
private static class ReversionClassFilter implements ClassFilter, Serializable {
private static class NegateClassFilter implements ClassFilter, Serializable {
private final ClassFilter filter;
private final ClassFilter original;
ReversionClassFilter(ClassFilter filter) {
this.filter = filter;
NegateClassFilter(ClassFilter original) {
this.original = original;
}
@Override
public boolean matches(Class<?> clazz) {
return !this.filter.matches(clazz);
return !this.original.matches(clazz);
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof ReversionClassFilter &&
this.filter.equals(((ReversionClassFilter)other).filter)));
public boolean equals(Object other) {
return (this == other || (other instanceof NegateClassFilter that
&& this.original.equals(that.original)));
}
@Override
public int hashCode() {
return 37 * this.filter.hashCode();
return Objects.hash(getClass(), this.original);
}
@Override
public String toString() {
return getClass().getName() + ": " + this.filter;
return "Negate " + this.original;
}
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.aop.support;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Objects;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.IntroductionAwareMethodMatcher;
@@ -82,13 +83,15 @@ public abstract class MethodMatchers {
}
/**
* reverse the given MethodMatcher match.
* @param mm the MethodMatcher
* @return a distinct MethodMatcher that not matches methods
* of the given MethodMatcher match
* 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 reversion(MethodMatcher mm) {
return new ReversionMethodMatcher(mm);
public static MethodMatcher negate(MethodMatcher methodMatcher) {
Assert.notNull(methodMatcher, "MethodMatcher must not be null");
return new NegateMethodMatcher(methodMatcher);
}
/**
@@ -349,56 +352,46 @@ public abstract class MethodMatchers {
}
/**
* MethodMatcher implementation for an reversion of the given MethodMatcher.
*/
@SuppressWarnings("serial")
private static class ReversionMethodMatcher implements MethodMatcher, Serializable {
private static class NegateMethodMatcher implements MethodMatcher, Serializable {
protected final MethodMatcher mm;
private final MethodMatcher original;
public ReversionMethodMatcher(MethodMatcher mm) {
Assert.notNull(mm, "MethodMatcher must not be null");
this.mm = mm;
NegateMethodMatcher(MethodMatcher original) {
this.original = original;
}
@Override
public boolean matches(Method method, Class<?> targetClass) {
return !this.mm.matches(method, targetClass);
return !this.original.matches(method, targetClass);
}
@Override
public boolean isRuntime() {
return this.mm.isRuntime();
return this.original.isRuntime();
}
@Override
public boolean matches(Method method, Class<?> targetClass, Object... args) {
return !this.mm.matches(method, targetClass, args);
return !this.original.matches(method, targetClass, args);
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ReversionMethodMatcher)) {
return false;
}
ReversionMethodMatcher that = (ReversionMethodMatcher) other;
return this.mm.equals(that.mm);
public boolean equals(Object other) {
return (this == other || (other instanceof NegateMethodMatcher that
&& this.original.equals(that.original)));
}
@Override
public int hashCode() {
return 37 * this.mm.hashCode();
return Objects.hash(getClass(), this.original);
}
@Override
public String toString() {
return getClass().getName() + ": " + this.mm;
return "Negate " + this.original;
}
}
}