001: /*******************************************************************************************
002: * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
003: * http://backport175.codehaus.org *
004: * --------------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of Apache License Version 2.0 *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: *******************************************************************************************/package com.tc.backport175;
008:
009: import com.tc.backport175.bytecode.AnnotationReader;
010:
011: import java.lang.reflect.Constructor;
012: import java.lang.reflect.Field;
013: import java.lang.reflect.Method;
014: import java.util.ArrayList;
015: import java.util.List;
016:
017: /**
018: * Helper class for reader retrieval of strongly typed JavaDoc annotations (as well as regular Java 5 {@link
019: * java.lang.reader.RetentionPolicy.RUNTIME} annotations when running Java 1.5.x).
020: *
021: * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
022: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
023: */
024: public final class Annotations {
025:
026: /**
027: * Checks if an annotation is present at a specific class.
028: *
029: * @param annotationType the annotation type
030: * @param target the annotated type
031: * @return true if the annotation is present else false
032: */
033: public static boolean isAnnotationPresent(
034: final Class annotationType, final Class target) {
035: boolean isPresent = AnnotationReader
036: .getReaderFor(target)
037: .isAnnotationPresent(getAnnnotationName(annotationType));
038: if (!isPresent && isInherited(annotationType)) {
039: if (target.getSuperclass() == null) {
040: return isPresent;
041: } else {
042: return isAnnotationPresent(annotationType, target
043: .getSuperclass());
044: }
045: }
046: return isPresent;
047: }
048:
049: /**
050: * Return all the annotations for a specific class.
051: *
052: * @param target the java.lang.Class object to find the annotations on.
053: * @return an array with the annotations
054: */
055: public static Annotation[] getAnnotations(final Class target) {
056: Annotation[] declaredAnnotations = AnnotationReader
057: .getReaderFor(target).getAnnotations();
058: if (target.getSuperclass() == null) {
059: return declaredAnnotations;
060: } else {
061: List annotations = new ArrayList(declaredAnnotations.length);
062: Annotation[] parents = getAnnotations(target
063: .getSuperclass());
064: for (int i = 0; i < parents.length; i++) {
065: if (isInherited(parents[i].annotationType())) {
066: annotations.add(parents[i]);
067: }
068: }
069: for (int i = 0; i < declaredAnnotations.length; i++) {
070: annotations.add(declaredAnnotations[i]);
071: }
072: return (Annotation[]) annotations
073: .toArray(new Annotation[] {});
074: }
075: }
076:
077: /**
078: * Return the annotation with a specific name for a specific class.
079: *
080: * @param annotationType the annotation class
081: * @param target the java.lang.Class object to find the annotation on.
082: * @return the annotation or null
083: */
084: public static Annotation getAnnotation(final Class annotationType,
085: final Class target) {
086: final AnnotationReader reader = AnnotationReader
087: .getReaderFor(target);
088: Annotation annotation = reader
089: .getAnnotation(getAnnnotationName(annotationType));
090: if (annotation == null && isInherited(annotationType)) {
091: if (target.getSuperclass() == null) {
092: return annotation;
093: } else {
094: return getAnnotation(annotationType, target
095: .getSuperclass());
096: }
097: }
098: return annotation;
099: }
100:
101: /**
102: * Checks if an annotation is present at a specific method.
103: *
104: * @param annotationType the annotation type
105: * @param method the annotated type
106: * @return true if the annotation is present else false
107: */
108: public static boolean isAnnotationPresent(
109: final Class annotationType, final Method method) {
110: final AnnotationReader reader = AnnotationReader
111: .getReaderFor(method.getDeclaringClass());
112: return reader.isAnnotationPresent(
113: getAnnnotationName(annotationType), method);
114: }
115:
116: /**
117: * Return all the annotations for a specific method.
118: *
119: * @param method the java.lang.reflect.Method object to find the annotations on.
120: * @return an array with the annotations
121: */
122: public static Annotation[] getAnnotations(final Method method) {
123: return AnnotationReader
124: .getReaderFor(method.getDeclaringClass())
125: .getAnnotations(method);
126: }
127:
128: /**
129: * Return the annotation with a specific name for a specific method.
130: *
131: * @param annotationType the annotation class
132: * @param method the java.lang.refect.Method object to find the annotation on.
133: * @return the annotation or null
134: */
135: public static Annotation getAnnotation(final Class annotationType,
136: final Method method) {
137: final AnnotationReader reader = AnnotationReader
138: .getReaderFor(method.getDeclaringClass());
139: return reader.getAnnotation(getAnnnotationName(annotationType),
140: method);
141: }
142:
143: /**
144: * Checks if an annotation is present at a specific method.
145: *
146: * @param annotationType the annotation type
147: * @param constructor the annotated type
148: * @return true if the annotation is present else false
149: */
150: public static boolean isAnnotationPresent(
151: final Class annotationType, final Constructor constructor) {
152: final AnnotationReader reader = AnnotationReader
153: .getReaderFor(constructor.getDeclaringClass());
154: return reader.isAnnotationPresent(
155: getAnnnotationName(annotationType), constructor);
156: }
157:
158: /**
159: * Return all the annotations for a specific constructor.
160: *
161: * @param constructor the java.lang.reflect.Constructor object to find the annotations on.
162: * @return an array with the annotations
163: */
164: public static Annotation[] getAnnotations(
165: final Constructor constructor) {
166: return AnnotationReader.getReaderFor(
167: constructor.getDeclaringClass()).getAnnotations(
168: constructor);
169: }
170:
171: /**
172: * Return the annotation with a specific name for a specific constructor.
173: *
174: * @param annotationType the annotation class
175: * @param constructor the java.lang.refect.Constructor object to find the annotation on.
176: * @return the annotation or null
177: */
178: public static Annotation getAnnotation(final Class annotationType,
179: final Constructor constructor) {
180: final AnnotationReader reader = AnnotationReader
181: .getReaderFor(constructor.getDeclaringClass());
182: return reader.getAnnotation(getAnnnotationName(annotationType),
183: constructor);
184: }
185:
186: /**
187: * Checks if an annotation is present at a specific field.
188: *
189: * @param annotationType the annotation type
190: * @param field the annotated type
191: * @return true if the annotation is present else false
192: */
193: public static boolean isAnnotationPresent(
194: final Class annotationType, final Field field) {
195: final AnnotationReader reader = AnnotationReader
196: .getReaderFor(field.getDeclaringClass());
197: return reader.isAnnotationPresent(
198: getAnnnotationName(annotationType), field);
199: }
200:
201: /**
202: * Return all the annotations for a specific field.
203: *
204: * @param field the java.lang.reflect.Field object to find the annotations on.
205: * @return an array with the annotations
206: */
207: public static Annotation[] getAnnotations(final Field field) {
208: return AnnotationReader.getReaderFor(field.getDeclaringClass())
209: .getAnnotations(field);
210: }
211:
212: /**
213: * Return the annotation with a specific name for a specific field.
214: *
215: * @param annotationType the annotation class
216: * @param field the java.lang.reflect.Field object to find the annotation on.
217: * @return the annotation or null
218: */
219: public static Annotation getAnnotation(final Class annotationType,
220: final Field field) {
221: final AnnotationReader reader = AnnotationReader
222: .getReaderFor(field.getDeclaringClass());
223: return reader.getAnnotation(getAnnnotationName(annotationType),
224: field);
225: }
226:
227: /**
228: * Returns the annotation class name in Java style.
229: *
230: * @param annotationType
231: * @return
232: */
233: private static String getAnnnotationName(final Class annotationType) {
234: return annotationType.getName().replace('/', '.');
235: }
236:
237: /**
238: * Returns true if the annotation is @Inherited
239: * @param annotationType
240: * @return
241: */
242: private static boolean isInherited(final Class annotationType) {
243: return AnnotationReader.getReaderFor(annotationType)
244: .isAnnotationPresent("java.lang.annotation.Inherited");
245: }
246: }
|