提交 22a8a668 编写于 作者: J Juergen Hoeller

AnnotationTypeFilter assumes no custom annotations on common Java types

Issue: SPR-16667
上级 78681c63
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
......@@ -71,7 +71,9 @@ public class AnnotationTypeFilter extends AbstractTypeHierarchyTraversingFilter
* @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) {
public AnnotationTypeFilter(
Class<? extends Annotation> annotationType, boolean considerMetaAnnotations, boolean considerInterfaces) {
super(annotationType.isAnnotationPresent(Inherited.class), considerInterfaces);
this.annotationType = annotationType;
this.considerMetaAnnotations = considerMetaAnnotations;
......@@ -111,6 +113,10 @@ public class AnnotationTypeFilter extends AbstractTypeHierarchyTraversingFilter
return false;
}
else if (typeName.startsWith("java")) {
if (!this.annotationType.getName().startsWith("java")) {
// Standard Java classes don't have non-standard annotations on them.
return false;
}
try {
Class<?> clazz = ClassUtils.forName(typeName, getClass().getClassLoader());
return ((this.considerMetaAnnotations ? AnnotationUtils.getAnnotation(clazz, this.annotationType) :
......
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2018 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.
......@@ -49,7 +49,7 @@ public class AnnotationTypeFilterTests {
@Test
public void testInheritedAnnotationFromInterfaceDoesNotMatch() throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubClassOfSomeComponentInterface";
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeClassWithSomeComponentInterface";
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
......@@ -61,7 +61,7 @@ public class AnnotationTypeFilterTests {
@Test
public void testInheritedAnnotationFromBaseClassDoesMatch() throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubClassOfSomeComponent";
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubclassOfSomeComponent";
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
......@@ -94,22 +94,21 @@ public class AnnotationTypeFilterTests {
@Test
public void testMatchesInterfacesIfConfigured() throws Exception {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeComponentInterface";
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeClassWithSomeComponentInterface";
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()
@Inherited
private static @interface InheritedAnnotation {
private @interface InheritedAnnotation {
}
......@@ -119,21 +118,21 @@ public class AnnotationTypeFilterTests {
@InheritedAnnotation
private static interface SomeComponentInterface {
private interface SomeComponentInterface {
}
@SuppressWarnings("unused")
private static class SomeSubClassOfSomeComponentInterface implements SomeComponentInterface {
private static class SomeClassWithSomeComponentInterface implements Cloneable, SomeComponentInterface {
}
@SuppressWarnings("unused")
private static class SomeSubClassOfSomeComponent extends SomeComponent {
private static class SomeSubclassOfSomeComponent extends SomeComponent {
}
private static @interface NonInheritedAnnotation {
private @interface NonInheritedAnnotation {
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册