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
This commit is contained in:
parent
f4bfd5766a
commit
6f41d8c68d
|
|
@ -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
|
||||
* '<code>considerMetaAnnotations</code>' argument.
|
||||
* '<code>considerMetaAnnotations</code>' argument. The filter will
|
||||
* not match interfaces.
|
||||
* @param annotationType the annotation type to match
|
||||
*/
|
||||
public AnnotationTypeFilter(Class<? extends Annotation> 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<? extends Annotation> 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<? extends Annotation> annotationType, boolean considerMetaAnnotations, boolean considerInterfaces) {
|
||||
super(annotationType.isAnnotationPresent(Inherited.class), considerInterfaces);
|
||||
this.annotationType = annotationType;
|
||||
this.considerMetaAnnotations = considerMetaAnnotations;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in New Issue