KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > backport175 > Annotations


1 /*******************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://backport175.codehaus.org *
4  * --------------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of Apache License Version 2.0 *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  *******************************************************************************************/

8 package com.tc.backport175;
9
10 import com.tc.backport175.bytecode.AnnotationReader;
11
12 import java.lang.reflect.Constructor JavaDoc;
13 import java.lang.reflect.Field JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 /**
19  * Helper class for reader retrieval of strongly typed JavaDoc annotations (as well as regular Java 5 {@link
20  * java.lang.reader.RetentionPolicy.RUNTIME} annotations when running Java 1.5.x).
21  *
22  * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bonér </a>
23  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
24  */

25 public final class Annotations {
26
27     /**
28      * Checks if an annotation is present at a specific class.
29      *
30      * @param annotationType the annotation type
31      * @param target the annotated type
32      * @return true if the annotation is present else false
33      */

34     public static boolean isAnnotationPresent(final Class JavaDoc annotationType, final Class JavaDoc target) {
35         boolean isPresent = AnnotationReader.getReaderFor(target).isAnnotationPresent(getAnnnotationName(annotationType));
36         if (!isPresent && isInherited(annotationType)) {
37             if (target.getSuperclass() == null) {
38                 return isPresent;
39             } else {
40                 return isAnnotationPresent(annotationType, target.getSuperclass());
41             }
42         }
43         return isPresent;
44     }
45
46     /**
47      * Return all the annotations for a specific class.
48      *
49      * @param target the java.lang.Class object to find the annotations on.
50      * @return an array with the annotations
51      */

52     public static Annotation[] getAnnotations(final Class JavaDoc target) {
53         Annotation[] declaredAnnotations = AnnotationReader.getReaderFor(target).getAnnotations();
54         if (target.getSuperclass() == null) {
55             return declaredAnnotations;
56         } else {
57             List JavaDoc annotations = new ArrayList JavaDoc(declaredAnnotations.length);
58             Annotation[] parents = getAnnotations(target.getSuperclass());
59             for (int i = 0; i < parents.length; i++) {
60                 if (isInherited(parents[i].annotationType())) {
61                     annotations.add(parents[i]);
62                 }
63             }
64             for (int i = 0; i < declaredAnnotations.length; i++) {
65                 annotations.add(declaredAnnotations[i]);
66             }
67             return (Annotation[])annotations.toArray(new Annotation[]{});
68         }
69     }
70
71     /**
72      * Return the annotation with a specific name for a specific class.
73      *
74      * @param annotationType the annotation class
75      * @param target the java.lang.Class object to find the annotation on.
76      * @return the annotation or null
77      */

78     public static Annotation getAnnotation(final Class JavaDoc annotationType, final Class JavaDoc target) {
79         final AnnotationReader reader = AnnotationReader.getReaderFor(target);
80         Annotation annotation = reader.getAnnotation(getAnnnotationName(annotationType));
81         if (annotation == null && isInherited(annotationType)) {
82             if (target.getSuperclass() == null) {
83                 return annotation;
84             } else {
85                 return getAnnotation(annotationType, target.getSuperclass());
86             }
87         }
88         return annotation;
89     }
90
91     /**
92      * Checks if an annotation is present at a specific method.
93      *
94      * @param annotationType the annotation type
95      * @param method the annotated type
96      * @return true if the annotation is present else false
97      */

98     public static boolean isAnnotationPresent(final Class JavaDoc annotationType, final Method JavaDoc method) {
99         final AnnotationReader reader = AnnotationReader.getReaderFor(method.getDeclaringClass());
100         return reader.isAnnotationPresent(getAnnnotationName(annotationType), method);
101     }
102
103     /**
104      * Return all the annotations for a specific method.
105      *
106      * @param method the java.lang.reflect.Method object to find the annotations on.
107      * @return an array with the annotations
108      */

109     public static Annotation[] getAnnotations(final Method JavaDoc method) {
110         return AnnotationReader.getReaderFor(method.getDeclaringClass()).getAnnotations(method);
111     }
112
113     /**
114      * Return the annotation with a specific name for a specific method.
115      *
116      * @param annotationType the annotation class
117      * @param method the java.lang.refect.Method object to find the annotation on.
118      * @return the annotation or null
119      */

120     public static Annotation getAnnotation(final Class JavaDoc annotationType, final Method JavaDoc method) {
121         final AnnotationReader reader = AnnotationReader.getReaderFor(method.getDeclaringClass());
122         return reader.getAnnotation(getAnnnotationName(annotationType), method);
123     }
124
125     /**
126      * Checks if an annotation is present at a specific method.
127      *
128      * @param annotationType the annotation type
129      * @param constructor the annotated type
130      * @return true if the annotation is present else false
131      */

132     public static boolean isAnnotationPresent(final Class JavaDoc annotationType, final Constructor JavaDoc constructor) {
133         final AnnotationReader reader = AnnotationReader.getReaderFor(constructor.getDeclaringClass());
134         return reader.isAnnotationPresent(getAnnnotationName(annotationType), constructor);
135     }
136
137     /**
138      * Return all the annotations for a specific constructor.
139      *
140      * @param constructor the java.lang.reflect.Constructor object to find the annotations on.
141      * @return an array with the annotations
142      */

143     public static Annotation[] getAnnotations(final Constructor JavaDoc constructor) {
144         return AnnotationReader.getReaderFor(constructor.getDeclaringClass()).getAnnotations(constructor);
145     }
146
147     /**
148      * Return the annotation with a specific name for a specific constructor.
149      *
150      * @param annotationType the annotation class
151      * @param constructor the java.lang.refect.Constructor object to find the annotation on.
152      * @return the annotation or null
153      */

154     public static Annotation getAnnotation(final Class JavaDoc annotationType, final Constructor JavaDoc constructor) {
155         final AnnotationReader reader = AnnotationReader.getReaderFor(constructor.getDeclaringClass());
156         return reader.getAnnotation(getAnnnotationName(annotationType), constructor);
157     }
158
159     /**
160      * Checks if an annotation is present at a specific field.
161      *
162      * @param annotationType the annotation type
163      * @param field the annotated type
164      * @return true if the annotation is present else false
165      */

166     public static boolean isAnnotationPresent(final Class JavaDoc annotationType, final Field JavaDoc field) {
167         final AnnotationReader reader = AnnotationReader.getReaderFor(field.getDeclaringClass());
168         return reader.isAnnotationPresent(getAnnnotationName(annotationType), field);
169     }
170
171     /**
172       * Return all the annotations for a specific field.
173       *
174       * @param field the java.lang.reflect.Field object to find the annotations on.
175       * @return an array with the annotations
176       */

177      public static Annotation[] getAnnotations(final Field JavaDoc field) {
178          return AnnotationReader.getReaderFor(field.getDeclaringClass()).getAnnotations(field);
179      }
180
181     /**
182      * Return the annotation with a specific name for a specific field.
183      *
184      * @param annotationType the annotation class
185      * @param field the java.lang.reflect.Field object to find the annotation on.
186      * @return the annotation or null
187      */

188     public static Annotation getAnnotation(final Class JavaDoc annotationType, final Field JavaDoc field) {
189         final AnnotationReader reader = AnnotationReader.getReaderFor(field.getDeclaringClass());
190         return reader.getAnnotation(getAnnnotationName(annotationType), field);
191     }
192
193     /**
194      * Returns the annotation class name in Java style.
195      *
196      * @param annotationType
197      * @return
198      */

199     private static String JavaDoc getAnnnotationName(final Class JavaDoc annotationType) {
200         return annotationType.getName().replace('/', '.');
201     }
202
203     /**
204      * Returns true if the annotation is @Inherited
205      * @param annotationType
206      * @return
207      */

208     private static boolean isInherited(final Class JavaDoc annotationType) {
209         return AnnotationReader.getReaderFor(annotationType).isAnnotationPresent("java.lang.annotation.Inherited");
210     }
211 }
Popular Tags