KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import org.springframework.aop.ClassFilter;
23 import org.springframework.aop.MethodMatcher;
24 import org.springframework.aop.Pointcut;
25 import org.springframework.core.ControlFlow;
26 import org.springframework.core.ControlFlowFactory;
27 import org.springframework.util.Assert;
28 import org.springframework.util.ObjectUtils;
29
30 /**
31  * Pointcut and method matcher for use in simple <b>cflow</b>-style pointcut.
32  * Note that evaluating such pointcuts is 10-15 times slower than evaluating
33  * normal pointcuts, but they are useful in some cases.
34  *
35  * @author Rod Johnson
36  * @author Rob Harrop
37  * @see org.springframework.core.ControlFlow
38  */

39 public class ControlFlowPointcut implements Pointcut, ClassFilter, MethodMatcher, Serializable JavaDoc {
40
41     private Class JavaDoc clazz;
42
43     private String JavaDoc methodName;
44
45     private int evaluations;
46
47
48     /**
49      * Construct a new pointcut that matches all control flows below that class.
50      * @param clazz the clazz
51      */

52     public ControlFlowPointcut(Class JavaDoc clazz) {
53         this(clazz, null);
54     }
55
56     /**
57      * Construct a new pointcut that matches all calls below the
58      * given method in the given class. If the method name is null,
59      * matches all control flows below that class.
60      * @param clazz the clazz
61      * @param methodName the name of the method
62      */

63     public ControlFlowPointcut(Class JavaDoc clazz, String JavaDoc methodName) {
64         Assert.notNull(clazz, "Class must not be null");
65         this.clazz = clazz;
66         this.methodName = methodName;
67     }
68
69
70     /**
71      * Subclasses can override this for greater filtering (and performance).
72      */

73     public boolean matches(Class JavaDoc clazz) {
74         return true;
75     }
76
77     /**
78      * Subclasses can override this if it's possible to filter out
79      * some candidate classes.
80      */

81     public boolean matches(Method JavaDoc method, Class JavaDoc targetClass) {
82         return true;
83     }
84
85     public boolean isRuntime() {
86         return true;
87     }
88
89     public boolean matches(Method JavaDoc method, Class JavaDoc targetClass, Object JavaDoc[] args) {
90         ++this.evaluations;
91         ControlFlow cflow = ControlFlowFactory.createControlFlow();
92         return (this.methodName != null) ? cflow.under(this.clazz, this.methodName) : cflow.under(this.clazz);
93     }
94
95     /**
96      * It's useful to know how many times we've fired, for optimization.
97      */

98     public int getEvaluations() {
99         return evaluations;
100     }
101
102
103     public ClassFilter getClassFilter() {
104         return this;
105     }
106
107     public MethodMatcher getMethodMatcher() {
108         return this;
109     }
110
111     public boolean equals(Object JavaDoc other) {
112         if (this == other) {
113             return true;
114         }
115         if (!(other instanceof ControlFlowPointcut)) {
116             return false;
117         }
118         ControlFlowPointcut that = (ControlFlowPointcut) other;
119         return (this.clazz.equals(that.clazz)) && ObjectUtils.nullSafeEquals(that.methodName, this.methodName);
120     }
121
122     public int hashCode() {
123         int code = 17;
124         code = 37 * code + this.clazz.hashCode();
125         if (this.methodName != null) {
126             code = 37 * code + this.methodName.hashCode();
127         }
128         return code;
129     }
130
131 }
132
Popular Tags