Skip to content

Commit f329140

Browse files
committed
Fixed ClassFilterAwareUnionMethodMatcher equals implementation
Issue: SPR-10604
1 parent 9e20a25 commit f329140

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -102,8 +102,9 @@ public static boolean matches(MethodMatcher mm, Method method, Class targetClass
102102
@SuppressWarnings("serial")
103103
private static class UnionMethodMatcher implements IntroductionAwareMethodMatcher, Serializable {
104104

105-
private MethodMatcher mm1;
106-
private MethodMatcher mm2;
105+
private final MethodMatcher mm1;
106+
107+
private final MethodMatcher mm2;
107108

108109
public UnionMethodMatcher(MethodMatcher mm1, MethodMatcher mm2) {
109110
Assert.notNull(mm1, "First MethodMatcher must not be null");
@@ -172,6 +173,7 @@ public int hashCode() {
172173
private static class ClassFilterAwareUnionMethodMatcher extends UnionMethodMatcher {
173174

174175
private final ClassFilter cf1;
176+
175177
private final ClassFilter cf2;
176178

177179
public ClassFilterAwareUnionMethodMatcher(MethodMatcher mm1, ClassFilter cf1, MethodMatcher mm2, ClassFilter cf2) {
@@ -195,11 +197,17 @@ public boolean equals(Object other) {
195197
if (this == other) {
196198
return true;
197199
}
198-
if (!(other instanceof ClassFilterAwareUnionMethodMatcher)) {
200+
if (!super.equals(other)) {
199201
return false;
200202
}
201-
ClassFilterAwareUnionMethodMatcher that = (ClassFilterAwareUnionMethodMatcher) other;
202-
return (this.cf1.equals(that.cf1) && this.cf2.equals(that.cf2) && super.equals(other));
203+
ClassFilter otherCf1 = ClassFilter.TRUE;
204+
ClassFilter otherCf2 = ClassFilter.TRUE;
205+
if (other instanceof ClassFilterAwareUnionMethodMatcher) {
206+
ClassFilterAwareUnionMethodMatcher cfa = (ClassFilterAwareUnionMethodMatcher) other;
207+
otherCf1 = cfa.cf1;
208+
otherCf2 = cfa.cf2;
209+
}
210+
return (this.cf1.equals(otherCf1) && this.cf2.equals(otherCf2));
203211
}
204212
}
205213

@@ -210,8 +218,9 @@ public boolean equals(Object other) {
210218
@SuppressWarnings("serial")
211219
private static class IntersectionMethodMatcher implements IntroductionAwareMethodMatcher, Serializable {
212220

213-
private MethodMatcher mm1;
214-
private MethodMatcher mm2;
221+
private final MethodMatcher mm1;
222+
223+
private final MethodMatcher mm2;
215224

216225
public IntersectionMethodMatcher(MethodMatcher mm1, MethodMatcher mm2) {
217226
Assert.notNull(mm1, "First MethodMatcher must not be null");

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ public final class MethodMatchersTests {
4343

4444
private final Method IOTHER_ABSQUATULATE;
4545

46+
4647
public MethodMatchersTests() throws Exception {
4748
EXCEPTION_GETMESSAGE = Exception.class.getMethod("getMessage", (Class[]) null);
4849
ITESTBEAN_GETAGE = ITestBean.class.getMethod("getAge", (Class[]) null);
4950
ITESTBEAN_SETAGE = ITestBean.class.getMethod("setAge", new Class[] { int.class });
5051
IOTHER_ABSQUATULATE = IOther.class.getMethod("absquatulate", (Class[]) null);
5152
}
5253

54+
5355
@Test
5456
public void testDefaultMatchesAll() throws Exception {
5557
MethodMatcher defaultMm = MethodMatcher.TRUE;
@@ -99,30 +101,41 @@ public void testStaticMethodMatcherUnion() throws Exception {
99101
assertTrue("Matched setAge method", union.matches(ITESTBEAN_SETAGE, TestBean.class));
100102
assertTrue("Matched getAge method", union.matches(ITESTBEAN_GETAGE, TestBean.class));
101103
assertFalse("Didn't matched absquatulate method", union.matches(IOTHER_ABSQUATULATE, TestBean.class));
104+
}
102105

106+
@Test
107+
public void testUnionEquals() {
108+
MethodMatcher first = MethodMatchers.union(MethodMatcher.TRUE, MethodMatcher.TRUE);
109+
MethodMatcher second = new ComposablePointcut(MethodMatcher.TRUE).union(new ComposablePointcut(MethodMatcher.TRUE)).getMethodMatcher();
110+
assertTrue(first.equals(second));
111+
assertTrue(second.equals(first));
103112
}
104113

105114

106115
public static class StartsWithMatcher extends StaticMethodMatcher {
107-
private String prefix;
116+
117+
private final String prefix;
118+
108119
public StartsWithMatcher(String s) {
109120
this.prefix = s;
110121
}
122+
111123
@Override
112124
public boolean matches(Method m, Class<?> targetClass) {
113125
return m.getName().startsWith(prefix);
114126
}
115127
}
116128

117-
118129
private static class TestDynamicMethodMatcherWhichMatches extends DynamicMethodMatcher {
130+
119131
@Override
120132
public boolean matches(Method m, Class<?> targetClass, Object[] args) {
121133
return true;
122134
}
123135
}
124136

125137
private static class TestDynamicMethodMatcherWhichDoesNotMatch extends DynamicMethodMatcher {
138+
126139
@Override
127140
public boolean matches(Method m, Class<?> targetClass, Object[] args) {
128141
return false;

0 commit comments

Comments
 (0)