Skip to content

Commit c0f79ca

Browse files
committed
Polish ControlFlowPointcut[Tests]
1 parent 66aac7e commit c0f79ca

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

spring-aop/src/main/java/org/springframework/aop/support/ControlFlowPointcut.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
import org.springframework.util.ObjectUtils;
2929

3030
/**
31-
* Pointcut and method matcher for use in simple <b>cflow</b>-style pointcut.
32-
* Note that evaluating such pointcuts is 10-15 times slower than evaluating
31+
* Pointcut and method matcher for use as a simple <b>cflow</b>-style pointcut.
32+
*
33+
* <p>Note that evaluating such pointcuts is 10-15 times slower than evaluating
3334
* normal pointcuts, but they are useful in some cases.
3435
*
3536
* @author Rod Johnson
@@ -45,22 +46,23 @@ public class ControlFlowPointcut implements Pointcut, ClassFilter, MethodMatcher
4546
@Nullable
4647
private final String methodName;
4748

48-
private final AtomicInteger evaluations = new AtomicInteger();
49+
private final AtomicInteger evaluationCount = new AtomicInteger();
4950

5051

5152
/**
52-
* Construct a new pointcut that matches all control flows below that class.
53-
* @param clazz the clazz
53+
* Construct a new pointcut that matches all control flows below the given class.
54+
* @param clazz the class
5455
*/
5556
public ControlFlowPointcut(Class<?> clazz) {
5657
this(clazz, null);
5758
}
5859

