Polish ControlFlowPointcut[Tests]

This commit is contained in:
Sam Brannen
2023-10-25 14:18:13 +02:00
parent 66aac7e359
commit c0f79ca3bf
2 changed files with 34 additions and 22 deletions

View File

@@ -28,8 +28,9 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Pointcut and method matcher for use in simple <b>cflow</b>-style pointcut.
* Note that evaluating such pointcuts is 10-15 times slower than evaluating
* Pointcut and method matcher for use as a simple <b>cflow</b>-style pointcut.
*
* <p>Note that evaluating such pointcuts is 10-15 times slower than evaluating
* normal pointcuts, but they are useful in some cases.
*
* @author Rod Johnson
@@ -45,12 +46,12 @@ public class ControlFlowPointcut implements Pointcut, ClassFilter, MethodMatcher
@Nullable
private final String methodName;
private final AtomicInteger evaluations = new AtomicInteger();
private final AtomicInteger evaluationCount = new AtomicInteger();
/**
* Construct a new pointcut that matches all control flows below that class.
* @param clazz the clazz
* Construct a new pointcut that matches all control flows below the given class.
* @param clazz the class
*/
public ControlFlowPointcut(Class<?> clazz) {
this(clazz, null);
@@ -58,9 +59,10 @@ public class ControlFlowPointcut implements Pointcut, ClassFilter, MethodMatcher
/**
* Construct a new pointcut that matches all calls below the given method
* in the given class. If no method name is given, matches all control flows
* in the given class.
* <p>If no method name is given, the pointcut matches all control flows
* below the given class.
* @param clazz the clazz
* @param clazz the class
* @param methodName the name of the method (may be {@code null})
*/
public ControlFlowPointcut(Class<?> clazz, @Nullable String methodName) {
@@ -72,6 +74,7 @@ public class ControlFlowPointcut implements Pointcut, ClassFilter, MethodMatcher
/**
* Subclasses can override this for greater filtering (and performance).
* <p>The default implementation always returns {@code true}.
*/
@Override
public boolean matches(Class<?> clazz) {
@@ -80,6 +83,7 @@ public class ControlFlowPointcut implements Pointcut, ClassFilter, MethodMatcher
/**
* Subclasses can override this if it's possible to filter out some candidate classes.
* <p>The default implementation always returns {@code true}.
*/
@Override
public boolean matches(Method method, Class<?> targetClass) {
@@ -93,7 +97,7 @@ public class ControlFlowPointcut implements Pointcut, ClassFilter, MethodMatcher
@Override
public boolean matches(Method method, Class<?> targetClass, Object... args) {
this.evaluations.incrementAndGet();
this.evaluationCount.incrementAndGet();
for (StackTraceElement element : new Throwable().getStackTrace()) {
if (element.getClassName().equals(this.clazz.getName()) &&
@@ -105,10 +109,12 @@ public class ControlFlowPointcut implements Pointcut, ClassFilter, MethodMatcher
}
/**
* It's useful to know how many times we've fired, for optimization.
* Get the number of times {@link #matches(Method, Class, Object...)} has been
* evaluated.
* <p>Useful for optimization and testing purposes.
*/
public int getEvaluations() {
return this.evaluations.get();
return this.evaluationCount.get();
}