Skip to content

Commit 22a8a66

Browse files
committed
AnnotationTypeFilter assumes no custom annotations on common Java types
Issue: SPR-16667
1 parent 78681c6 commit 22a8a66

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

spring-core/src/main/java/org/springframework/core/type/filter/AnnotationTypeFilter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,7 +71,9 @@ public AnnotationTypeFilter(Class<? extends Annotation> annotationType, boolean
7171
* @param considerMetaAnnotations whether to also match on meta-annotations
7272
* @param considerInterfaces whether to also match interfaces
7373
*/
74-
public AnnotationTypeFilter(Class<? extends Annotation> annotationType, boolean considerMetaAnnotations, boolean considerInterfaces) {
74+
public AnnotationTypeFilter(
75+
Class<? extends Annotation> annotationType, boolean considerMetaAnnotations, boolean considerInterfaces) {
76+
7577
super(annotationType.isAnnotationPresent(Inherited.class), considerInterfaces);
7678
this.annotationType = annotationType;
7779
this.considerMetaAnnotations = considerMetaAnnotations;
@@ -111,6 +113,10 @@ protected Boolean hasAnnotation(String typeName) {
111113
return false;
112114
}
113115
else if (typeName.startsWith("java")) {
116+
if (!this.annotationType.getName().startsWith("java")) {
117+
// Standard Java classes don't have non-standard annotations on them.
118+
return false;
119+
}
114120
try {
115121
Class<?> clazz = ClassUtils.forName(typeName, getClass().getClassLoader());
116122
return ((this.considerMetaAnnotations ? AnnotationUtils.getAnnotation(clazz, this.annotationType) :

spring-core/src/test/java/org/springframework/core/type/AnnotationTypeFilterTests.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,7 +49,7 @@ public void testDirectAnnotationMatch() throws Exception {
4949
@Test
5050
public void testInheritedAnnotationFromInterfaceDoesNotMatch() throws Exception {
5151
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
52-
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubClassOfSomeComponentInterface";
52+
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeClassWithSomeComponentInterface";
5353
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
5454

5555
AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
@@ -61,7 +61,7 @@ public void testInheritedAnnotationFromInterfaceDoesNotMatch() throws Exception
6161
@Test
6262
public void testInheritedAnnotationFromBaseClassDoesMatch() throws Exception {
6363
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
64-
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubClassOfSomeComponent";
64+
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubclassOfSomeComponent";
6565
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
6666

6767
AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
@@ -94,22 +94,21 @@ public void testNonAnnotatedClassDoesntMatch() throws Exception {
9494

9595
@Test
9696
public void testMatchesInterfacesIfConfigured() throws Exception {
97-
9897
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
99-
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeComponentInterface";
98+
String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeClassWithSomeComponentInterface";
10099
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
101100

102101
AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class, false, true);
103-
104102
assertTrue(filter.match(metadataReader, metadataReaderFactory));
105103
ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
106104
}
107105

106+
108107
// We must use a standalone set of types to ensure that no one else is loading them
109108
// and interfering with ClassloadingAssertions.assertClassNotLoaded()
110109

111110
@Inherited
112-
private static @interface InheritedAnnotation {
111+
private @interface InheritedAnnotation {
113112
}
114113

115114

@@ -119,21 +118,21 @@ private static class SomeComponent {
119118

120119

121120
@InheritedAnnotation
122-
private static interface SomeComponentInterface {
121+
private interface SomeComponentInterface {
123122
}
124123

125124

126125
@SuppressWarnings("unused")
127-
private static class SomeSubClassOfSomeComponentInterface implements SomeComponentInterface {
126+
private static class SomeClassWithSomeComponentInterface implements Cloneable, SomeComponentInterface {
128127
}
129128

130129

131130
@SuppressWarnings("unused")
132-
private static class SomeSubClassOfSomeComponent extends SomeComponent {
131+
private static class SomeSubclassOfSomeComponent extends SomeComponent {
133132
}
134133

135134

136-
private static @interface NonInheritedAnnotation {
135+
private @interface NonInheritedAnnotation {
137136
}
138137

139138

0 commit comments

Comments
 (0)