|
26 | 26 | import org.springframework.util.PatternMatchUtils;
|
27 | 27 |
|
28 | 28 | /**
|
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). |
30 | 35 | *
|
31 | 36 | * <p>Does not handle overloaded methods: all methods with a given name will be eligible.
|
32 | 37 | *
|
33 | 38 | * @author Juergen Hoeller
|
34 | 39 | * @author Rod Johnson
|
35 | 40 | * @author Rob Harrop
|
| 41 | + * @author Sam Brannen |
36 | 42 | * @since 11.02.2004
|
37 | 43 | * @see #isMatch
|
| 44 | + * @see JdkRegexpMethodPointcut |
38 | 45 | */
|
39 | 46 | @SuppressWarnings("serial")
|
40 | 47 | public class NameMatchMethodPointcut extends StaticMethodMatcherPointcut implements Serializable {
|
41 | 48 |
|
42 |
| - private List<String> mappedNames = new ArrayList<>(); |
| 49 | + private List<String> mappedNamePatterns = new ArrayList<>(); |
43 | 50 |
|
44 | 51 |
|
45 | 52 | /**
|
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. |
48 | 55 | * @see #setMappedNames
|
49 | 56 | */
|
50 |
| - public void setMappedName(String mappedName) { |
51 |
| - setMappedNames(mappedName); |
| 57 | + public void setMappedName(String mappedNamePattern) { |
| 58 | + setMappedNames(mappedNamePattern); |
52 | 59 | }
|
53 | 60 |
|
54 | 61 | /**
|
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) |
58 | 65 | */
|
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)); |
61 | 68 | }
|
62 | 69 |
|
63 | 70 | /**
|
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, |
66 | 73 | * 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) |
71 | 80 | */
|
72 |
| - public NameMatchMethodPointcut addMethodName(String name) { |
73 |
| - this.mappedNames.add(name); |
| 81 | + public NameMatchMethodPointcut addMethodName(String mappedNamePattern) { |
| 82 | + this.mappedNamePatterns.add(mappedNamePattern); |
74 | 83 | return this;
|
75 | 84 | }
|
76 | 85 |
|
77 | 86 |
|
78 | 87 | @Override
|
79 | 88 | 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)) { |
82 | 91 | return true;
|
83 | 92 | }
|
84 | 93 | }
|
85 | 94 | return false;
|
86 | 95 | }
|
87 | 96 |
|
88 | 97 | /**
|
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) |
96 | 106 | */
|
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); |
99 | 109 | }
|
100 | 110 |
|
101 | 111 |
|
102 | 112 | @Override
|
103 | 113 | public boolean equals(@Nullable Object other) {
|
104 | 114 | return (this == other || (other instanceof NameMatchMethodPointcut that &&
|
105 |
| - this.mappedNames.equals(that.mappedNames))); |
| 115 | + this.mappedNamePatterns.equals(that.mappedNamePatterns))); |
106 | 116 | }
|
107 | 117 |
|
108 | 118 | @Override
|
109 | 119 | public int hashCode() {
|
110 |
| - return this.mappedNames.hashCode(); |
| 120 | + return this.mappedNamePatterns.hashCode(); |
111 | 121 | }
|
112 | 122 |
|
113 | 123 | @Override
|
114 | 124 | public String toString() {
|
115 |
| - return getClass().getName() + ": " + this.mappedNames; |
| 125 | + return getClass().getName() + ": " + this.mappedNamePatterns; |
116 | 126 | }
|
117 | 127 |
|
118 | 128 | }
|
0 commit comments