5960
/**
6061
* Construct a new pointcut that matches all calls below the given method
61-
* in the given class. If no method name is given, matches all control flows
62+
* in the given class.
63+
* <p>If no method name is given, the pointcut matches all control flows
6264
* below the given class.
63-
* @param clazz the clazz
65+
* @param clazz the class
6466
* @param methodName the name of the method (may be {@code null})
6567
*/
6668
public ControlFlowPointcut(Class<?> clazz, @Nullable String methodName) {
@@ -72,6 +74,7 @@ public ControlFlowPointcut(Class<?> clazz, @Nullable String methodName) {
7274

7375
/**
7476
* Subclasses can override this for greater filtering (and performance).
77+
* <p>The default implementation always returns {@code true}.
7578
*/
7679
@Override
7780
public boolean matches(Class<?> clazz) {
@@ -80,6 +83,7 @@ public boolean matches(Class<?> clazz) {
8083

8184
/**
8285
* Subclasses can override this if it's possible to filter out some candidate classes.
86+
* <p>The default implementation always returns {@code true}.
8387
*/
8488
@Override
8589
public boolean matches(Method method, Class<?> targetClass) {
@@ -93,7 +97,7 @@ public boolean isRuntime() {
9397

9498
@Override
9599
public boolean matches(Method method, Class<?> targetClass, Object... args) {
96-
this.evaluations.incrementAndGet();
100+
this.evaluationCount.incrementAndGet();
97101

98102
for (StackTraceElement element : new Throwable().getStackTrace()) {
99103
if (element.getClassName().equals(this.clazz.getName()) &&
@@ -105,10 +109,12 @@ public boolean matches(Method method, Class<?> targetClass, Object... args) {
105109
}
106110

107111
/**
108-
* It's useful to know how many times we've fired, for optimization.
112+
* Get the number of times {@link #matches(Method, Class, Object...)} has been
113+
* evaluated.
114+
* <p>Useful for optimization and testing purposes.
109115
*/
110116
public int getEvaluations() {
111-
return this.evaluations.get();
117+
return this.evaluationCount.get();
112118
}
113119

114120

spring-aop/src/test/java/org/springframework/aop/support/ControlFlowPointcutTests.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
import static org.assertj.core.api.Assertions.assertThat;
2828

2929
/**
30+
* Tests for {@link ControlFlowPointcut}.
31+
*
3032
* @author Rod Johnson
3133
* @author Chris Beams
3234
*/
33-
public class ControlFlowPointcutTests {
35+
class ControlFlowPointcutTests {
3436

3537
@Test
36-
public void testMatches() {
38+
void matches() {
3739
TestBean target = new TestBean();
3840
target.setAge(27);
3941
NopInterceptor nop = new NopInterceptor();
@@ -45,10 +47,12 @@ public void testMatches() {
4547
// Not advised, not under One
4648
assertThat(proxied.getAge()).isEqualTo(target.getAge());
4749
assertThat(nop.getCount()).isEqualTo(0);
50+
assertThat(cflow.getEvaluations()).isEqualTo(1);
4851

4952
// Will be advised
5053
assertThat(new One().getAge(proxied)).isEqualTo(target.getAge());
5154
assertThat(nop.getCount()).isEqualTo(1);
55+
assertThat(cflow.getEvaluations()).isEqualTo(2);
5256

5357
// Won't be advised
5458
assertThat(new One().nomatch(proxied)).isEqualTo(target.getAge());
@@ -64,7 +68,7 @@ public void testMatches() {
6468
* expensive.
6569
*/
6670
@Test
67-
public void testSelectiveApplication() {
71+
void selectiveApplication() {
6872
TestBean target = new TestBean();
6973
target.setAge(27);
7074
NopInterceptor nop = new NopInterceptor();
@@ -91,24 +95,26 @@ public void testSelectiveApplication() {
9195
}
9296

9397
@Test
94-
public void testEqualsAndHashCode() throws Exception {
98+
void equalsAndHashCode() throws Exception {
9599
assertThat(new ControlFlowPointcut(One.class)).isEqualTo(new ControlFlowPointcut(One.class));
96100
assertThat(new ControlFlowPointcut(One.class, "getAge")).isEqualTo(new ControlFlowPointcut(One.class, "getAge"));
97-
assertThat(new ControlFlowPointcut(One.class, "getAge").equals(new ControlFlowPointcut(One.class))).isFalse();
98-
assertThat(new ControlFlowPointcut(One.class).hashCode()).isEqualTo(new ControlFlowPointcut(One.class).hashCode());
99-
assertThat(new ControlFlowPointcut(One.class, "getAge").hashCode()).isEqualTo(new ControlFlowPointcut(One.class, "getAge").hashCode());
100-
assertThat(new ControlFlowPointcut(One.class, "getAge").hashCode()).isNotEqualTo(new ControlFlowPointcut(One.class).hashCode());
101+
assertThat(new ControlFlowPointcut(One.class, "getAge")).isNotEqualTo(new ControlFlowPointcut(One.class));
102+
103+
assertThat(new ControlFlowPointcut(One.class)).hasSameHashCodeAs(new ControlFlowPointcut(One.class));
104+
assertThat(new ControlFlowPointcut(One.class, "getAge")).hasSameHashCodeAs(new ControlFlowPointcut(One.class, "getAge"));
105+
assertThat(new ControlFlowPointcut(One.class, "getAge")).doesNotHaveSameHashCodeAs(new ControlFlowPointcut(One.class));
101106
}
102107

103108
@Test
104-
public void testToString() {
105-
assertThat(new ControlFlowPointcut(One.class).toString())
109+
void testToString() {
110+
assertThat(new ControlFlowPointcut(One.class)).asString()
106111
.isEqualTo(ControlFlowPointcut.class.getName() + ": class = " + One.class.getName() + "; methodName = null");
107-
assertThat(new ControlFlowPointcut(One.class, "getAge").toString())
112+
assertThat(new ControlFlowPointcut(One.class, "getAge")).asString()
108113
.isEqualTo(ControlFlowPointcut.class.getName() + ": class = " + One.class.getName() + "; methodName = getAge");
109114
}
110115

111-
public class One {
116+
117+
private static class One {
112118
int getAge(ITestBean proxied) {
113119
return proxied.getAge();
114120
}

0 commit comments

Comments
 (0)