From 6f41d8c68dc6a67f89d888097a21d44a3bfc154f Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sat, 14 May 2011 06:22:44 +0000 Subject: [PATCH] SPR-8336 - Added constructor to AnnotationTypeFilter to allow matching interfaces as well. Reviewed by Chris. git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4308 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../type/filter/AnnotationTypeFilter.java | 16 +++++++++++-- .../core/type/AnnotationTypeFilterTests.java | 23 +++++++++++++------ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/type/filter/AnnotationTypeFilter.java b/org.springframework.core/src/main/java/org/springframework/core/type/filter/AnnotationTypeFilter.java index 5f256ad435f..e0a1aba156e 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/type/filter/AnnotationTypeFilter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/type/filter/AnnotationTypeFilter.java @@ -44,7 +44,8 @@ public class AnnotationTypeFilter extends AbstractTypeHierarchyTraversingFilter * Create a new AnnotationTypeFilter for the given annotation type. * This filter will also match meta-annotations. To disable the * meta-annotation matching, use the constructor that accepts a - * 'considerMetaAnnotations' argument. + * 'considerMetaAnnotations' argument. The filter will + * not match interfaces. * @param annotationType the annotation type to match */ public AnnotationTypeFilter(Class annotationType) { @@ -53,11 +54,22 @@ public class AnnotationTypeFilter extends AbstractTypeHierarchyTraversingFilter /** * Create a new AnnotationTypeFilter for the given annotation type. + * The filter will not match interfaces. * @param annotationType the annotation type to match * @param considerMetaAnnotations whether to also match on meta-annotations */ public AnnotationTypeFilter(Class annotationType, boolean considerMetaAnnotations) { - super(annotationType.isAnnotationPresent(Inherited.class), false); + this(annotationType, considerMetaAnnotations, false); + } + + /** + * Create a new {@link AnnotationTypeFilter} for the given annotation type. + * @param annotationType the annotation type to match + * @param considerMetaAnnotations whether to also match on meta-annotations + * @param considerInterfaces whether to also match interfaces + */ + public AnnotationTypeFilter(Class annotationType, boolean considerMetaAnnotations, boolean considerInterfaces) { + super(annotationType.isAnnotationPresent(Inherited.class), considerInterfaces); this.annotationType = annotationType; this.considerMetaAnnotations = considerMetaAnnotations; } diff --git a/org.springframework.core/src/test/java/org/springframework/core/type/AnnotationTypeFilterTests.java b/org.springframework.core/src/test/java/org/springframework/core/type/AnnotationTypeFilterTests.java index 3d086d2bc33..68841aca1db 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/type/AnnotationTypeFilterTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/type/AnnotationTypeFilterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,10 +18,8 @@ package org.springframework.core.type; import java.lang.annotation.Inherited; -import junit.framework.TestCase; - -import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.stereotype.Component; @@ -31,7 +29,7 @@ import org.springframework.stereotype.Component; * @author Juergen Hoeller */ public class AnnotationTypeFilterTests extends TestCase { - + public void testDirectAnnotationMatch() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeComponent"; @@ -52,7 +50,7 @@ public class AnnotationTypeFilterTests extends TestCase { assertFalse(filter.match(metadataReader, metadataReaderFactory)); ClassloadingAssertions.assertClassNotLoaded(classUnderTest); } - + public void testInheritedAnnotationFromBaseClassDoesMatch() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubClassOfSomeComponent"; @@ -73,7 +71,7 @@ public class AnnotationTypeFilterTests extends TestCase { assertFalse(filter.match(metadataReader, metadataReaderFactory)); ClassloadingAssertions.assertClassNotLoaded(classUnderTest); } - + public void testNonAnnotatedClassDoesntMatch() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeNonCandidateClass"; @@ -84,6 +82,17 @@ public class AnnotationTypeFilterTests extends TestCase { ClassloadingAssertions.assertClassNotLoaded(classUnderTest); } + public void testMatchesInterfacesIfConfigured() throws Exception { + + MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); + String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeComponentInterface"; + MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest); + + AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class, false, true); + + assertTrue(filter.match(metadataReader, metadataReaderFactory)); + ClassloadingAssertions.assertClassNotLoaded(classUnderTest); + } // We must use a standalone set of types to ensure that no one else is loading them // and interfering with ClassloadingAssertions.assertClassNotLoaded()