Skip java.lang.annotations when reading metadata
Update `StandardAnnotationMetadata` and `AnnotationMetadataReadingVisitor` so that `java.lang.annotation` annotations are consistently skipped. Closes gh-22885
This commit is contained in:
parent
8a293f51a4
commit
1fa5937834
|
@ -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
|
|||
|
||||
@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));
|
||||
}
|
||||
|
|
|
@ -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<Set<String>> allMetaTypes = this.metaAnnotationMap.values();
|
||||
for (Set<String> metaTypes : allMetaTypes) {
|
||||
if (metaTypes.contains(metaAnnotationType)) {
|
||||
|
|
|
@ -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…
Reference in New Issue