1 22 package org.jboss.aop.annotation; 23 24 import javassist.ClassPool; 25 import javassist.CtClass; 26 import javassist.CtConstructor; 27 import javassist.CtField; 28 import javassist.CtMethod; 29 import javassist.NotFoundException; 30 import javassist.bytecode.AnnotationsAttribute; 31 import javassist.bytecode.ClassFile; 32 import javassist.bytecode.FieldInfo; 33 import javassist.bytecode.MethodInfo; 34 35 import org.jboss.aop.AspectManager; 36 import org.jboss.aop.annotation.factory.duplicate.javassist.AnnotationProxy; 37 import org.jboss.aop.util.ReflectToJavassist; 38 39 import java.lang.reflect.Constructor ; 40 import java.lang.reflect.Field ; 41 import java.lang.reflect.Method ; 42 import java.lang.reflect.Proxy ; 43 44 51 public class PortableAnnotationElement 52 { 53 public static boolean isInvisibleAnnotationPresent(Field field, String annotation) throws Exception 54 { 55 CtField ctMethod = ReflectToJavassist.fieldToJavassist(field); 56 return AnnotationElement.isInvisibleAnnotationPresent(ctMethod, annotation); 57 58 } 59 60 public static boolean isInvisibleAnnotationPresent(CtField field, String annotation) 61 { 62 FieldInfo mi = field.getFieldInfo2(); 63 64 AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag); 65 if (invisible == null) return false; 66 67 return invisible.getAnnotation(annotation) != null; 68 } 69 70 public static boolean isVisibleAnnotationPresent(CtField field, String annotation) 71 { 72 FieldInfo mi = field.getFieldInfo2(); 73 74 AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag); 75 if (visible == null) return false; 76 77 return visible.getAnnotation(annotation) != null; 78 } 79 80 public static boolean isAnyAnnotationPresent(CtField ctField, String annotation) 81 { 82 FieldInfo mi = ctField.getFieldInfo2(); 83 84 AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag); 85 if (visible != null) 86 { 87 if (visible.getAnnotation(annotation) != null) return true; 88 } 89 90 AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag); 91 if (invisible != null) 92 { 93 if (invisible.getAnnotation(annotation) != null) return true; 94 } 95 96 return false; 97 } 98 99 public static boolean isInvisibleAnnotationPresent(Method method, String annotation) throws Exception 100 { 101 CtMethod ctMethod = ReflectToJavassist.methodToJavassist(method); 102 if (ctMethod == null) return false; 103 MethodInfo mi = ctMethod.getMethodInfo2(); 104 105 106 AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag); 107 if (invisible == null) return false; 108 109 110 return invisible.getAnnotation(annotation) != null; 111 } 112 113 public static boolean isAnyAnnotationPresent(Field field, String annotation) throws Exception 114 { 115 CtField ctField = ReflectToJavassist.fieldToJavassist(field); 116 return AnnotationElement.isAnyAnnotationPresent(ctField, annotation); 117 118 } 119 120 public static boolean isAnyAnnotationPresent(Method method, String annotation) throws Exception 121 { 122 CtMethod ctMethod = ReflectToJavassist.methodToJavassist(method); 123 if (ctMethod == null) return false; 124 boolean present = AnnotationElement.isAnyAnnotationPresent(ctMethod, annotation); 125 return present; 126 127 } 128 129 public static boolean isAnyAnnotationPresent(CtMethod ctMethod, String annotation) 130 { 131 MethodInfo mi = ctMethod.getMethodInfo2(); 132 133 AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag); 134 if (visible != null) 135 { 136 if (visible.getAnnotation(annotation) != null) return true; 137 } 138 139 AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag); 140 if (invisible != null) 141 { 142 if (invisible.getAnnotation(annotation) != null) return true; 143 } 144 145 return false; 146 } 147 148 public static boolean isInvisibleAnnotationPresent(Constructor con, String annotation) throws Exception 149 { 150 CtConstructor ctMethod = ReflectToJavassist.constructorToJavassist(con); 151 return AnnotationElement.isInvisibleAnnotationPresent(ctMethod, annotation); 152 153 154 } 155 156 public static boolean isInvisibleAnnotationPresent(CtConstructor ctMethod, String annotation) 157 { 158 MethodInfo mi = ctMethod.getMethodInfo2(); 159 160 161 AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag); 162 if (invisible == null) return false; 163 164 return invisible.getAnnotation(annotation) != null; 165 } 166 167 public static boolean isVisibleAnnotationPresent(CtConstructor ctMethod, String annotation) 168 { 169 MethodInfo mi = ctMethod.getMethodInfo2(); 170 171 172 AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag); 173 if (visible == null) return false; 174 175 return visible.getAnnotation(annotation) != null; 176 } 177 178 public static boolean isAnyAnnotationPresent(Constructor con, String annotation) throws Exception 179 { 180 CtConstructor ctMethod = ReflectToJavassist.constructorToJavassist(con); 181 return AnnotationElement.isAnyAnnotationPresent(ctMethod, annotation); 182 183 } 184 185 public static boolean isAnyAnnotationPresent(CtConstructor ctMethod, String annotation) 186 { 187 MethodInfo mi = ctMethod.getMethodInfo2(); 188 189 AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag); 190 if (visible != null) 191 { 192 if (visible.getAnnotation(annotation) != null) return true; 193 } 194 195 AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag); 196 if (invisible != null) 197 { 198 if (invisible.getAnnotation(annotation) != null) return true; 199 } 200 201 return false; 202 } 203 204 public static boolean isInvisibleAnnotationPresent(Class clazz, String annotation) throws Exception 205 { 206 if (clazz == Void.TYPE) return false; 207 ClassFile cf = AnnotationElement.getClassFile(clazz); 208 209 AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag); 210 if (invisible == null) return false; 211 212 return invisible.getAnnotation(annotation) != null; 213 } 214 215 public static boolean isAnyAnnotationPresent(CtClass clazz, String annotation) throws Exception 216 { 217 if (clazz == CtClass.voidType) return false; 218 ClassFile cf = clazz.getClassFile2(); 219 AnnotationsAttribute visible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag); 220 if (visible != null) 221 { 222 if (visible.getAnnotation(annotation) != null) return true; 223 } 224 225 AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag); 226 if (invisible != null) 227 { 228 if (invisible.getAnnotation(annotation) != null) return true; 229 } 230 231 return false; 232 } 233 234 public static boolean isAnyAnnotationPresent(Class clazz, String annotation) throws Exception 235 { 236 if (clazz == Void.TYPE) return false; 237 ClassFile cf = AnnotationElement.getClassFile(clazz); 238 AnnotationsAttribute visible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag); 239 if (visible != null) 240 { 241 if (visible.getAnnotation(annotation) != null) return true; 242 } 243 244 AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag); 245 if (invisible != null) 246 { 247 if (invisible.getAnnotation(annotation) != null) return true; 248 } 249 250 return false; 251 } 252 253 protected static ClassFile getClassFile(Class clazz) throws NotFoundException 254 { 255 ClassPool pool = AspectManager.instance().findClassPool(clazz.getClassLoader()); 256 CtClass ct = pool.get(clazz.getName()); 257 ClassFile cf = ct.getClassFile2(); 258 return cf; 259 } 260 261 protected static Object create(AnnotationsAttribute group, Class annotation) throws Exception 262 { 263 if (group == null) return null; 264 javassist.bytecode.annotation.Annotation info = group.getAnnotation(annotation.getName()); 265 if (info == null) return null; 266 return AnnotationProxy.createProxy(info, annotation); 267 } 268 269 public static Object getInvisibleAnnotation(Method method, Class annotation) 270 { 271 try 272 { 273 CtMethod ctMethod = ReflectToJavassist.methodToJavassist(method); 274 if (ctMethod == null) 275 { 276 return null; 277 } 278 MethodInfo mi = ctMethod.getMethodInfo2(); 279 280 AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag); 281 if (invisible == null) return null; 282 283 return create(invisible, annotation); 284 } 285 catch (Exception e) 286 { 287 throw new RuntimeException (e); } 289 } 290 291 public static Object getInvisibleAnnotation(Constructor con, Class annotation) 292 { 293 try 294 { 295 CtConstructor ctMethod = ReflectToJavassist.constructorToJavassist(con); 296 MethodInfo mi = ctMethod.getMethodInfo2(); 297 298 299 AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag); 300 if (invisible == null) return null; 301 302 return create(invisible, annotation); 303 } 304 catch (Exception e) 305 { 306 throw new RuntimeException (e); } 308 } 309 310 public static Object getInvisibleAnnotation(Field field, Class annotation) 311 { 312 try 313 { 314 CtField ctField = ReflectToJavassist.fieldToJavassist(field); 315 FieldInfo mi = ctField.getFieldInfo2(); 316 317 318 AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag); 319 if (invisible == null) return null; 320 321 return create(invisible, annotation); 322 } 323 catch (Exception e) 324 { 325 throw new RuntimeException (e); } 327 } 328 329 public static Object getInvisibleAnnotation(Class clazz, Class annotation) 330 { 331 try 332 { 333 if (clazz == Void.TYPE) return null; 334 ClassFile cf = getClassFile(clazz); 335 336 AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag); 337 if (invisible == null) return null; 338 339 return create(invisible, annotation); 340 } 341 catch (Exception e) 342 { 343 throw new RuntimeException (e); } 345 } 346 347 354 public static Object getAnyAnnotation(Method method, Class annotation) 355 { 356 Object rtn = AnnotationElement.getVisibleAnnotation(method, annotation); 357 if (rtn != null) return rtn; 358 return getInvisibleAnnotation(method, annotation); 359 } 360 361 368 public static Object getAnyAnnotation(Constructor con, Class annotation) 369 { 370 Object rtn = AnnotationElement.getVisibleAnnotation(con, annotation); 371 if (rtn != null) return rtn; 372 return getInvisibleAnnotation(con, annotation); 373 } 374 375 public static Object getAnyAnnotation(Field field, Class annotation) 376 { 377 Object rtn = AnnotationElement.getVisibleAnnotation(field, annotation); 378 if (rtn != null) return rtn; 379 return getInvisibleAnnotation(field, annotation); 380 } 381 382 public static Object getAnyAnnotation(Class clazz, Class annotation) 383 { 384 if (clazz == Void.TYPE) return null; 385 Object rtn = AnnotationElement.getVisibleAnnotation(clazz, annotation); 386 if (rtn != null) return rtn; 387 return getInvisibleAnnotation(clazz, annotation); 388 } 389 390 public static boolean isAnyAnnotationPresent(Field field, Class annotation) throws Exception 391 { 392 if (AnnotationElement.isVisibleAnnotationPresent(field, annotation)) return true; 393 CtField ctMethod = ReflectToJavassist.fieldToJavassist(field); 394 return isInvisibleAnnotationPresent(ctMethod, annotation.getName()); 395 } 396 397 public static boolean isAnyAnnotationPresent(Class clazz, Class annotation) throws Exception 398 { 399 if (clazz == Void.TYPE) return false; 400 if (AnnotationElement.isVisibleAnnotationPresent(clazz, annotation)) return true; 401 ClassFile cf = getClassFile(clazz); 402 403 AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag); 404 if (invisible == null) return false; 405 406 return invisible.getAnnotation(annotation.getName()) != null; 407 } 408 409 public static boolean isAnyAnnotationPresent(Constructor con, Class annotation) throws Exception 410 { 411 if (AnnotationElement.isVisibleAnnotationPresent(con, annotation)) return true; 412 CtConstructor ctMethod = ReflectToJavassist.constructorToJavassist(con); 413 return isVisibleAnnotationPresent(ctMethod, annotation.getName()); 414 415 416 } 417 418 public static boolean isAnyAnnotationPresent(Method method, Class annotation) throws Exception 419 { 420 if (AnnotationElement.isVisibleAnnotationPresent(method, annotation)) return true; 421 CtMethod ctMethod = ReflectToJavassist.methodToJavassist(method); 422 if (ctMethod == null) return false; 423 MethodInfo mi = ctMethod.getMethodInfo2(); 424 425 426 AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag); 427 if (invisible == null) return false; 428 429 return invisible.getAnnotation(annotation.getName()) != null; 430 } 431 432 public static boolean isVisibleAnnotationPresent(Field field, String annotation) throws Exception 433 { 434 CtField ctMethod = ReflectToJavassist.fieldToJavassist(field); 435 return isVisibleAnnotationPresent(ctMethod, annotation); 436 } 437 438 public static boolean isVisibleAnnotationPresent(Class clazz, String annotation) throws Exception 439 { 440 if (clazz == Void.TYPE) return false; 441 442 ClassFile cf = getClassFile(clazz); 443 444 AnnotationsAttribute visible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag); 445 if (visible == null) return false; 446 447 return visible.getAnnotation(annotation) != null; 448 } 449 450 public static boolean isVisibleAnnotationPresent(Constructor con, String annotation) throws Exception 451 { 452 CtConstructor ctMethod = ReflectToJavassist.constructorToJavassist(con); 453 return isVisibleAnnotationPresent(ctMethod, annotation); 454 455 456 } 457 458 public static boolean isVisibleAnnotationPresent(Method method, String annotation) throws Exception 459 { 460 CtMethod ctMethod = ReflectToJavassist.methodToJavassist(method); 461 if (ctMethod == null) return false; 462 MethodInfo mi = ctMethod.getMethodInfo2(); 463 464 465 AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag); 466 if (visible == null) return false; 467 468 return visible.getAnnotation(annotation) != null; 469 } 470 471 public static Object [] getVisibleAnnotations(Class clazz) throws Exception 472 { 473 return AnnotationElement.getVisibleAnnotations(clazz); 474 } 475 476 public static Object [] getVisibleAnnotations(Method m) throws Exception 477 { 478 return AnnotationElement.getVisibleAnnotations(m); 479 } 480 481 public static Object [] getVisibleAnnotations(Field f) throws Exception 482 { 483 return AnnotationElement.getVisibleAnnotations(f); 484 } 485 486 public static Object [] getVisibleAnnotations(Constructor c) throws Exception 487 { 488 return AnnotationElement.getVisibleAnnotations(c); 489 } 490 491 public static Class getAnnotationType(Object o) 492 { 493 Class proxy = o.getClass(); 494 if (Proxy.isProxyClass(proxy)) 495 { 496 Class [] interfaces = proxy.getInterfaces(); 497 if (interfaces.length == 1) 498 { 499 return interfaces[0]; 500 } 501 } 502 return null; 503 } 504 } 505 | Popular Tags |