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

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