KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.regex.Matcher JavaDoc;
20 import java.util.regex.Pattern JavaDoc;
21 import java.util.regex.PatternSyntaxException JavaDoc;
22
23 /**
24  * Java 1.4+ regular expression pointcut. JavaBean properties are:
25  * <ul>
26  * <li>pattern: Java 1.4 regular expression for the fully-qualified method names to match
27  * <li>patterns: alternative property taking a String array of patterns. The result will
28  * be the union of these patterns.
29  * </ul>
30  *
31  * <p>Note: the regular expressions must be a match. For example,
32  * <code>.*get.*</code> will match com.mycom.Foo.getBar().
33  * <code>get.*</code> will not.
34  *
35  * <p>Requires JDK 1.4+, as it builds on the <code>java.util.regex</code> package.
36  * As alternative on JDK 1.3 or for Perl5-style regular expression parsing,
37  * consider Perl5RegexpMethodPointcut.
38  *
39  * @author Dmitriy Kopylenko
40  * @author Rob Harrop
41  * @since 1.1
42  * @see Perl5RegexpMethodPointcut
43  */

44 public class JdkRegexpMethodPointcut extends AbstractRegexpMethodPointcut {
45     
46     /**
47      * Java 1.4 compiled form of the patterns.
48      */

49     private transient Pattern JavaDoc[] compiledPatterns = new Pattern JavaDoc[0];
50
51     /**
52      * Java 1.4 compiled form of the exclusion patterns.
53      */

54     private transient Pattern JavaDoc[] compiledExclusionPatterns = new Pattern JavaDoc[0];
55
56     /**
57      * Initialize Java 1.4 {@link Pattern Patterns} from the supplied <code>String[]</code>.
58      */

59     protected void initPatternRepresentation(String JavaDoc[] patterns) throws PatternSyntaxException JavaDoc {
60         this.compiledPatterns = compilePatterns(patterns);
61     }
62
63     /**
64      * Returns <code>true</code> if the {@link Pattern} at index <code>patternIndex</code>
65      * matches the supplied candidate <code>String</code>.
66      */

67     protected boolean matches(String JavaDoc pattern, int patternIndex) {
68         Matcher JavaDoc matcher = this.compiledPatterns[patternIndex].matcher(pattern);
69         return matcher.matches();
70     }
71
72     /**
73      * Initialize Java 1.4 exclusion {@link Pattern Patterns} from the supplied <code>String[]</code>.
74      */

75     protected void initExcludedPatternRepresentation(String JavaDoc[] excludedPatterns) throws IllegalArgumentException JavaDoc {
76         this.compiledExclusionPatterns = compilePatterns(excludedPatterns);
77     }
78
79     /**
80      * Returns <code>true</code> if the exclusion {@link Pattern} at index <code>patternIndex</code>
81      * matches the supplied candidate <code>String</code>.
82      */

83     protected boolean matchesExclusion(String JavaDoc candidate, int patternIndex) {
84         Matcher JavaDoc matcher = this.compiledExclusionPatterns[patternIndex].matcher(candidate);
85         return matcher.matches();
86     }
87
88     /**
89      * Compiles the supplied <code>String[]</code> into an array of
90      * {@link Pattern} objects and returns that array.
91      */

92     private Pattern JavaDoc[] compilePatterns(String JavaDoc[] source) {
93         Pattern JavaDoc[] destination = new Pattern JavaDoc[source.length];
94         for (int i = 0; i < source.length; i++) {
95             destination[i] = Pattern.compile(source[i]);
96         }
97         return destination;
98     }
99
100 }
101
Popular Tags