KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > aspectj > TypePatternClassFilter


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.aspectj;
18
19 import org.aspectj.weaver.tools.PointcutParser;
20 import org.aspectj.weaver.tools.TypePatternMatcher;
21
22 import org.springframework.aop.ClassFilter;
23 import org.springframework.util.Assert;
24
25 /**
26  * Spring AOP {@link ClassFilter} implementation using AspectJ type matching.
27  *
28  * @author Rod Johnson
29  * @since 2.0
30  */

31 public class TypePatternClassFilter implements ClassFilter {
32
33     private String JavaDoc typePattern;
34
35     private TypePatternMatcher aspectJTypePatternMatcher;
36
37
38     /**
39      * Creates a new instance of the {@link TypePatternClassFilter} class.
40      * <p>This is the JavaBean constructor; be sure to set the
41      * {@link #setTypePattern(String) typePattern} property, else a
42      * no doubt fatal {@link IllegalStateException} will be thrown
43      * when the {@link #matches(Class)} method is first invoked.
44      */

45     public TypePatternClassFilter() {
46     }
47
48     /**
49      * Create a fully configured {@link TypePatternClassFilter} using the
50      * given type pattern.
51      * @param typePattern the type pattern that AspectJ weaver should parse
52      * @throws IllegalArgumentException if the supplied <code>typePattern</code> is <code>null</code>
53      * or is recognized as invalid
54      */

55     public TypePatternClassFilter(String JavaDoc typePattern) {
56         setTypePattern(typePattern);
57     }
58
59
60     /**
61      * Set the AspectJ type pattern to match.
62      * <p>Examples include:
63      * <code class="code">
64      * org.springframework.beans.*
65      * </code>
66      * This will match any class or interface in the given package.
67      * <code class="code">
68      * org.springframework.beans.ITestBean+
69      * </code>
70      * This will match the <code>ITestBean</code> interface and any class
71      * that implements it.
72      * <p>These conventions are established by AspectJ, not Spring AOP.
73      * @param typePattern the type pattern that AspectJ weaver should parse
74      * @throws IllegalArgumentException if the supplied <code>typePattern</code> is <code>null</code>
75      * or is recognized as invalid
76      */

77     public void setTypePattern(String JavaDoc typePattern) {
78         Assert.notNull(typePattern);
79         this.typePattern = typePattern;
80         this.aspectJTypePatternMatcher =
81                 PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingContextClassloaderForResolution().
82                 parseTypePattern(typePattern);
83     }
84
85     public String JavaDoc getTypePattern() {
86         return typePattern;
87     }
88
89     /**
90      * Should the pointcut apply to the given interface or target class?
91      * @param clazz candidate target class
92      * @return whether the advice should apply to this candidate target class
93      * @throws IllegalStateException if no {@link #setTypePattern(String)} has been set
94      */

95     public boolean matches(Class JavaDoc clazz) {
96         if (this.aspectJTypePatternMatcher == null) {
97             throw new IllegalStateException JavaDoc("No 'typePattern' has been set via ctor/setter.");
98         }
99         return this.aspectJTypePatternMatcher.matches(clazz);
100     }
101
102 }
103
Popular Tags