KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > support > annotation > AnnotationMatchingPointcut


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.annotation;
18
19 import java.lang.annotation.Annotation JavaDoc;
20
21 import org.springframework.aop.ClassFilter;
22 import org.springframework.aop.MethodMatcher;
23 import org.springframework.aop.Pointcut;
24 import org.springframework.util.Assert;
25
26 /**
27  * Simple Pointcut that looks for a specific Java 5 annotation
28  * being present on a {@link #forClassAnnotation class} or
29  * {@link #forMethodAnnotation method}.
30  *
31  * @author Juergen Hoeller
32  * @since 2.0
33  * @see AnnotationClassFilter
34  * @see AnnotationMethodMatcher
35  */

36 public class AnnotationMatchingPointcut implements Pointcut {
37
38     private final ClassFilter classFilter;
39
40     private final MethodMatcher methodMatcher;
41
42
43     /**
44      * Create a new AnnotationMatchingPointcut for the given annotation type.
45      * @param classAnnotationType the annotation type to look for at the class level
46      */

47     public AnnotationMatchingPointcut(Class JavaDoc<? extends Annotation JavaDoc> classAnnotationType) {
48         this(classAnnotationType, null);
49     }
50
51     /**
52      * Create a new AnnotationMatchingPointcut for the given annotation type.
53      * @param classAnnotationType the annotation type to look for at the class level
54      * (can be <code>null</code>)
55      * @param methodAnnotationType the annotation type to look for at the method level
56      * (can be <code>null</code>)
57      */

58     public AnnotationMatchingPointcut(
59             Class JavaDoc<? extends Annotation JavaDoc> classAnnotationType, Class JavaDoc<? extends Annotation JavaDoc> methodAnnotationType) {
60
61         Assert.isTrue((classAnnotationType != null || methodAnnotationType != null),
62                 "Either Class annotation type or Method annotation type needs to be specified (or both)");
63
64         if (classAnnotationType != null) {
65             this.classFilter = new AnnotationClassFilter(classAnnotationType);
66         }
67         else {
68             this.classFilter = ClassFilter.TRUE;
69         }
70
71         if (methodAnnotationType != null) {
72             this.methodMatcher = new AnnotationMethodMatcher(methodAnnotationType);
73         }
74         else {
75             this.methodMatcher = MethodMatcher.TRUE;
76         }
77     }
78
79
80     public ClassFilter getClassFilter() {
81         return this.classFilter;
82     }
83
84     public MethodMatcher getMethodMatcher() {
85         return this.methodMatcher;
86     }
87
88
89     /**
90      * Factory method for an AnnotationMatchingPointcut that matches
91      * for the specified annotation at the class level.
92      * @param annotationType the annotation type to look for at the class level
93      * @return the corresponding AnnotationMatchingPointcut
94      */

95     public static AnnotationMatchingPointcut forClassAnnotation(Class JavaDoc<? extends Annotation JavaDoc> annotationType) {
96         Assert.notNull(annotationType, "Annotation type must not be null");
97         return new AnnotationMatchingPointcut(annotationType);
98     }
99
100     /**
101      * Factory method for an AnnotationMatchingPointcut that matches
102      * for the specified annotation at the method level.
103      * @param annotationType the annotation type to look for at the method level
104      * @return the corresponding AnnotationMatchingPointcut
105      */

106     public static AnnotationMatchingPointcut forMethodAnnotation(Class JavaDoc<? extends Annotation JavaDoc> annotationType) {
107         Assert.notNull(annotationType, "Annotation type must not be null");
108         return new AnnotationMatchingPointcut(null, annotationType);
109     }
110
111 }
112
Popular Tags