1 8 package org.codehaus.aspectwerkz.annotation; 9 10 import org.codehaus.aspectwerkz.reflect.MethodInfo; 11 import org.codehaus.aspectwerkz.reflect.ClassInfo; 12 import org.codehaus.aspectwerkz.reflect.ConstructorInfo; 13 import org.codehaus.aspectwerkz.reflect.FieldInfo; 14 import org.codehaus.aspectwerkz.reflect.ReflectHelper; 15 import org.codehaus.aspectwerkz.reflect.impl.asm.AsmClassInfo; 16 import org.codehaus.aspectwerkz.annotation.instrumentation.asm.AsmAnnotations; 17 18 import java.lang.reflect.Constructor ; 19 import java.lang.reflect.Field ; 20 import java.lang.reflect.Method ; 21 import java.util.List ; 22 import java.util.ArrayList ; 23 24 32 public final class Annotations { 33 34 private final static List EMPTY_LIST = new ArrayList (); 35 36 43 public static Annotation getAnnotation(final String annotationName, final Class klass) { 44 ClassInfo classInfo = AsmClassInfo.getClassInfo(klass.getName(), klass.getClassLoader()); 45 return AsmAnnotations.getAnnotation(annotationName, classInfo); 46 } 47 48 55 public static Annotation getAnnotation(final Class annotation, final Class klass) { 56 return getAnnotation(getAnnnotationName(annotation), klass); 57 } 58 59 66 public static Annotation getAnnotation(final String annotationName, final Method method) { 67 ClassLoader loader = method.getDeclaringClass().getClassLoader(); 68 ClassInfo classInfo = AsmClassInfo.getClassInfo(method.getDeclaringClass().getName(), loader); 69 MethodInfo methodInfo = classInfo.getMethod(ReflectHelper.calculateHash(method)); 70 return AsmAnnotations.getAnnotation(annotationName, methodInfo); 71 } 72 73 80 public static Annotation getAnnotation(final Class annotation, final Method method) { 81 return getAnnotation(getAnnnotationName(annotation), method); 82 } 83 84 91 public static Annotation getAnnotation(final String annotationName, final Constructor constructor) { 92 ClassLoader loader = constructor.getDeclaringClass().getClassLoader(); 93 ClassInfo classInfo = AsmClassInfo.getClassInfo(constructor.getDeclaringClass().getName(), loader); 94 ConstructorInfo constructorInfo = classInfo.getConstructor(ReflectHelper.calculateHash(constructor)); 95 return AsmAnnotations.getAnnotation(annotationName, constructorInfo); 96 } 97 98 105 public static Annotation getAnnotation(final Class annotation, final Constructor constructor) { 106 return getAnnotation(getAnnnotationName(annotation), constructor); 107 } 108 109 116 public static Annotation getAnnotation(final String annotationName, final Field field) { 117 ClassLoader loader = field.getDeclaringClass().getClassLoader(); 118 ClassInfo classInfo = AsmClassInfo.getClassInfo(field.getDeclaringClass().getName(), loader); 119 FieldInfo fieldInfo = classInfo.getField(ReflectHelper.calculateHash(field)); 120 return AsmAnnotations.getAnnotation(annotationName, fieldInfo); 121 } 122 123 130 public static Annotation getAnnotation(final Class annotation, final Field field) { 131 return getAnnotation(getAnnnotationName(annotation), field); 132 } 133 134 141 public static List getAnnotations(final String annotationName, final Class klass) { 142 ClassInfo classInfo = AsmClassInfo.getClassInfo(klass.getName(), klass.getClassLoader()); 143 return AsmAnnotations.getAnnotations(annotationName, classInfo); 144 } 145 146 153 public static List getAnnotations(final Class annotation, final Class klass) { 154 return getAnnotations(getAnnnotationName(annotation), klass); 155 } 156 157 164 public static List getAnnotations(final String annotationName, final Method method) { 165 ClassLoader loader = method.getDeclaringClass().getClassLoader(); 166 ClassInfo classInfo = AsmClassInfo.getClassInfo(method.getDeclaringClass().getName(), loader); 167 MethodInfo methodInfo = classInfo.getMethod(ReflectHelper.calculateHash(method)); 168 return AsmAnnotations.getAnnotations(annotationName, methodInfo); 169 } 170 171 178 public static List getAnnotations(final Class annotation, final Method method) { 179 return getAnnotations(getAnnnotationName(annotation), method); 180 } 181 182 189 public static List getAnnotations(final String annotationName, final Constructor constructor) { 190 ClassLoader loader = constructor.getDeclaringClass().getClassLoader(); 191 ClassInfo classInfo = AsmClassInfo.getClassInfo(constructor.getDeclaringClass().getName(), loader); 192 ConstructorInfo constructorInfo = classInfo.getConstructor(ReflectHelper.calculateHash(constructor)); 193 return AsmAnnotations.getAnnotations(annotationName, constructorInfo); 194 } 195 196 203 public static List getAnnotations(final Class annotation, final Constructor constructor) { 204 return getAnnotations(getAnnnotationName(annotation), constructor); 205 } 206 207 214 public static List getAnnotations(final String annotationName, final Field field) { 215 ClassLoader loader = field.getDeclaringClass().getClassLoader(); 216 ClassInfo classInfo = AsmClassInfo.getClassInfo(field.getDeclaringClass().getName(), loader); 217 FieldInfo fieldInfo = classInfo.getField(ReflectHelper.calculateHash(field)); 218 return AsmAnnotations.getAnnotations(annotationName, fieldInfo); 219 } 220 221 228 public static List getAnnotations(final Class annotation, final Field field) { 229 return getAnnotations(getAnnnotationName(annotation), field); 230 } 231 232 239 public static List getAnnotationInfos(final Class klass) { 240 return AsmClassInfo.getClassInfo(klass.getName(), klass.getClassLoader()).getAnnotations(); 241 } 242 243 250 public static List getAnnotationInfos(final Method method) { 251 ClassLoader loader = method.getDeclaringClass().getClassLoader(); 252 ClassInfo classInfo = AsmClassInfo.getClassInfo(method.getDeclaringClass().getName(), loader); 253 MethodInfo methodInfo = classInfo.getMethod(ReflectHelper.calculateHash(method)); 255 if (methodInfo != null) { 256 return methodInfo.getAnnotations(); 257 } else { 258 return EMPTY_LIST; 259 } 260 } 261 262 269 public static List getAnnotationInfos(final Constructor constructor) { 270 ClassLoader loader = constructor.getDeclaringClass().getClassLoader(); 271 ClassInfo classInfo = AsmClassInfo.getClassInfo(constructor.getDeclaringClass().getName(), loader); 272 return classInfo.getConstructor(ReflectHelper.calculateHash(constructor)).getAnnotations(); 273 } 274 275 282 public static List getAnnotationInfos(final Field field) { 283 ClassLoader loader = field.getDeclaringClass().getClassLoader(); 284 ClassInfo classInfo = AsmClassInfo.getClassInfo(field.getDeclaringClass().getName(), loader); 285 FieldInfo fieldInfo = classInfo.getField(ReflectHelper.calculateHash(field)); 287 if (fieldInfo != null) { 288 return fieldInfo.getAnnotations(); 289 } else { 290 return EMPTY_LIST; 291 } 292 } 293 294 323 private static String getAnnnotationName(Class annotation) { 324 return annotation.getName().replace('/', '.'); 325 } 326 }
| Popular Tags
|