Browse Source

Skip java.lang.annotations when reading metadata

Update `StandardAnnotationMetadata` and `AnnotationMetadataReadingVisitor`
so that `java.lang.annotation` annotations are consistently skipped.

Closes gh-22885
pull/25019/head
Phillip Webb 6 years ago committed by Juergen Hoeller
parent
commit
1fa5937834
  1. 13
      spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java
  2. 9
      spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java
  3. 4
      spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java

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

@ -77,13 +77,18 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements @@ -77,13 +77,18 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
public Set<String> getAnnotationTypes() {
Set<String> 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<String> 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 @@ -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 @@ -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));
}

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

@ -89,6 +89,9 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito @@ -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 @@ -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<Set<String>> allMetaTypes = this.metaAnnotationMap.values();
for (Set<String> metaTypes : allMetaTypes) {
if (metaTypes.contains(metaAnnotationType)) {

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

@ -160,10 +160,10 @@ public class AnnotationMetadataTests { @@ -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));
}
/**

Loading…
Cancel
Save