1 8 package com.tc.backport175; 9 10 import com.tc.backport175.bytecode.AnnotationReader; 11 12 import java.lang.reflect.Constructor ; 13 import java.lang.reflect.Field ; 14 import java.lang.reflect.Method ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 25 public final class Annotations { 26 27 34 public static boolean isAnnotationPresent(final Class annotationType, final Class 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 52 public static Annotation[] getAnnotations(final Class target) { 53 Annotation[] declaredAnnotations = AnnotationReader.getReaderFor(target).getAnnotations(); 54 if (target.getSuperclass() == null) { 55 return declaredAnnotations; 56 } else { 57 List annotations = new ArrayList (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 78 public static Annotation getAnnotation(final Class annotationType, final Class 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 98 public static boolean isAnnotationPresent(final Class annotationType, final Method method) { 99 final AnnotationReader reader = AnnotationReader.getReaderFor(method.getDeclaringClass()); 100 return reader.isAnnotationPresent(getAnnnotationName(annotationType), method); 101 } 102 103 109 public static Annotation[] getAnnotations(final Method method) { 110 return AnnotationReader.getReaderFor(method.getDeclaringClass()).getAnnotations(method); 111 } 112 113 120 public static Annotation getAnnotation(final Class annotationType, final Method method) { 121 final AnnotationReader reader = AnnotationReader.getReaderFor(method.getDeclaringClass()); 122 return reader.getAnnotation(getAnnnotationName(annotationType), method); 123 } 124 125 132 public static boolean isAnnotationPresent(final Class annotationType, final Constructor constructor) { 133 final AnnotationReader reader = AnnotationReader.getReaderFor(constructor.getDeclaringClass()); 134 return reader.isAnnotationPresent(getAnnnotationName(annotationType), constructor); 135 } 136 137 143 public static Annotation[] getAnnotations(final Constructor constructor) { 144 return AnnotationReader.getReaderFor(constructor.getDeclaringClass()).getAnnotations(constructor); 145 } 146 147 154 public static Annotation getAnnotation(final Class annotationType, final Constructor constructor) { 155 final AnnotationReader reader = AnnotationReader.getReaderFor(constructor.getDeclaringClass()); 156 return reader.getAnnotation(getAnnnotationName(annotationType), constructor); 157 } 158 159 166 public static boolean isAnnotationPresent(final Class annotationType, final Field field) { 167 final AnnotationReader reader = AnnotationReader.getReaderFor(field.getDeclaringClass()); 168 return reader.isAnnotationPresent(getAnnnotationName(annotationType), field); 169 } 170 171 177 public static Annotation[] getAnnotations(final Field field) { 178 return AnnotationReader.getReaderFor(field.getDeclaringClass()).getAnnotations(field); 179 } 180 181 188 public static Annotation getAnnotation(final Class annotationType, final Field field) { 189 final AnnotationReader reader = AnnotationReader.getReaderFor(field.getDeclaringClass()); 190 return reader.getAnnotation(getAnnnotationName(annotationType), field); 191 } 192 193 199 private static String getAnnnotationName(final Class annotationType) { 200 return annotationType.getName().replace('/', '.'); 201 } 202 203 208 private static boolean isInherited(final Class annotationType) { 209 return AnnotationReader.getReaderFor(annotationType).isAnnotationPresent("java.lang.annotation.Inherited"); 210 } 211 } | Popular Tags |