Skip to content

Commit c5def4c

Browse files
committed
Document pattern matching support in NameMatchMethodPointcut
Closes gh-31500
1 parent 555404e commit c5def4c

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

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

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,93 +26,103 @@
2626
import org.springframework.util.PatternMatchUtils;
2727

2828
/**
29-
* Pointcut bean for simple method name matches, as an alternative to regexp patterns.
29+
* Pointcut bean for simple method name matches, as an alternative to regular
30+
* expression patterns.
31+
*
32+
* <p>Each configured method name can be an exact method name or a method name
33+
* pattern (see {@link #isMatch(String, String)} for details on the supported
34+
* pattern styles).
3035
*
3136
* <p>Does not handle overloaded methods: all methods with a given name will be eligible.
3237
*
3338
* @author Juergen Hoeller
3439
* @author Rod Johnson
3540
* @author Rob Harrop
41+
* @author Sam Brannen
3642
* @since 11.02.2004
3743
* @see #isMatch
44+
* @see JdkRegexpMethodPointcut
3845
*/
3946
@SuppressWarnings("serial")
4047
public class NameMatchMethodPointcut extends StaticMethodMatcherPointcut implements Serializable {
4148

42-
private List<String> mappedNames = new ArrayList<>();
49+
private List<String> mappedNamePatterns = new ArrayList<>();
4350

4451

4552
/**
46-
* Convenience method when we have only a single method name to match.
47-
* Use either this method or {@code setMappedNames}, not both.
53+
* Convenience method for configuring a single method name pattern.
54+
* <p>Use either this method or {@link #setMappedNames(String...)}, but not both.
4855
* @see #setMappedNames
4956
*/
50-
public void setMappedName(String mappedName) {
51-
setMappedNames(mappedName);
57+
public void setMappedName(String mappedNamePattern) {
58+
setMappedNames(mappedNamePattern);
5259
}
5360

5461
/**
55-
* Set the method names defining methods to match.
56-
* Matching will be the union of all these; if any match,
57-
* the pointcut matches.
62+
* Set the method name patterns defining methods to match.
63+
* <p>Matching will be the union of all these; if any match, the pointcut matches.
64+
* @see #setMappedName(String)
5865
*/
59-
public void setMappedNames(String... mappedNames) {
60-
this.mappedNames = new ArrayList<>(Arrays.asList(mappedNames));
66+
public void setMappedNames(String... mappedNamePatterns) {
67+
this.mappedNamePatterns = new ArrayList<>(Arrays.asList(mappedNamePatterns));
6168
}
6269

6370
/**
64-
* Add another eligible method name, in addition to those already named.
65-
* Like the set methods, this method is for use when configuring proxies,
71+
* Add another method name pattern, in addition to those already configured.
72+
* <p>Like the "set" methods, this method is for use when configuring proxies,
6673
* before a proxy is used.
67-
* <p><b>NB:</b> This method does not work after the proxy is in
68-
* use, as advice chains will be cached.
69-
* @param name the name of the additional method that will match
70-
* @return this pointcut to allow for multiple additions in one line
74+
* <p><b>NOTE:</b> This method does not work after the proxy is in use, since
75+
* advice chains will be cached.
76+
* @param mappedNamePattern the additional method name pattern
77+
* @return this pointcut to allow for method chaining
78+
* @see #setMappedNames(String...)
79+
* @see #setMappedName(String)
7180
*/
72-
public NameMatchMethodPointcut addMethodName(String name) {
73-
this.mappedNames.add(name);
81+
public NameMatchMethodPointcut addMethodName(String mappedNamePattern) {
82+
this.mappedNamePatterns.add(mappedNamePattern);
7483
return this;
7584
}
7685

7786

7887
@Override
7988
public boolean matches(Method method, Class<?> targetClass) {
80-
for (String mappedName : this.mappedNames) {
81-
if (mappedName.equals(method.getName()) || isMatch(method.getName(), mappedName)) {
89+
for (String mappedNamePattern : this.mappedNamePatterns) {
90+
if (mappedNamePattern.equals(method.getName()) || isMatch(method.getName(), mappedNamePattern)) {
8291
return true;
8392
}
8493
}
8594
return false;
8695
}
8796

8897
/**
89-
* Return if the given method name matches the mapped name.
90-
* <p>The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches,
91-
* as well as direct equality. Can be overridden in subclasses.
92-
* @param methodName the method name of the class
93-
* @param mappedName the name in the descriptor
94-
* @return if the names match
95-
* @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
98+
* Determine if the given method name matches the mapped name pattern.
99+
* <p>The default implementation checks for {@code xxx*}, {@code *xxx},
100+
* {@code *xxx*}, and {@code xxx*yyy} matches, as well as direct equality.
101+
* <p>Can be overridden in subclasses.
102+
* @param methodName the method name to check
103+
* @param mappedNamePattern the method name pattern
104+
* @return {@code true} if the method name matches the pattern
105+
* @see PatternMatchUtils#simpleMatch(String, String)
96106
*/
97-
protected boolean isMatch(String methodName, String mappedName) {
98-
return PatternMatchUtils.simpleMatch(mappedName, methodName);
107+
protected boolean isMatch(String methodName, String mappedNamePattern) {
108+
return PatternMatchUtils.simpleMatch(mappedNamePattern, methodName);
99109
}
100110

101111

102112
@Override
103113
public boolean equals(@Nullable Object other) {
104114
return (this == other || (other instanceof NameMatchMethodPointcut that &&
105-
this.mappedNames.equals(that.mappedNames)));
115+
this.mappedNamePatterns.equals(that.mappedNamePatterns)));
106116
}
107117

108118
@Override
109119
public int hashCode() {
110-
return this.mappedNames.hashCode();
120+
return this.mappedNamePatterns.hashCode();
111121
}
112122

113123
@Override
114124
public String toString() {
115-
return getClass().getName() + ": " + this.mappedNames;
125+
return getClass().getName() + ": " + this.mappedNamePatterns;
116126
}
117127

118128
}

0 commit comments

Comments
 (0)