KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > support > NameMatchMethodPointcut


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.aop.support;
18
19 import java.io.Serializable JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.springframework.util.ObjectUtils;
25 import org.springframework.util.PatternMatchUtils;
26
27 /**
28  * Pointcut bean for simple method name matches, as alternative to regexp patterns.
29  * Does not handle overloaded methods: all methods *with a given name will be eligible.
30  *
31  * @author Juergen Hoeller
32  * @author Rod Johnson
33  * @author Rob Harrop
34  * @since 11.02.2004
35  * @see #isMatch
36  */

37 public class NameMatchMethodPointcut extends StaticMethodMatcherPointcut implements Serializable JavaDoc {
38
39     private List JavaDoc mappedNames = new LinkedList JavaDoc();
40
41
42     /**
43      * Convenience method when we have only a single method name to match.
44      * Use either this method or <code>setMappedNames</code>, not both.
45      * @see #setMappedNames
46      */

47     public void setMappedName(String JavaDoc mappedName) {
48         setMappedNames(new String JavaDoc[] { mappedName });
49     }
50
51     /**
52      * Set the method names defining methods to match.
53      * Matching will be the union of all these; if any match,
54      * the pointcut matches.
55      */

56     public void setMappedNames(String JavaDoc[] mappedNames) {
57         this.mappedNames = new LinkedList JavaDoc();
58         if (mappedNames != null) {
59             for (int i = 0; i < mappedNames.length; i++) {
60                 this.mappedNames.add(mappedNames[i]);
61             }
62         }
63     }
64
65     /**
66      * Add another eligible method name, in addition to those already named.
67      * Like the set methods, this method is for use when configuring proxies,
68      * before a proxy is used.
69      * <p><b>NB:</b> This method does not work after the proxy is in
70      * use, as advice chains will be cached.
71      * @param name name of the additional method that will match
72      * @return this pointcut to allow for multiple additions in one line
73      */

74     public NameMatchMethodPointcut addMethodName(String JavaDoc name) {
75         // TODO in a future release, consider a way of letting proxies
76
// cause advice changed events.
77
this.mappedNames.add(name);
78         return this;
79     }
80
81
82     public boolean matches(Method JavaDoc method, Class JavaDoc targetClass) {
83         for (int i = 0; i < this.mappedNames.size(); i++) {
84             String JavaDoc mappedName = (String JavaDoc) this.mappedNames.get(i);
85             if (mappedName.equals(method.getName()) || isMatch(method.getName(), mappedName)) {
86                 return true;
87             }
88         }
89         return false;
90     }
91
92     /**
93      * Return if the given method name matches the mapped name.
94      * <p>The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches,
95      * as well as direct equality. Can be overridden in subclasses.
96      * @param methodName the method name of the class
97      * @param mappedName the name in the descriptor
98      * @return if the names match
99      * @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
100      */

101     protected boolean isMatch(String JavaDoc methodName, String JavaDoc mappedName) {
102         return PatternMatchUtils.simpleMatch(mappedName, methodName);
103     }
104
105
106     public boolean equals(Object JavaDoc other) {
107         if (this == other) {
108             return true;
109         }
110         return (other instanceof NameMatchMethodPointcut &&
111                 ObjectUtils.nullSafeEquals(this.mappedNames, ((NameMatchMethodPointcut) other).mappedNames));
112     }
113
114     public int hashCode() {
115         return (this.mappedNames != null ? this.mappedNames.hashCode() : 0);
116     }
117
118 }
119
Popular Tags