From a4be54d760edae45b80aa8c639a2adb4626f0576 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 31 Jul 2018 21:37:34 +0200 Subject: [PATCH] Avoid synthesizable check for common annotation types This revision immediately returns false from isSynthesizable for java.lang.annotation types. Issue: SPR-16933 --- .../core/annotation/AnnotationUtils.java | 14 ++++++---- .../AnnotationAttributesReadingVisitor.java | 28 ++++++++++--------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java index 12693854498..2a4569c5be9 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java @@ -1584,7 +1584,7 @@ public abstract class AnnotationUtils { * @see #synthesizeAnnotation(Annotation, AnnotatedElement) */ public static A synthesizeAnnotation(Class annotationType) { - return synthesizeAnnotation(Collections. emptyMap(), annotationType, null); + return synthesizeAnnotation(Collections.emptyMap(), annotationType, null); } /** @@ -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 { */ @SuppressWarnings("unchecked") @Nullable - static A[] synthesizeAnnotationArray(@Nullable Map[] maps, Class annotationType) { + static A[] synthesizeAnnotationArray( + @Nullable Map[] maps, Class annotationType) { + if (maps == null) { return null; } @@ -1715,6 +1715,10 @@ public abstract class AnnotationUtils { */ @SuppressWarnings("unchecked") private static boolean isSynthesizable(Class annotationType) { + if (isInJavaLangAnnotationPackage(annotationType)) { + return false; + } + Boolean synthesizable = synthesizableCache.get(annotationType); if (synthesizable != null) { return synthesizable; diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationAttributesReadingVisitor.java b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationAttributesReadingVisitor.java index e79d19145d5..1e8561afbb0 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationAttributesReadingVisitor.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -64,7 +64,7 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib public void visitEnd() { super.visitEnd(); - Class annotationClass = this.attributes.annotationType(); + Class annotationClass = this.attributes.annotationType(); if (annotationClass != null) { List attributeList = this.attributesMap.get(this.annotationType); if (attributeList == null) { @@ -73,20 +73,22 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib else { attributeList.add(0, this.attributes); } - Set 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 visited = new LinkedHashSet<>(); + Annotation[] metaAnnotations = AnnotationUtils.getAnnotations(annotationClass); + if (!ObjectUtils.isEmpty(metaAnnotations)) { + for (Annotation metaAnnotation : metaAnnotations) { + if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) { + recursivelyCollectMetaAnnotations(visited, metaAnnotation); + } } } + Set metaAnnotationTypeNames = new LinkedHashSet<>(visited.size()); + for (Annotation ann : visited) { + metaAnnotationTypeNames.add(ann.annotationType().getName()); + } + this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames); } - Set metaAnnotationTypeNames = new LinkedHashSet<>(visited.size()); - for (Annotation ann : visited) { - metaAnnotationTypeNames.add(ann.annotationType().getName()); - } - this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames); } }