KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.Method JavaDoc;
21
22 import org.springframework.aop.support.AopUtils;
23 import org.springframework.aop.support.StaticMethodMatcher;
24 import org.springframework.util.Assert;
25
26 /**
27  * Simple MethodMatcher that looks for a specific Java 5 annotation
28  * being present on a method (checking both the method on the invoked
29  * interface, if any, and the corresponding method on the target class).
30  *
31  * @author Juergen Hoeller
32  * @since 2.0
33  * @see AnnotationMatchingPointcut
34  */

35 public class AnnotationMethodMatcher extends StaticMethodMatcher {
36
37     private final Class JavaDoc<? extends Annotation JavaDoc> annotationType;
38
39
40     /**
41      * Create a new AnnotationClassFilter for the given annotation type.
42      * @param annotationType the annotation type to look for
43      */

44     public AnnotationMethodMatcher(Class JavaDoc<? extends Annotation JavaDoc> annotationType) {
45         Assert.notNull(annotationType, "Annotation type must not be null");
46         this.annotationType = annotationType;
47     }
48
49
50     public boolean matches(Method JavaDoc method, Class JavaDoc targetClass) {
51         if (method.isAnnotationPresent(this.annotationType)) {
52             return true;
53         }
54         // The method may be on an interface, so let's check on the target class as well.
55
Method JavaDoc specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
56         return (specificMethod != method && specificMethod.isAnnotationPresent(this.annotationType));
57     }
58
59 }
60
Popular Tags