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 (classAnnotationType, null);
049: }
050:
051: /**
052: * Create a new AnnotationMatchingPointcut for the given annotation type.
053: * @param classAnnotationType the annotation type to look for at the class level
054: * (can be <code>null</code>)
055: * @param methodAnnotationType the annotation type to look for at the method level
056: * (can be <code>null</code>)
057: */
058: public AnnotationMatchingPointcut(
059: Class<? extends Annotation> classAnnotationType,
060: Class<? extends Annotation> methodAnnotationType) {
061:
062: Assert
063: .isTrue(
064: (classAnnotationType != null || methodAnnotationType != null),
065: "Either Class annotation type or Method annotation type needs to be specified (or both)");
066:
067: if (classAnnotationType != null) {
068: this .classFilter = new AnnotationClassFilter(
069: classAnnotationType);
070: } else {
071: this .classFilter = ClassFilter.TRUE;
072: }
073:
074: if (methodAnnotationType != null) {
075: this .methodMatcher = new AnnotationMethodMatcher(
076: methodAnnotationType);
077: } else {
078: this .methodMatcher = MethodMatcher.TRUE;
079: }
080: }
081:
082: public ClassFilter getClassFilter() {
083: return this .classFilter;
084: }
085:
086: public MethodMatcher getMethodMatcher() {
087: return this .methodMatcher;
088: }
089:
090: /**
091: * Factory method for an AnnotationMatchingPointcut that matches
092: * for the specified annotation at the class level.
093: * @param annotationType the annotation type to look for at the class level
094: * @return the corresponding AnnotationMatchingPointcut
095: */
096: public static AnnotationMatchingPointcut forClassAnnotation(
097: Class<? extends Annotation> annotationType) {
098: Assert.notNull(annotationType,
099: "Annotation type must not be null");
100: return new AnnotationMatchingPointcut(annotationType);
101: }
102:
103: /**
104: * Factory method for an AnnotationMatchingPointcut that matches
105: * for the specified annotation at the method level.
106: * @param annotationType the annotation type to look for at the method level
107: * @return the corresponding AnnotationMatchingPointcut
108: */
109: public static AnnotationMatchingPointcut forMethodAnnotation(
110: Class<? extends Annotation> annotationType) {
111: Assert.notNull(annotationType,
112: "Annotation type must not be null");
113: return new AnnotationMatchingPointcut(null, annotationType);
114: }
115:
116: }
|