001/* 002 * Copyright 2002-2018 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.AnnotatedElementUtils; 023import org.springframework.lang.Nullable; 024import org.springframework.util.Assert; 025 026/** 027 * Simple ClassFilter that looks for a specific Java 5 annotation 028 * being present on a class. 029 * 030 * @author Juergen Hoeller 031 * @since 2.0 032 * @see AnnotationMatchingPointcut 033 */ 034public class AnnotationClassFilter implements ClassFilter { 035 036 private final Class<? extends Annotation> annotationType; 037 038 private final boolean checkInherited; 039 040 041 /** 042 * Create a new AnnotationClassFilter for the given annotation type. 043 * @param annotationType the annotation type to look for 044 */ 045 public AnnotationClassFilter(Class<? extends Annotation> annotationType) { 046 this(annotationType, false); 047 } 048 049 /** 050 * Create a new AnnotationClassFilter for the given annotation type. 051 * @param annotationType the annotation type to look for 052 * @param checkInherited whether to also check the superclasses and 053 * interfaces as well as meta-annotations for the annotation type 054 * (i.e. whether to use {@link AnnotatedElementUtils#hasAnnotation} 055 * semantics instead of standard Java {@link Class#isAnnotationPresent}) 056 */ 057 public AnnotationClassFilter(Class<? extends Annotation> annotationType, boolean checkInherited) { 058 Assert.notNull(annotationType, "Annotation type must not be null"); 059 this.annotationType = annotationType; 060 this.checkInherited = checkInherited; 061 } 062 063 064 @Override 065 public boolean matches(Class<?> clazz) { 066 return (this.checkInherited ? AnnotatedElementUtils.hasAnnotation(clazz, this.annotationType) : 067 clazz.isAnnotationPresent(this.annotationType)); 068 } 069 070 @Override 071 public boolean equals(@Nullable Object other) { 072 if (this == other) { 073 return true; 074 } 075 if (!(other instanceof AnnotationClassFilter)) { 076 return false; 077 } 078 AnnotationClassFilter otherCf = (AnnotationClassFilter) other; 079 return (this.annotationType.equals(otherCf.annotationType) && this.checkInherited == otherCf.checkInherited); 080 } 081 082 @Override 083 public int hashCode() { 084 return this.annotationType.hashCode(); 085 } 086 087 @Override 088 public String toString() { 089 return getClass().getName() + ": " + this.annotationType; 090 } 091 092}