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

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -64,7 +64,7 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib
public void visitEnd() { public void visitEnd() {
super.visitEnd(); super.visitEnd();
Class<?> annotationClass = this.attributes.annotationType(); Class<? extends Annotation> annotationClass = this.attributes.annotationType();
if (annotationClass != null) { if (annotationClass != null) {
List<AnnotationAttributes> attributeList = this.attributesMap.get(this.annotationType); List<AnnotationAttributes> attributeList = this.attributesMap.get(this.annotationType);
if (attributeList == null) { if (attributeList == null) {
@ -73,6 +73,7 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib
else { else {
attributeList.add(0, this.attributes); attributeList.add(0, this.attributes);
} }
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotationClass.getName())) {
Set<Annotation> visited = new LinkedHashSet<>(); Set<Annotation> visited = new LinkedHashSet<>();
Annotation[] metaAnnotations = AnnotationUtils.getAnnotations(annotationClass); Annotation[] metaAnnotations = AnnotationUtils.getAnnotations(annotationClass);
if (!ObjectUtils.isEmpty(metaAnnotations)) { if (!ObjectUtils.isEmpty(metaAnnotations)) {
@ -89,6 +90,7 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib
this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames); this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames);
} }
} }
}
private void recursivelyCollectMetaAnnotations(Set<Annotation> visited, Annotation annotation) { private void recursivelyCollectMetaAnnotations(Set<Annotation> visited, Annotation annotation) {
Class<? extends Annotation> annotationType = annotation.annotationType(); Class<? extends Annotation> annotationType = annotation.annotationType();

Loading…
Cancel
Save