From e35855f9b52ea13a2ce4fd321e4189a0a7f3cfb3 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 5 Nov 2015 12:26:54 +0100 Subject: [PATCH] Avoid expensive annotation retrieval algorithm if no annotations present in the first place Issue: SPR-13621 --- .../AutowiredAnnotationBeanPostProcessor.java | 10 +++--- .../core/type/StandardAnnotationMetadata.java | 34 +++++++++++-------- .../AnnotationTransactionAttributeSource.java | 12 ++++--- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index 2ac33ba559..20171c0fdf 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -441,10 +441,12 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean } private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) { - for (Class type : this.autowiredAnnotationTypes) { - AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type); - if (attributes != null) { - return attributes; + if (ao.getAnnotations().length > 0) { + for (Class type : this.autowiredAnnotationTypes) { + AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type); + if (attributes != null) { + return attributes; + } } } return null; diff --git a/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java b/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java index 6e135937fa..b389b1cf4c 100644 --- a/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java +++ b/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java @@ -38,6 +38,8 @@ import org.springframework.util.MultiValueMap; */ public class StandardAnnotationMetadata extends StandardClassMetadata implements AnnotationMetadata { + private final Annotation[] annotations; + private final boolean nestedAnnotationsAsMap; @@ -63,6 +65,7 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements */ public StandardAnnotationMetadata(Class introspectedClass, boolean nestedAnnotationsAsMap) { super(introspectedClass); + this.annotations = introspectedClass.getAnnotations(); this.nestedAnnotationsAsMap = nestedAnnotationsAsMap; } @@ -70,8 +73,7 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements @Override public Set getAnnotationTypes() { Set types = new LinkedHashSet(); - Annotation[] anns = getIntrospectedClass().getAnnotations(); - for (Annotation ann : anns) { + for (Annotation ann : this.annotations) { types.add(ann.annotationType().getName()); } return types; @@ -79,13 +81,13 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements @Override public Set getMetaAnnotationTypes(String annotationName) { - return AnnotatedElementUtils.getMetaAnnotationTypes(getIntrospectedClass(), annotationName); + return (this.annotations.length > 0 ? + AnnotatedElementUtils.getMetaAnnotationTypes(getIntrospectedClass(), annotationName) : null); } @Override public boolean hasAnnotation(String annotationName) { - Annotation[] anns = getIntrospectedClass().getAnnotations(); - for (Annotation ann : anns) { + for (Annotation ann : this.annotations) { if (ann.annotationType().getName().equals(annotationName)) { return true; } @@ -95,23 +97,25 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements @Override public boolean hasMetaAnnotation(String annotationName) { - return AnnotatedElementUtils.hasMetaAnnotationTypes(getIntrospectedClass(), annotationName); + return (this.annotations.length > 0 && + AnnotatedElementUtils.hasMetaAnnotationTypes(getIntrospectedClass(), annotationName)); } @Override public boolean isAnnotated(String annotationName) { - return AnnotatedElementUtils.isAnnotated(getIntrospectedClass(), annotationName); + return (this.annotations.length > 0 && + AnnotatedElementUtils.isAnnotated(getIntrospectedClass(), annotationName)); } @Override public Map getAnnotationAttributes(String annotationName) { - return this.getAnnotationAttributes(annotationName, false); + return getAnnotationAttributes(annotationName, false); } @Override public Map getAnnotationAttributes(String annotationName, boolean classValuesAsString) { - return AnnotatedElementUtils.getMergedAnnotationAttributes(getIntrospectedClass(), - annotationName, classValuesAsString, this.nestedAnnotationsAsMap); + return (this.annotations.length > 0 ? AnnotatedElementUtils.getMergedAnnotationAttributes( + getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap) : null); } @Override @@ -121,15 +125,16 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements @Override public MultiValueMap getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) { - return AnnotatedElementUtils.getAllAnnotationAttributes(getIntrospectedClass(), - annotationName, classValuesAsString, this.nestedAnnotationsAsMap); + return (this.annotations.length > 0 ? AnnotatedElementUtils.getAllAnnotationAttributes( + getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap) : null); } @Override public boolean hasAnnotatedMethods(String annotationName) { Method[] methods = getIntrospectedClass().getDeclaredMethods(); for (Method method : methods) { - if (!method.isBridge() && AnnotatedElementUtils.isAnnotated(method, annotationName)) { + if (!method.isBridge() && method.getAnnotations().length > 0 && + AnnotatedElementUtils.isAnnotated(method, annotationName)) { return true; } } @@ -141,7 +146,8 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements Method[] methods = getIntrospectedClass().getDeclaredMethods(); Set annotatedMethods = new LinkedHashSet(); for (Method method : methods) { - if (!method.isBridge() && AnnotatedElementUtils.isAnnotated(method, annotationName)) { + if (!method.isBridge() && method.getAnnotations().length > 0 && + AnnotatedElementUtils.isAnnotated(method, annotationName)) { annotatedMethods.add(new StandardMethodMetadata(method, this.nestedAnnotationsAsMap)); } } diff --git a/spring-tx/src/main/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java b/spring-tx/src/main/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java index 30cd95157d..476d6b14f5 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java +++ b/spring-tx/src/main/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 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. @@ -150,10 +150,12 @@ public class AnnotationTransactionAttributeSource extends AbstractFallbackTransa * or {@code null} if none was found */ protected TransactionAttribute determineTransactionAttribute(AnnotatedElement ae) { - for (TransactionAnnotationParser annotationParser : this.annotationParsers) { - TransactionAttribute attr = annotationParser.parseTransactionAnnotation(ae); - if (attr != null) { - return attr; + if (ae.getAnnotations().length > 0) { + for (TransactionAnnotationParser annotationParser : this.annotationParsers) { + TransactionAttribute attr = annotationParser.parseTransactionAnnotation(ae); + if (attr != null) { + return attr; + } } } return null;