From df3761e3f68eea980a73f507e5d04ab3629b5c3d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 22 Jul 2011 09:34:42 +0000 Subject: [PATCH] introduced AnnotationUtils.getAnnotation(AnnotatedElement, annotationType) --- .../core/annotation/AnnotationUtils.java | 23 ++++++++++++++++++- .../SpringTransactionAnnotationParser.java | 14 +++-------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java b/org.springframework.core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java index b8e8ca73ba..ab18738245 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java +++ b/org.springframework.core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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. @@ -18,6 +18,7 @@ package org.springframework.core.annotation; import java.io.IOException; import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; @@ -60,6 +61,26 @@ public abstract class AnnotationUtils { private static final Map, Boolean> annotatedInterfaceCache = new WeakHashMap, Boolean>(); + /** + * Get a single {@link Annotation} of annotationType from the supplied + * Method, Constructor or Field. + * @param ae the Method, Constructor or Field to look for annotations on + * @param annotationType the annotation class to look for + * @return the annotations found + */ + public static T getAnnotation(AnnotatedElement ae, Class annotationType) { + T ann = ae.getAnnotation(annotationType); + if (ann == null) { + for (Annotation metaAnn : ae.getAnnotations()) { + ann = metaAnn.annotationType().getAnnotation(annotationType); + if (ann != null) { + break; + } + } + } + return ann; + } + /** * Get all {@link Annotation Annotations} from the supplied {@link Method}. *

Correctly handles bridge {@link Method Methods} generated by the compiler. diff --git a/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/SpringTransactionAnnotationParser.java b/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/SpringTransactionAnnotationParser.java index 6d18e3e0cb..3468c2dc28 100644 --- a/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/SpringTransactionAnnotationParser.java +++ b/org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/SpringTransactionAnnotationParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2011 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. @@ -17,10 +17,10 @@ package org.springframework.transaction.annotation; import java.io.Serializable; -import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.util.ArrayList; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.transaction.interceptor.NoRollbackRuleAttribute; import org.springframework.transaction.interceptor.RollbackRuleAttribute; import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute; @@ -35,15 +35,7 @@ import org.springframework.transaction.interceptor.TransactionAttribute; public class SpringTransactionAnnotationParser implements TransactionAnnotationParser, Serializable { public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) { - Transactional ann = ae.getAnnotation(Transactional.class); - if (ann == null) { - for (Annotation metaAnn : ae.getAnnotations()) { - ann = metaAnn.annotationType().getAnnotation(Transactional.class); - if (ann != null) { - break; - } - } - } + Transactional ann = AnnotationUtils.getAnnotation(ae, Transactional.class); if (ann != null) { return parseTransactionAnnotation(ann); }