01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20: import java.lang.reflect.Method;
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<? extends Annotation> annotationType;
38:
39: /**
40: * Create a new AnnotationClassFilter for the given annotation type.
41: * @param annotationType the annotation type to look for
42: */
43: public AnnotationMethodMatcher(
44: Class<? extends Annotation> annotationType) {
45: Assert.notNull(annotationType,
46: "Annotation type must not be null");
47: this .annotationType = annotationType;
48: }
49:
50: public boolean matches(Method method, Class 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 specificMethod = AopUtils.getMostSpecificMethod(method,
56: targetClass);
57: return (specificMethod != method && specificMethod
58: .isAnnotationPresent(this.annotationType));
59: }
60:
61: }
|