KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > annotation > AnnotationUtils


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core.annotation;
18
19 import java.lang.annotation.Annotation JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21
22 import org.springframework.core.BridgeMethodResolver;
23
24 /**
25  * General utility methods for working with annotations, handling bridge methods
26  * (which the compiler generates for generic declarations) as well as super
27  * methods (for optional "annotation inheritance"). Note that none of this is
28  * provided by the JDK's introspection facilities themselves.
29  *
30  * <p>As a general rule for runtime-retained annotations (e.g. for transaction
31  * control, authorization or service exposure), always use the lookup methods
32  * on this class instead of the plain annotation lookup methods in the JDK.
33  * You can still explicitly choose between lookup on the given class level only
34  * ({@link #getAnnotation}) and lookup in the entire inheritance hierarchy of
35  * the given method ({@link #findAnnotation}).
36  *
37  * @author Rod Johnson
38  * @author Rob Harrop
39  * @author Juergen Hoeller
40  * @since 2.0
41  * @see java.lang.reflect.Method#getAnnotations()
42  * @see java.lang.reflect.Method#getAnnotation(Class)
43  */

44 public abstract class AnnotationUtils {
45
46     /**
47      * Get all {@link Annotation Annotations} from the supplied {@link Method}.
48      * <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
49      * @param method the method to look for annotations on
50      * @return the annotations found
51      * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)
52      */

53     public static Annotation JavaDoc[] getAnnotations(Method JavaDoc method) {
54         return BridgeMethodResolver.findBridgedMethod(method).getAnnotations();
55     }
56
57     /**
58      * Get a single {@link Annotation} of <code>annotationType</code> from the
59      * supplied {@link Method}.
60      * <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
61      * @param method the method to look for annotations on
62      * @param annotationType the annotation class to look for
63      * @return the annotations found
64      * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)
65      */

66     public static <A extends Annotation JavaDoc> A getAnnotation(Method JavaDoc method, Class JavaDoc<A> annotationType) {
67         return BridgeMethodResolver.findBridgedMethod(method).getAnnotation(annotationType);
68     }
69
70     /**
71      * Get a single {@link Annotation} of <code>annotationType</code> from the
72      * supplied {@link Method}, traversing its super methods if no annotation
73      * can be found on the given method.
74      * <p>Annotations on methods are not inherited by default, so we need to
75      * handle this explicitly.
76      * @param method the method to look for annotations on
77      * @param annotationType the annotation class to look for
78      * @return the annotation of the given type found, or <code>null</code>
79      */

80     public static <A extends Annotation JavaDoc> A findAnnotation(Method JavaDoc method, Class JavaDoc<A> annotationType) {
81         if (!annotationType.isAnnotation()) {
82             throw new IllegalArgumentException JavaDoc(annotationType + " is not an annotation");
83         }
84         A annotation = getAnnotation(method, annotationType);
85         Class JavaDoc cl = method.getDeclaringClass();
86         while (annotation == null) {
87             cl = cl.getSuperclass();
88             if (cl == null || cl.equals(Object JavaDoc.class)) {
89                 break;
90             }
91             try {
92                 method = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());
93                 annotation = getAnnotation(method, annotationType);
94             }
95             catch (NoSuchMethodException JavaDoc ex) {
96                 // We're done...
97
}
98         }
99         return annotation;
100     }
101
102 }
103
Popular Tags