KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.Arrays JavaDoc;
24
25 import org.springframework.util.Assert;
26 import org.springframework.util.ObjectUtils;
27 import org.springframework.util.StringUtils;
28
29 /**
30  * Abstract base regular expression pointcut bean. JavaBean properties are:
31  * <ul>
32  * <li>pattern: regular expression for the fully-qualified method names to match.
33  * The exact regexp syntax will depend on the subclass (e.g. Perl5 regular expressions)
34  * <li>patterns: alternative property taking a String array of patterns. The result will
35  * be the union of these patterns.
36  * </ul>
37  *
38  * <p>Note: the regular expressions must be a match. For example,
39  * <code>.*get.*</code> will match com.mycom.Foo.getBar().
40  * <code>get.*</code> will not.
41  *
42  * <p>This base class is serializable. Subclasses should declare all fields transient
43  * - the initPatternRepresentation method in this class will be invoked again on the
44  * client side on deserialization.
45  *
46  * @author Rod Johnson
47  * @author Juergen Hoeller
48  * @author Rob Harrop
49  * @since 1.1
50  * @see Perl5RegexpMethodPointcut
51  * @see JdkRegexpMethodPointcut
52  */

53 public abstract class AbstractRegexpMethodPointcut extends StaticMethodMatcherPointcut
54         implements Serializable JavaDoc {
55
56     /** Regular expressions to match */
57     private String JavaDoc[] patterns = new String JavaDoc[0];
58
59     /** Regaular expressions <strong>not</strong> to match */
60     private String JavaDoc[] excludedPatterns = new String JavaDoc[0];
61
62
63     /**
64      * Convenience method when we have only a single pattern.
65      * Use either this method or {@link #setPatterns}, not both.
66      * @see #setPatterns
67      */

68     public void setPattern(String JavaDoc pattern) {
69         setPatterns(new String JavaDoc[] {pattern});
70     }
71
72     /**
73      * Set the regular expressions defining methods to match.
74      * Matching will be the union of all these; if any match,
75      * the pointcut matches.
76      */

77     public void setPatterns(String JavaDoc[] patterns) {
78         Assert.notEmpty(patterns, "'patterns' must not be empty");
79         this.patterns = new String JavaDoc[patterns.length];
80         for (int i = 0; i < patterns.length; i++) {
81             this.patterns[i] = StringUtils.trimWhitespace(patterns[i]);
82         }
83         initPatternRepresentation(this.patterns);
84     }
85
86     /**
87      * Return the regular expressions for method matching.
88      */

89     public String JavaDoc[] getPatterns() {
90         return this.patterns;
91     }
92
93     /**
94      * Convenience method when we have only a single exclusion pattern.
95      * Use either this method or {@link #setExcludedPatterns}, not both.
96      * @see #setExcludedPatterns
97      */

98     public void setExcludedPattern(String JavaDoc excludedPattern) {
99         setExcludedPatterns(new String JavaDoc[] {excludedPattern});
100     }
101
102     /**
103      * Set the regular expressions defining methods to match for exclusion.
104      * Matching will be the union of all these; if any match,
105      * the pointcut matches.
106      */

107     public void setExcludedPatterns(String JavaDoc[] excludedPatterns) {
108         Assert.notEmpty(excludedPatterns, "'excludedPatterns' must not be empty");
109         this.excludedPatterns = new String JavaDoc[excludedPatterns.length];
110         for (int i = 0; i < excludedPatterns.length; i++) {
111             this.excludedPatterns[i] = StringUtils.trimWhitespace(excludedPatterns[i]);
112         }
113         initExcludedPatternRepresentation(this.excludedPatterns);
114     }
115
116     /**
117      * Returns the regular expressions for exclusion matching.
118      */

119     public String JavaDoc[] getExcludedPatterns() {
120         return this.excludedPatterns;
121     }
122
123
124     /**
125      * Try to match the regular expression against the fully qualified name
126      * of the method's declaring class, plus the name of the method.
127      * Note that the declaring class is that class that originally declared
128      * the method, not necessarily the class that's currently exposing it.
129      * <p>For example, "java.lang.Object.hashCode" matches any subclass
130      * of Object's <code>hashCode()</code> method.
131      */

132     public final boolean matches(Method JavaDoc method, Class JavaDoc targetClass) {
133         // TODO use target class here?
134
String JavaDoc patt = method.getDeclaringClass().getName() + "." + method.getName();
135         for (int i = 0; i < this.patterns.length; i++) {
136             boolean matched = matches(patt, i);
137             if (matched) {
138                 for (int j = 0; j < this.excludedPatterns.length; j++) {
139                     boolean excluded = matchesExclusion(patt, j);
140                     if(excluded) {
141                         return false;
142                     }
143                 }
144                 return true;
145             }
146         }
147         return false;
148     }
149
150
151     /**
152      * Subclasses must implement this to initialize regexp pointcuts.
153      * Can be invoked multiple times.
154      * <p>This method will be invoked from the setPatterns method,
155      * and also on deserialization.
156      * @param patterns the patterns to initialize
157      * @throws IllegalArgumentException in case of an invalid pattern
158      */

159     protected abstract void initPatternRepresentation(String JavaDoc[] patterns) throws IllegalArgumentException JavaDoc;
160
161     protected abstract void initExcludedPatternRepresentation(String JavaDoc[] excludedPatterns) throws IllegalArgumentException JavaDoc;
162
163     /**
164      * Does the pattern at the given index match this string?
165      * @param pattern <code>String</code> pattern to match
166      * @param patternIndex index of pattern from 0
167      * @return <code>true</code> if there is a match, else <code>false</code>.
168      */

169     protected abstract boolean matches(String JavaDoc pattern, int patternIndex);
170
171     /**
172      * Does the exclusion pattern at the given index match this string?
173      * @param pattern <code>String</code> pattern to match.
174      * @param patternIndex index of pattern starting from 0.
175      * @return <code>true</code> if there is a match, else <code>false</code>.
176      */

177     protected abstract boolean matchesExclusion(String JavaDoc pattern, int patternIndex);
178
179
180     public boolean equals(Object JavaDoc other) {
181         if (this == other) {
182             return true;
183         }
184         if (!(other instanceof AbstractRegexpMethodPointcut)) {
185             return false;
186         }
187         AbstractRegexpMethodPointcut otherPointcut = (AbstractRegexpMethodPointcut) other;
188         return (Arrays.equals(this.patterns, otherPointcut.patterns) &&
189                 Arrays.equals(this.excludedPatterns, otherPointcut.excludedPatterns));
190     }
191
192     public int hashCode() {
193         int result = 27;
194         for (int i = 0; i < this.patterns.length; i++) {
195             String JavaDoc pattern = this.patterns[i];
196             result = 13 * result + pattern.hashCode();
197         }
198         for (int i = 0; i < this.excludedPatterns.length; i++) {
199             String JavaDoc excludedPattern = this.excludedPatterns[i];
200             result = 13 * result + excludedPattern.hashCode();
201         }
202         return result;
203     }
204
205     public String JavaDoc toString() {
206         return getClass().getName() + ": patterns " + ObjectUtils.nullSafeToString(this.patterns) +
207                 ", excluded patterns " + ObjectUtils.nullSafeToString(this.excludedPatterns);
208     }
209
210
211     //---------------------------------------------------------------------
212
// Serialization support
213
//---------------------------------------------------------------------
214

215     private void readObject(ObjectInputStream JavaDoc ois) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
216         // Rely on default serialization; just initialize state after deserialization.
217
ois.defaultReadObject();
218
219         // Ask subclass to reinitialize.
220
initPatternRepresentation(this.patterns);
221         initExcludedPatternRepresentation(this.excludedPatterns);
222     }
223
224 }
225
Popular Tags