001/* 002 * Copyright 2002-2013 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 * https://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 017package org.springframework.aop.support.annotation; 018 019import java.lang.annotation.Annotation; 020import java.lang.reflect.Method; 021 022import org.springframework.aop.support.AopUtils; 023import org.springframework.aop.support.StaticMethodMatcher; 024import org.springframework.util.Assert; 025 026/** 027 * Simple MethodMatcher that looks for a specific Java 5 annotation 028 * being present on a method (checking both the method on the invoked 029 * interface, if any, and the corresponding method on the target class). 030 * 031 * @author Juergen Hoeller 032 * @since 2.0 033 * @see AnnotationMatchingPointcut 034 */ 035public class AnnotationMethodMatcher extends StaticMethodMatcher { 036 037 private final Class<? extends Annotation> annotationType; 038 039 040 /** 041 * Create a new AnnotationClassFilter for the given annotation type. 042 * @param annotationType the annotation type to look for 043 */ 044 public AnnotationMethodMatcher(Class<? extends Annotation> annotationType) { 045 Assert.notNull(annotationType, "Annotation type must not be null"); 046 this.annotationType = annotationType; 047 } 048 049 050 @Override 051 public boolean matches(Method method, Class<?> targetClass) { 052 if (method.isAnnotationPresent(this.annotationType)) { 053 return true; 054 } 055 // The method may be on an interface, so let's check on the target class as well. 056 Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass); 057 return (specificMethod != method && specificMethod.isAnnotationPresent(this.annotationType)); 058 } 059 060 @Override 061 public boolean equals(Object other) { 062 if (this == other) { 063 return true; 064 } 065 if (!(other instanceof AnnotationMethodMatcher)) { 066 return false; 067 } 068 AnnotationMethodMatcher otherMm = (AnnotationMethodMatcher) other; 069 return this.annotationType.equals(otherMm.annotationType); 070 } 071 072 @Override 073 public int hashCode() { 074 return this.annotationType.hashCode(); 075 } 076 077 @Override 078 public String toString() { 079 return getClass().getName() + ": " + this.annotationType; 080 } 081 082}