Browse Source

Avoid synthesizable check for common annotation types

This revision immediately returns false from isSynthesizable for java.lang.annotation types.

Issue: SPR-16933
pull/1916/head
Juergen Hoeller 7 years ago
parent
commit
a4be54d760
  1. 14
      spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java
  2. 28
      spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationAttributesReadingVisitor.java

14
spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

@ -1584,7 +1584,7 @@ public abstract class AnnotationUtils { @@ -1584,7 +1584,7 @@ public abstract class AnnotationUtils {
* @see #synthesizeAnnotation(Annotation, AnnotatedElement)
*/
public static <A extends Annotation> A synthesizeAnnotation(Class<A> annotationType) {
return synthesizeAnnotation(Collections.<String, Object> emptyMap(), annotationType, null);
return synthesizeAnnotation(Collections.emptyMap(), annotationType, null);
}
/**
@ -1603,9 +1603,7 @@ public abstract class AnnotationUtils { @@ -1603,9 +1603,7 @@ public abstract class AnnotationUtils {
* @see #synthesizeAnnotation(Annotation, AnnotatedElement)
* @see #synthesizeAnnotation(Map, Class, AnnotatedElement)
*/
static Annotation[] synthesizeAnnotationArray(
Annotation[] annotations, @Nullable Object annotatedElement) {
static Annotation[] synthesizeAnnotationArray(Annotation[] annotations, @Nullable Object annotatedElement) {
Annotation[] synthesized = (Annotation[]) Array.newInstance(
annotations.getClass().getComponentType(), annotations.length);
for (int i = 0; i < annotations.length; i++) {
@ -1633,7 +1631,9 @@ public abstract class AnnotationUtils { @@ -1633,7 +1631,9 @@ public abstract class AnnotationUtils {
*/
@SuppressWarnings("unchecked")
@Nullable
static <A extends Annotation> A[] synthesizeAnnotationArray(@Nullable Map<String, Object>[] maps, Class<A> annotationType) {
static <A extends Annotation> A[] synthesizeAnnotationArray(
@Nullable Map<String, Object>[] maps, Class<A> annotationType) {
if (maps == null) {
return null;
}
@ -1715,6 +1715,10 @@ public abstract class AnnotationUtils { @@ -1715,6 +1715,10 @@ public abstract class AnnotationUtils {
*/
@SuppressWarnings("unchecked")
private static boolean isSynthesizable(Class<? extends Annotation> annotationType) {
if (isInJavaLangAnnotationPackage(annotationType)) {
return false;
}
Boolean synthesizable = synthesizableCache.get(annotationType);
if (synthesizable != null) {
return synthesizable;

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -64,7 +64,7 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib @@ -64,7 +64,7 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib
public void visitEnd() {
super.visitEnd();
Class<?> annotationClass = this.attributes.annotationType();
Class<? extends Annotation> annotationClass = this.attributes.annotationType();
if (annotationClass != null) {
List<AnnotationAttributes> attributeList = this.attributesMap.get(this.annotationType);
if (attributeList == null) {
@ -73,20 +73,22 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib @@ -73,20 +73,22 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib
else {
attributeList.add(0, this.attributes);
}
Set<Annotation> visited = new LinkedHashSet<>();
Annotation[] metaAnnotations = AnnotationUtils.getAnnotations(annotationClass);
if (!ObjectUtils.isEmpty(metaAnnotations)) {
for (Annotation metaAnnotation : metaAnnotations) {
if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) {
recursivelyCollectMetaAnnotations(visited, metaAnnotation);
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotationClass.getName())) {
Set<Annotation> visited = new LinkedHashSet<>();
Annotation[] metaAnnotations = AnnotationUtils.getAnnotations(annotationClass);
if (!ObjectUtils.isEmpty(metaAnnotations)) {
for (Annotation metaAnnotation : metaAnnotations) {
if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) {
recursivelyCollectMetaAnnotations(visited, metaAnnotation);
}
}
}
Set<String> metaAnnotationTypeNames = new LinkedHashSet<>(visited.size());
for (Annotation ann : visited) {
metaAnnotationTypeNames.add(ann.annotationType().getName());
}
this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames);
}
Set<String> metaAnnotationTypeNames = new LinkedHashSet<>(visited.size());
for (Annotation ann : visited) {
metaAnnotationTypeNames.add(ann.annotationType().getName());
}
this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames);
}
}

Loading…
Cancel
Save