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

Issue: SPR-13621
This commit is contained in:
Juergen Hoeller
2015-11-05 12:26:54 +01:00
parent 1733d0111d
commit e35855f9b5
3 changed files with 33 additions and 23 deletions

View File

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

View File

@@ -38,6 +38,8 @@ import org.springframework.util.MultiValueMap;
*/ */
public class StandardAnnotationMetadata extends StandardClassMetadata implements AnnotationMetadata { public class StandardAnnotationMetadata extends StandardClassMetadata implements AnnotationMetadata {
private final Annotation[] annotations;
private final boolean nestedAnnotationsAsMap; private final boolean nestedAnnotationsAsMap;
@@ -63,6 +65,7 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
*/ */
public StandardAnnotationMetadata(Class<?> introspectedClass, boolean nestedAnnotationsAsMap) { public StandardAnnotationMetadata(Class<?> introspectedClass, boolean nestedAnnotationsAsMap) {
super(introspectedClass); super(introspectedClass);
this.annotations = introspectedClass.getAnnotations();
this.nestedAnnotationsAsMap = nestedAnnotationsAsMap; this.nestedAnnotationsAsMap = nestedAnnotationsAsMap;
} }
@@ -70,8 +73,7 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
@Override @Override
public Set<String> getAnnotationTypes() { public Set<String> getAnnotationTypes() {
Set<String> types = new LinkedHashSet<String>(); Set<String> types = new LinkedHashSet<String>();
Annotation[] anns = getIntrospectedClass().getAnnotations(); for (Annotation ann : this.annotations) {
for (Annotation ann : anns) {
types.add(ann.annotationType().getName()); types.add(ann.annotationType().getName());
} }
return types; return types;
@@ -79,13 +81,13 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
@Override @Override
public Set<String> getMetaAnnotationTypes(String annotationName) { public Set<String> getMetaAnnotationTypes(String annotationName) {
return AnnotatedElementUtils.getMetaAnnotationTypes(getIntrospectedClass(), annotationName); return (this.annotations.length > 0 ?
AnnotatedElementUtils.getMetaAnnotationTypes(getIntrospectedClass(), annotationName) : null);
} }
@Override @Override
public boolean hasAnnotation(String annotationName) { public boolean hasAnnotation(String annotationName) {
Annotation[] anns = getIntrospectedClass().getAnnotations(); for (Annotation ann : this.annotations) {
for (Annotation ann : anns) {
if (ann.annotationType().getName().equals(annotationName)) { if (ann.annotationType().getName().equals(annotationName)) {
return true; return true;
} }
@@ -95,23 +97,25 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
@Override @Override
public boolean hasMetaAnnotation(String annotationName) { public boolean hasMetaAnnotation(String annotationName) {
return AnnotatedElementUtils.hasMetaAnnotationTypes(getIntrospectedClass(), annotationName); return (this.annotations.length > 0 &&
AnnotatedElementUtils.hasMetaAnnotationTypes(getIntrospectedClass(), annotationName));
} }
@Override @Override
public boolean isAnnotated(String annotationName) { public boolean isAnnotated(String annotationName) {
return AnnotatedElementUtils.isAnnotated(getIntrospectedClass(), annotationName); return (this.annotations.length > 0 &&
AnnotatedElementUtils.isAnnotated(getIntrospectedClass(), annotationName));
} }
@Override @Override
public Map<String, Object> getAnnotationAttributes(String annotationName) { public Map<String, Object> getAnnotationAttributes(String annotationName) {
return this.getAnnotationAttributes(annotationName, false); return getAnnotationAttributes(annotationName, false);
} }
@Override @Override
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) { public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
return AnnotatedElementUtils.getMergedAnnotationAttributes(getIntrospectedClass(), return (this.annotations.length > 0 ? AnnotatedElementUtils.getMergedAnnotationAttributes(
annotationName, classValuesAsString, this.nestedAnnotationsAsMap); getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap) : null);
} }
@Override @Override
@@ -121,15 +125,16 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
@Override @Override
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) { public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {
return AnnotatedElementUtils.getAllAnnotationAttributes(getIntrospectedClass(), return (this.annotations.length > 0 ? AnnotatedElementUtils.getAllAnnotationAttributes(
annotationName, classValuesAsString, this.nestedAnnotationsAsMap); getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap) : null);
} }
@Override @Override
public boolean hasAnnotatedMethods(String annotationName) { public boolean hasAnnotatedMethods(String annotationName) {
Method[] methods = getIntrospectedClass().getDeclaredMethods(); Method[] methods = getIntrospectedClass().getDeclaredMethods();
for (Method method : methods) { for (Method method : methods) {
if (!method.isBridge() && AnnotatedElementUtils.isAnnotated(method, annotationName)) { if (!method.isBridge() && method.getAnnotations().length > 0 &&
AnnotatedElementUtils.isAnnotated(method, annotationName)) {
return true; return true;
} }
} }
@@ -141,7 +146,8 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
Method[] methods = getIntrospectedClass().getDeclaredMethods(); Method[] methods = getIntrospectedClass().getDeclaredMethods();
Set<MethodMetadata> annotatedMethods = new LinkedHashSet<MethodMetadata>(); Set<MethodMetadata> annotatedMethods = new LinkedHashSet<MethodMetadata>();
for (Method method : methods) { 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)); annotatedMethods.add(new StandardMethodMetadata(method, this.nestedAnnotationsAsMap));
} }
} }

View File

@@ -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"); * 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.
@@ -150,10 +150,12 @@ public class AnnotationTransactionAttributeSource extends AbstractFallbackTransa
* or {@code null} if none was found * or {@code null} if none was found
*/ */
protected TransactionAttribute determineTransactionAttribute(AnnotatedElement ae) { protected TransactionAttribute determineTransactionAttribute(AnnotatedElement ae) {
for (TransactionAnnotationParser annotationParser : this.annotationParsers) { if (ae.getAnnotations().length > 0) {
TransactionAttribute attr = annotationParser.parseTransactionAnnotation(ae); for (TransactionAnnotationParser annotationParser : this.annotationParsers) {
if (attr != null) { TransactionAttribute attr = annotationParser.parseTransactionAnnotation(ae);
return attr; if (attr != null) {
return attr;
}
} }
} }
return null; return null;