Browse Source

introduced AnnotationUtils.getAnnotation(AnnotatedElement, annotationType)

pull/7/head
Juergen Hoeller 14 years ago
parent
commit
df3761e3f6
  1. 23
      org.springframework.core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java
  2. 14
      org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/SpringTransactionAnnotationParser.java

23
org.springframework.core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -60,6 +61,26 @@ public abstract class AnnotationUtils {
private static final Map<Class<?>, Boolean> annotatedInterfaceCache = new WeakHashMap<Class<?>, Boolean>();
/**
* Get a single {@link Annotation} of <code>annotationType</code> 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 extends Annotation> T getAnnotation(AnnotatedElement ae, Class<T> 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}.
* <p>Correctly handles bridge {@link Method Methods} generated by the compiler.

14
org.springframework.transaction/src/main/java/org/springframework/transaction/annotation/SpringTransactionAnnotationParser.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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; @@ -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);
}

Loading…
Cancel
Save