Skip to content

Commit 8992f59

Browse files
committed
AnnotationUtils makes use of Java 8 getDeclaredAnnotation method
Issue: SPR-15287
1 parent 5237e47 commit 8992f59

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -488,15 +488,13 @@ public static <A extends Annotation> A findAnnotation(AnnotatedElement annotated
488488
private static <A extends Annotation> A findAnnotation(
489489
AnnotatedElement annotatedElement, Class<A> annotationType, Set<Annotation> visited) {
490490
try {
491-
Annotation[] anns = annotatedElement.getDeclaredAnnotations();
492-
for (Annotation ann : anns) {
493-
if (ann.annotationType() == annotationType) {
494-
return (A) ann;
495-
}
491+
A annotation = annotatedElement.getDeclaredAnnotation(annotationType);
492+
if (annotation != null) {
493+
return annotation;
496494
}
497-
for (Annotation ann : anns) {
495+
for (Annotation ann : annotatedElement.getDeclaredAnnotations()) {
498496
if (!isInJavaLangAnnotationPackage(ann) && visited.add(ann)) {
499-
A annotation = findAnnotation((AnnotatedElement) ann.annotationType(), annotationType, visited);
497+
annotation = findAnnotation((AnnotatedElement) ann.annotationType(), annotationType, visited);
500498
if (annotation != null) {
501499
return annotation;
502500
}
@@ -677,15 +675,13 @@ private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A>
677675
@SuppressWarnings("unchecked")
678676
private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType, Set<Annotation> visited) {
679677
try {
680-
Annotation[] anns = clazz.getDeclaredAnnotations();
681-
for (Annotation ann : anns) {
682-
if (ann.annotationType() == annotationType) {
683-
return (A) ann;
684-
}
678+
A annotation = clazz.getDeclaredAnnotation(annotationType);
679+
if (annotation != null) {
680+
return annotation;
685681
}
686-
for (Annotation ann : anns) {
682+
for (Annotation ann : clazz.getDeclaredAnnotations()) {
687683
if (!isInJavaLangAnnotationPackage(ann) && visited.add(ann)) {
688-
A annotation = findAnnotation(ann.annotationType(), annotationType, visited);
684+
annotation = findAnnotation(ann.annotationType(), annotationType, visited);
689685
if (annotation != null) {
690686
return annotation;
691687
}
@@ -803,16 +799,12 @@ public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> an
803799
Assert.notNull(annotationType, "Annotation type must not be null");
804800
Assert.notNull(clazz, "Class must not be null");
805801
try {
806-
for (Annotation ann : clazz.getDeclaredAnnotations()) {
807-
if (ann.annotationType() == annotationType) {
808-
return true;
809-
}
810-
}
802+
return (clazz.getDeclaredAnnotation(annotationType) != null);
811803
}
812804
catch (Throwable ex) {
813805
handleIntrospectionFailure(clazz, ex);
806+
return false;
814807
}
815-
return false;
816808
}
817809

818810
/**

0 commit comments

Comments
 (0)