From 1fa5937834b271b9b149654ba5b42b6c333bdd8f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 3 May 2019 19:59:08 -0700 Subject: [PATCH] Skip java.lang.annotations when reading metadata Update `StandardAnnotationMetadata` and `AnnotationMetadataReadingVisitor` so that `java.lang.annotation` annotations are consistently skipped. Closes gh-22885 --- .../core/type/StandardAnnotationMetadata.java | 13 ++++++++++++- .../AnnotationMetadataReadingVisitor.java | 9 +++++++++ .../core/type/AnnotationMetadataTests.java | 4 ++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java b/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java index 4f23470d96..8cbf61d75b 100644 --- a/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java +++ b/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java @@ -77,13 +77,18 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements public Set getAnnotationTypes() { Set types = new LinkedHashSet<>(); for (Annotation ann : this.annotations) { - types.add(ann.annotationType().getName()); + if (!AnnotationUtils.isInJavaLangAnnotationPackage(ann.annotationType().getName())) { + types.add(ann.annotationType().getName()); + } } return types; } @Override public Set getMetaAnnotationTypes(String annotationName) { + if (AnnotationUtils.isInJavaLangAnnotationPackage(annotationName)) { + return Collections.emptySet(); + } return (this.annotations.length > 0 ? AnnotatedElementUtils.getMetaAnnotationTypes(getIntrospectedClass(), annotationName) : Collections.emptySet()); @@ -91,6 +96,9 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements @Override public boolean hasAnnotation(String annotationName) { + if (AnnotationUtils.isInJavaLangAnnotationPackage(annotationName)) { + return false; + } for (Annotation ann : this.annotations) { if (ann.annotationType().getName().equals(annotationName)) { return true; @@ -101,6 +109,9 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements @Override public boolean hasMetaAnnotation(String annotationName) { + if (AnnotationUtils.isInJavaLangAnnotationPackage(annotationName)) { + return false; + } return (this.annotations.length > 0 && AnnotatedElementUtils.hasMetaAnnotationTypes(getIntrospectedClass(), annotationName)); } diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java index 03a96cf431..a32628088d 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java @@ -89,6 +89,9 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito return null; } String className = Type.getType(desc).getClassName(); + if (AnnotationUtils.isInJavaLangAnnotationPackage(className)) { + return null; + } this.annotationSet.add(className); return new AnnotationAttributesReadingVisitor( className, this.attributesMap, this.metaAnnotationMap, this.classLoader); @@ -108,11 +111,17 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito @Override public boolean hasAnnotation(String annotationName) { + if (AnnotationUtils.isInJavaLangAnnotationPackage(annotationName)) { + return false; + } return this.annotationSet.contains(annotationName); } @Override public boolean hasMetaAnnotation(String metaAnnotationType) { + if (AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotationType)) { + return false; + } Collection> allMetaTypes = this.metaAnnotationMap.values(); for (Set metaTypes : allMetaTypes) { if (metaTypes.contains(metaAnnotationType)) { diff --git a/spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java b/spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java index ae44543137..5cbd75bbee 100644 --- a/spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java +++ b/spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java @@ -160,10 +160,10 @@ public class AnnotationMetadataTests { assertThat(metadata.isAnnotated(Documented.class.getName()), is(false)); assertThat(metadata.isAnnotated(Scope.class.getName()), is(false)); assertThat(metadata.isAnnotated(SpecialAttr.class.getName()), is(false)); - assertThat(metadata.hasAnnotation(Documented.class.getName()), is(true)); + assertThat(metadata.hasAnnotation(Documented.class.getName()), is(false)); assertThat(metadata.hasAnnotation(Scope.class.getName()), is(false)); assertThat(metadata.hasAnnotation(SpecialAttr.class.getName()), is(false)); - assertThat(metadata.getAnnotationTypes().size(), is(4)); + assertThat(metadata.getAnnotationTypes().size(), is(1)); } /**