KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Assert;
26
27 /**
28  * Pointcut unions are tricky, because we can't just 'OR' the MethodMatchers:
29  * We need to check that each MethodMatcher's ClassFilter was happy as well.
30  *
31  * @author Rod Johnson
32  * @author Rob Harrop
33  */

34 class UnionPointcut implements Pointcut, Serializable JavaDoc {
35
36     private final Pointcut a;
37
38     private final Pointcut b;
39
40     private MethodMatcher methodMatcher;
41
42
43     public UnionPointcut(Pointcut a, Pointcut b) {
44         Assert.notNull(a, "First Pointcut must not be null");
45         Assert.notNull(b, "Second Pointcut must not be null");
46         this.a = a;
47         this.b = b;
48         this.methodMatcher = new PointcutUnionMethodMatcher();
49     }
50
51     public ClassFilter getClassFilter() {
52         return ClassFilters.union(this.a.getClassFilter(), this.b.getClassFilter());
53     }
54
55     public MethodMatcher getMethodMatcher() {
56         // Complicated: we need to consider both class filter and method matcher.
57
return this.methodMatcher;
58     }
59
60
61     public boolean equals(Object JavaDoc other) {
62         if (this == other) {
63             return true;
64         }
65         if (!(other instanceof UnionPointcut)) {
66             return false;
67         }
68         UnionPointcut that = (UnionPointcut) other;
69         return (this.a.equals(that.a) && this.b.equals(that.b));
70     }
71
72     public int hashCode() {
73         int code = 17;
74         code = 37 * code + this.a.hashCode();
75         code = 37 * code + this.b.hashCode();
76         return code;
77     }
78
79
80     private class PointcutUnionMethodMatcher implements MethodMatcher, Serializable JavaDoc {
81
82         public boolean matches(Method JavaDoc method, Class JavaDoc targetClass) {
83             return (a.getClassFilter().matches(targetClass) && a.getMethodMatcher().matches(method, targetClass)) ||
84                  (b.getClassFilter().matches(targetClass) && b.getMethodMatcher().matches(method, targetClass));
85         }
86
87         public boolean isRuntime() {
88             return a.getMethodMatcher().isRuntime() || b.getMethodMatcher().isRuntime();
89         }
90
91         public boolean matches(Method JavaDoc method, Class JavaDoc targetClass, Object JavaDoc[] args) {
92             // 2-arg matcher will already have run, so we don't need to do class filtering again.
93
return a.getMethodMatcher().matches(method, targetClass, args) ||
94                     b.getMethodMatcher().matches(method, targetClass, args);
95         }
96     }
97
98 }
99
Popular Tags