Browse Source

AnnotationUtils makes use of Java 8 getDeclaredAnnotation method

Issue: SPR-15287
pull/828/merge
Juergen Hoeller 9 years ago
parent
commit
8992f5924f
  1. 34
      spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 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.
@ -488,15 +488,13 @@ public abstract class AnnotationUtils {
private static <A extends Annotation> A findAnnotation( private static <A extends Annotation> A findAnnotation(
AnnotatedElement annotatedElement, Class<A> annotationType, Set<Annotation> visited) { AnnotatedElement annotatedElement, Class<A> annotationType, Set<Annotation> visited) {
try { try {
Annotation[] anns = annotatedElement.getDeclaredAnnotations(); A annotation = annotatedElement.getDeclaredAnnotation(annotationType);
for (Annotation ann : anns) { if (annotation != null) {
if (ann.annotationType() == annotationType) { return annotation;
return (A) ann;
}
} }
for (Annotation ann : anns) { for (Annotation ann : annotatedElement.getDeclaredAnnotations()) {
if (!isInJavaLangAnnotationPackage(ann) && visited.add(ann)) { if (!isInJavaLangAnnotationPackage(ann) && visited.add(ann)) {
A annotation = findAnnotation((AnnotatedElement) ann.annotationType(), annotationType, visited); annotation = findAnnotation((AnnotatedElement) ann.annotationType(), annotationType, visited);
if (annotation != null) { if (annotation != null) {
return annotation; return annotation;
} }
@ -677,15 +675,13 @@ public abstract class AnnotationUtils {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType, Set<Annotation> visited) { private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType, Set<Annotation> visited) {
try { try {
Annotation[] anns = clazz.getDeclaredAnnotations(); A annotation = clazz.getDeclaredAnnotation(annotationType);
for (Annotation ann : anns) { if (annotation != null) {
if (ann.annotationType() == annotationType) { return annotation;
return (A) ann;
}
} }
for (Annotation ann : anns) { for (Annotation ann : clazz.getDeclaredAnnotations()) {
if (!isInJavaLangAnnotationPackage(ann) && visited.add(ann)) { if (!isInJavaLangAnnotationPackage(ann) && visited.add(ann)) {
A annotation = findAnnotation(ann.annotationType(), annotationType, visited); annotation = findAnnotation(ann.annotationType(), annotationType, visited);
if (annotation != null) { if (annotation != null) {
return annotation; return annotation;
} }
@ -803,16 +799,12 @@ public abstract class AnnotationUtils {
Assert.notNull(annotationType, "Annotation type must not be null"); Assert.notNull(annotationType, "Annotation type must not be null");
Assert.notNull(clazz, "Class must not be null"); Assert.notNull(clazz, "Class must not be null");
try { try {
for (Annotation ann : clazz.getDeclaredAnnotations()) { return (clazz.getDeclaredAnnotation(annotationType) != null);
if (ann.annotationType() == annotationType) {
return true;
}
}
} }
catch (Throwable ex) { catch (Throwable ex) {
handleIntrospectionFailure(clazz, ex); handleIntrospectionFailure(clazz, ex);
return false;
} }
return false;
} }
/** /**

Loading…
Cancel
Save