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; 020 021import org.springframework.aop.ClassFilter; 022import org.springframework.core.annotation.AnnotationUtils; 023import org.springframework.util.Assert; 024 025/** 026 * Simple ClassFilter that looks for a specific Java 5 annotation 027 * being present on a class. 028 * 029 * @author Juergen Hoeller 030 * @since 2.0 031 * @see AnnotationMatchingPointcut 032 */ 033public class AnnotationClassFilter implements ClassFilter { 034 035 private final Class<? extends Annotation> annotationType; 036 037 private final boolean checkInherited; 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 AnnotationClassFilter(Class<? extends Annotation> annotationType) { 045 this(annotationType, false); 046 } 047 048 /** 049 * Create a new AnnotationClassFilter for the given annotation type. 050 * @param annotationType the annotation type to look for 051 * @param checkInherited whether to explicitly check the superclasses and 052 * interfaces for the annotation type as well (even if the annotation type 053 * is not marked as inherited itself) 054 */ 055 public AnnotationClassFilter(Class<? extends Annotation> annotationType, boolean checkInherited) { 056 Assert.notNull(annotationType, "Annotation type must not be null"); 057 this.annotationType = annotationType; 058 this.checkInherited = checkInherited; 059 } 060 061 062 @Override 063 public boolean matches(Class<?> clazz) { 064 return (this.checkInherited ? 065 (AnnotationUtils.findAnnotation(clazz, this.annotationType) != null) : 066 clazz.isAnnotationPresent(this.annotationType)); 067 } 068 069 @Override 070 public boolean equals(Object other) { 071 if (this == other) { 072 return true; 073 } 074 if (!(other instanceof AnnotationClassFilter)) { 075 return false; 076 } 077 AnnotationClassFilter otherCf = (AnnotationClassFilter) other; 078 return (this.annotationType.equals(otherCf.annotationType) && this.checkInherited == otherCf.checkInherited); 079 } 080 081 @Override 082 public int hashCode() { 083 return this.annotationType.hashCode(); 084 } 085 086 @Override 087 public String toString() { 088 return getClass().getName() + ": " + this.annotationType; 089 } 090 091}