Avoid expensive annotation retrieval algorithm if no annotations present in the first place

Issue: SPR-13621
master
Juergen Hoeller 9 years ago
parent 1733d0111d
commit e35855f9b5
  1. 10
      spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java
  2. 34
      spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java
  3. 12
      spring-tx/src/main/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.java

@ -441,10 +441,12 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
}
private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) {
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type);
if (attributes != null) {
return attributes;
if (ao.getAnnotations().length > 0) {
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type);
if (attributes != null) {
return attributes;
}
}
}
return null;

@ -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<String> getAnnotationTypes() {
Set<String> types = new LinkedHashSet<String>();
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<String> 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<String, Object> getAnnotationAttributes(String annotationName) {
return this.getAnnotationAttributes(annotationName, false);
return getAnnotationAttributes(annotationName, false);
}
@Override
public Map<String, Object> 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<String, Object> 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<MethodMetadata> annotatedMethods = new LinkedHashSet<MethodMetadata>();
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));
}
}

@ -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;

Loading…
Cancel
Save