Skip to content

Commit 71c62c7

Browse files
committed
Skip java.lang.annotations when reading metadata
Update `StandardAnnotationMetadata` and `AnnotationMetadataReadingVisitor` so that `java.lang.annotation` annotations are consistently skipped. Closes spring-projectsgh-22885
1 parent f4519b4 commit 71c62c7

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,28 @@ public StandardAnnotationMetadata(Class<?> introspectedClass, boolean nestedAnno
7777
public Set<String> getAnnotationTypes() {
7878
Set<String> types = new LinkedHashSet<>();
7979
for (Annotation ann : this.annotations) {
80-
types.add(ann.annotationType().getName());
80+
if (!AnnotationUtils.isInJavaLangAnnotationPackage(ann.annotationType().getName())) {
81+
types.add(ann.annotationType().getName());
82+
}
8183
}
8284
return types;
8385
}
8486

8587
@Override
8688
public Set<String> getMetaAnnotationTypes(String annotationName) {
89+
if (AnnotationUtils.isInJavaLangAnnotationPackage(annotationName)) {
90+
return Collections.emptySet();
91+
}
8792
return (this.annotations.length > 0 ?
8893
AnnotatedElementUtils.getMetaAnnotationTypes(getIntrospectedClass(), annotationName) :
8994
Collections.emptySet());
9095
}
9196

9297
@Override
9398
public boolean hasAnnotation(String annotationName) {
99+
if (AnnotationUtils.isInJavaLangAnnotationPackage(annotationName)) {
100+
return false;
101+
}
94102
for (Annotation ann : this.annotations) {
95103
if (ann.annotationType().getName().equals(annotationName)) {
96104
return true;
@@ -101,6 +109,9 @@ public boolean hasAnnotation(String annotationName) {
101109

102110
@Override
103111
public boolean hasMetaAnnotation(String annotationName) {
112+
if (AnnotationUtils.isInJavaLangAnnotationPackage(annotationName)) {
113+
return false;
114+
}
104115
return (this.annotations.length > 0 &&
105116
AnnotatedElementUtils.hasMetaAnnotationTypes(getIntrospectedClass(), annotationName));
106117
}

spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
8989
return null;
9090
}
9191
String className = Type.getType(desc).getClassName();
92+
if (AnnotationUtils.isInJavaLangAnnotationPackage(className)) {
93+
return null;
94+
}
9295
this.annotationSet.add(className);
9396
return new AnnotationAttributesReadingVisitor(
9497
className, this.attributesMap, this.metaAnnotationMap, this.classLoader);
@@ -108,11 +111,17 @@ public Set<String> getMetaAnnotationTypes(String annotationName) {
108111

109112
@Override
110113
public boolean hasAnnotation(String annotationName) {
114+
if (AnnotationUtils.isInJavaLangAnnotationPackage(annotationName)) {
115+
return false;
116+
}
111117
return this.annotationSet.contains(annotationName);
112118
}
113119

114120
@Override
115121
public boolean hasMetaAnnotation(String metaAnnotationType) {
122+
if (AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotationType)) {
123+
return false;
124+
}
116125
Collection<Set<String>> allMetaTypes = this.metaAnnotationMap.values();
117126
for (Set<String> metaTypes : allMetaTypes) {
118127
if (metaTypes.contains(metaAnnotationType)) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ private void doTestMetadataForAnnotationClass(AnnotationMetadata metadata) {
160160
assertThat(metadata.isAnnotated(Documented.class.getName()), is(false));
161161
assertThat(metadata.isAnnotated(Scope.class.getName()), is(false));
162162
assertThat(metadata.isAnnotated(SpecialAttr.class.getName()), is(false));
163-
assertThat(metadata.hasAnnotation(Documented.class.getName()), is(true));
163+
assertThat(metadata.hasAnnotation(Documented.class.getName()), is(false));
164164
assertThat(metadata.hasAnnotation(Scope.class.getName()), is(false));
165165
assertThat(metadata.hasAnnotation(SpecialAttr.class.getName()), is(false));
166-
assertThat(metadata.getAnnotationTypes().size(), is(4));
166+
assertThat(metadata.getAnnotationTypes().size(), is(1));
167167
}
168168

169169
/**

0 commit comments

Comments
 (0)