001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.aop.support.annotation;
018:
019: import java.lang.annotation.Annotation;
020:
021: import org.springframework.aop.ClassFilter;
022: import org.springframework.aop.MethodMatcher;
023: import org.springframework.aop.Pointcut;
024: import org.springframework.util.Assert;
025:
026: /**
027: * Simple Pointcut that looks for a specific Java 5 annotation
028: * being present on a {@link #forClassAnnotation class} or
029: * {@link #forMethodAnnotation method}.
030: *
031: * @author Juergen Hoeller
032: * @since 2.0
033: * @see AnnotationClassFilter
034: * @see AnnotationMethodMatcher
035: */
036: public class AnnotationMatchingPointcut implements Pointcut {
037:
038: private final ClassFilter classFilter;
039:
040: private final MethodMatcher methodMatcher;
041:
042: /**
043: * Create a new AnnotationMatchingPointcut for the given annotation type.
044: * @param classAnnotationType the annotation type to look for at the class level
045: */
046: public AnnotationMatchingPointcut(
047: Class<? extends Annotation> classAnnotationType) {
048: this .classFilter = new AnnotationClassFilter(
049: classAnnotationType);
050: this .methodMatcher = MethodMatcher.TRUE;
051: }
052:
053: /**
054: * Create a new AnnotationMatchingPointcut for the given annotation type.
055: * @param classAnnotationType the annotation type to look for at the class level
056: * @param checkInherited whether to explicitly check the superclasses and
057: * interfaces for the annotation type as well (even if the annotation type
058: * is not marked as inherited itself)
059: */
060: public AnnotationMatchingPointcut(
061: Class<? extends Annotation> classAnnotationType,
062: boolean checkInherited) {
063: this .classFilter = new AnnotationClassFilter(
064: classAnnotationType, checkInherited);
065: this .methodMatcher = MethodMatcher.TRUE;
066: }
067:
068: /**
069: * Create a new AnnotationMatchingPointcut for the given annotation type.
070: * @param classAnnotationType the annotation type to look for at the class level
071: * (can be <code>null</code>)
072: * @param methodAnnotationType the annotation type to look for at the method level
073: * (can be <code>null</code>)
074: */
075: public AnnotationMatchingPointcut(
076: Class<? extends Annotation> classAnnotationType,
077: Class<? extends Annotation> methodAnnotationType) {
078:
079: Assert
080: .isTrue(
081: (classAnnotationType != null || methodAnnotationType != null),
082: "Either Class annotation type or Method annotation type needs to be specified (or both)");
083:
084: if (classAnnotationType != null) {
085: this .classFilter = new AnnotationClassFilter(
086: classAnnotationType);
087: } else {
088: this .classFilter = ClassFilter.TRUE;
089: }
090:
091: if (methodAnnotationType != null) {
092: this .methodMatcher = new AnnotationMethodMatcher(
093: methodAnnotationType);
094: } else {
095: this .methodMatcher = MethodMatcher.TRUE;
096: }
097: }
098:
099: public ClassFilter getClassFilter() {
100: return this .classFilter;
101: }
102:
103: public MethodMatcher getMethodMatcher() {
104: return this .methodMatcher;
105: }
106:
107: /**
108: * Factory method for an AnnotationMatchingPointcut that matches
109: * for the specified annotation at the class level.
110: * @param annotationType the annotation type to look for at the class level
111: * @return the corresponding AnnotationMatchingPointcut
112: */
113: public static AnnotationMatchingPointcut forClassAnnotation(
114: Class<? extends Annotation> annotationType) {
115: Assert.notNull(annotationType,
116: "Annotation type must not be null");
117: return new AnnotationMatchingPointcut(annotationType);
118: }
119:
120: /**
121: * Factory method for an AnnotationMatchingPointcut that matches
122: * for the specified annotation at the method level.
123: * @param annotationType the annotation type to look for at the method level
124: * @return the corresponding AnnotationMatchingPointcut
125: */
126: public static AnnotationMatchingPointcut forMethodAnnotation(
127: Class<? extends Annotation> annotationType) {
128: Assert.notNull(annotationType,
129: "Annotation type must not be null");
130: return new AnnotationMatchingPointcut(null, annotationType);
131: }
132:
133: }
|