KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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
22 import org.springframework.aop.MethodMatcher;
23 import org.springframework.aop.Pointcut;
24 import org.springframework.util.Assert;
25
26 /**
27  * Pointcut constants for matching getters and setters,
28  * and static methods useful for manipulating and evaluating pointcuts.
29  * These methods are particularly useful for composing pointcuts
30  * using the union and intersection methods.
31  *
32  * @author Rod Johnson
33  * @author Juergen Hoeller
34  */

35 public abstract class Pointcuts {
36
37     /** Pointcut matching all bean property setters, in any class */
38     public static final Pointcut SETTERS = SetterPointcut.INSTANCE;
39
40     /** Pointcut matching all bean property getters, in any class */
41     public static final Pointcut GETTERS = GetterPointcut.INSTANCE;
42
43
44     /**
45      * Match all methods that <b>either</b> (or both) of the given pointcuts matches.
46      * @param pc1 the first Pointcut
47      * @param pc2 the second Pointcut
48      * @return a distinct Pointcut that matches all methods that either
49      * of the given Pointcuts matches
50      */

51     public static Pointcut union(Pointcut pc1, Pointcut pc2) {
52         return new ComposablePointcut(pc1).union(pc2);
53     }
54
55     /**
56      * Match all methods that <b>both</b> the given pointcuts match.
57      * @param pc1 the first Pointcut
58      * @param pc2 the second Pointcut
59      * @return a distinct Pointcut that matches all methods that both
60      * of the given Pointcuts match
61      */

62     public static Pointcut intersection(Pointcut pc1, Pointcut pc2) {
63         return new ComposablePointcut(pc1).intersection(pc2);
64     }
65
66     /**
67      * Perform the least expensive check for a pointcut match.
68      * @param pointcut the pointcut to match
69      * @param method the candidate method
70      * @param targetClass the target class
71      * @param args arguments to the method
72      * @return whether there's a runtime match
73      */

74     public static boolean matches(Pointcut pointcut, Method JavaDoc method, Class JavaDoc targetClass, Object JavaDoc[] args) {
75         Assert.notNull(pointcut, "Pointcut must not be null");
76         if (pointcut == Pointcut.TRUE) {
77             return true;
78         }
79         if (pointcut.getClassFilter().matches(targetClass)) {
80             // Only check if it gets past first hurdle.
81
MethodMatcher mm = pointcut.getMethodMatcher();
82             if (mm.matches(method, targetClass)) {
83                 // We may need additional runtime (argument) check.
84
return (!mm.isRuntime() || mm.matches(method, targetClass, args));
85             }
86         }
87         return false;
88     }
89
90
91     /**
92      * Pointcut implementation that matches bean property setters.
93      */

94     private static class SetterPointcut extends StaticMethodMatcherPointcut implements Serializable JavaDoc {
95
96         public static SetterPointcut INSTANCE = new SetterPointcut();
97
98         public boolean matches(Method JavaDoc method, Class JavaDoc targetClass) {
99             return method.getName().startsWith("set") &&
100                 method.getParameterTypes().length == 1 &&
101                 method.getReturnType() == Void.TYPE;
102         }
103
104         private Object JavaDoc readResolve() {
105             return INSTANCE;
106         }
107     }
108
109
110     /**
111      * Pointcut implementation that matches bean property getters.
112      */

113     private static class GetterPointcut extends StaticMethodMatcherPointcut implements Serializable JavaDoc {
114
115         public static GetterPointcut INSTANCE = new GetterPointcut();
116
117         public boolean matches(Method JavaDoc method, Class JavaDoc targetClass) {
118             return method.getName().startsWith("get") &&
119                 method.getParameterTypes().length == 0;
120         }
121
122         private Object JavaDoc readResolve() {
123             return INSTANCE;
124         }
125     }
126
127 }
128
Popular Tags