1 12 package org.aspectj.internal.lang.reflect; 13 14 import java.lang.annotation.Annotation ; 15 import java.lang.reflect.Constructor ; 16 import java.lang.reflect.Field ; 17 import java.lang.reflect.Method ; 18 import java.lang.reflect.Modifier ; 19 import java.lang.reflect.Type ; 20 import java.lang.reflect.TypeVariable ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.EnumSet ; 24 import java.util.List ; 25 import java.util.Set ; 26 27 import org.aspectj.internal.lang.annotation.ajcDeclareAnnotation; 28 import org.aspectj.internal.lang.annotation.ajcDeclareEoW; 29 import org.aspectj.internal.lang.annotation.ajcDeclareParents; 30 import org.aspectj.internal.lang.annotation.ajcDeclarePrecedence; 31 import org.aspectj.internal.lang.annotation.ajcDeclareSoft; 32 import org.aspectj.internal.lang.annotation.ajcITD; 33 import org.aspectj.internal.lang.annotation.ajcPrivileged; 34 import org.aspectj.lang.annotation.After; 35 import org.aspectj.lang.annotation.AfterReturning; 36 import org.aspectj.lang.annotation.AfterThrowing; 37 import org.aspectj.lang.annotation.Around; 38 import org.aspectj.lang.annotation.Aspect; 39 import org.aspectj.lang.annotation.Before; 40 import org.aspectj.lang.annotation.DeclareError; 41 import org.aspectj.lang.annotation.DeclareWarning; 42 import org.aspectj.lang.reflect.Advice; 43 import org.aspectj.lang.reflect.AdviceKind; 44 import org.aspectj.lang.reflect.AjType; 45 import org.aspectj.lang.reflect.AjTypeSystem; 46 import org.aspectj.lang.reflect.DeclareAnnotation; 47 import org.aspectj.lang.reflect.DeclareErrorOrWarning; 48 import org.aspectj.lang.reflect.DeclareParents; 49 import org.aspectj.lang.reflect.DeclarePrecedence; 50 import org.aspectj.lang.reflect.DeclareSoft; 51 import org.aspectj.lang.reflect.InterTypeConstructorDeclaration; 52 import org.aspectj.lang.reflect.InterTypeFieldDeclaration; 53 import org.aspectj.lang.reflect.InterTypeMethodDeclaration; 54 import org.aspectj.lang.reflect.NoSuchAdviceException; 55 import org.aspectj.lang.reflect.NoSuchPointcutException; 56 import org.aspectj.lang.reflect.PerClause; 57 import org.aspectj.lang.reflect.PerClauseKind; 58 import org.aspectj.lang.reflect.Pointcut; 59 60 61 65 public class AjTypeImpl<T> implements AjType<T> { 66 67 private static final String ajcMagic = "ajc$"; 68 69 private Class <T> clazz; 70 private Pointcut[] declaredPointcuts = null; 71 private Pointcut[] pointcuts = null; 72 private Advice[] declaredAdvice = null; 73 private Advice[] advice = null; 74 private InterTypeMethodDeclaration[] declaredITDMethods = null; 75 private InterTypeMethodDeclaration[] itdMethods = null; 76 private InterTypeFieldDeclaration[] declaredITDFields = null; 77 private InterTypeFieldDeclaration[] itdFields = null; 78 private InterTypeConstructorDeclaration[] itdCons = null; 79 private InterTypeConstructorDeclaration[] declaredITDCons = null; 80 81 public AjTypeImpl(Class <T> fromClass) { 82 this.clazz = fromClass; 83 } 84 85 88 public String getName() { 89 return clazz.getName(); 90 } 91 92 95 public Package getPackage() { 96 return clazz.getPackage(); 97 } 98 99 102 public AjType<?>[] getInterfaces() { 103 Class <?>[] baseInterfaces = clazz.getInterfaces(); 104 return toAjTypeArray(baseInterfaces); 105 } 106 107 110 public int getModifiers() { 111 return clazz.getModifiers(); 112 } 113 114 public Class <T> getJavaClass() { 115 return clazz; 116 } 117 118 121 public AjType<? super T> getSupertype() { 122 Class <? super T> superclass = clazz.getSuperclass(); 123 return superclass==null ? null : (AjType<? super T>) new AjTypeImpl(superclass); 124 } 125 126 129 public Type getGenericSupertype() { 130 return clazz.getGenericSuperclass(); 131 } 132 133 136 public Method getEnclosingMethod() { 137 return clazz.getEnclosingMethod(); 138 } 139 140 143 public Constructor getEnclosingConstructor() { 144 return clazz.getEnclosingConstructor(); 145 } 146 147 150 public AjType<?> getEnclosingType() { 151 Class <?> enc = clazz.getEnclosingClass(); 152 return enc != null ? new AjTypeImpl(enc) : null; 153 } 154 155 158 public AjType<?> getDeclaringType() { 159 Class dec = clazz.getDeclaringClass(); 160 return dec != null ? new AjTypeImpl(dec) : null; 161 } 162 163 public PerClause getPerClause() { 164 if (isAspect()) { 165 Aspect aspectAnn = clazz.getAnnotation(Aspect.class); 166 String perClause = aspectAnn.value(); 167 if (perClause.equals("")) { 168 if (getSupertype().isAspect()) { 169 return getSupertype().getPerClause(); 170 } 171 return new PerClauseImpl(PerClauseKind.SINGLETON); 172 } else if (perClause.startsWith("perthis(")) { 173 return new PointcutBasedPerClauseImpl(PerClauseKind.PERTHIS,perClause.substring("perthis(".length(),perClause.length() - 1)); 174 } else if (perClause.startsWith("pertarget(")) { 175 return new PointcutBasedPerClauseImpl(PerClauseKind.PERTARGET,perClause.substring("pertarget(".length(),perClause.length() - 1)); 176 } else if (perClause.startsWith("percflow(")) { 177 return new PointcutBasedPerClauseImpl(PerClauseKind.PERCFLOW,perClause.substring("percflow(".length(),perClause.length() - 1)); 178 } else if (perClause.startsWith("percflowbelow(")) { 179 return new PointcutBasedPerClauseImpl(PerClauseKind.PERCFLOWBELOW,perClause.substring("percflowbelow(".length(),perClause.length() - 1)); 180 } else if (perClause.startsWith("pertypewithin")) { 181 return new TypePatternBasedPerClauseImpl(PerClauseKind.PERTYPEWITHIN,perClause.substring("pertypewithin(".length(),perClause.length() - 1)); 182 } else { 183 throw new IllegalStateException ("Per-clause not recognized: " + perClause); 184 } 185 } else { 186 return null; 187 } 188 } 189 190 193 public boolean isAnnotationPresent(Class <? extends Annotation > annotationType) { 194 return clazz.isAnnotationPresent(annotationType); 195 } 196 197 public <A extends Annotation > A getAnnotation(Class <A> annotationType) { 198 return clazz.getAnnotation(annotationType); 199 } 200 201 204 public Annotation [] getAnnotations() { 205 return clazz.getAnnotations(); 206 } 207 208 211 public Annotation [] getDeclaredAnnotations() { 212 return clazz.getDeclaredAnnotations(); 213 } 214 215 218 public AjType<?>[] getAjTypes() { 219 Class [] classes = clazz.getClasses(); 220 return toAjTypeArray(classes); 221 } 222 223 226 public AjType<?>[] getDeclaredAjTypes() { 227 Class [] classes = clazz.getDeclaredClasses(); 228 return toAjTypeArray(classes); 229 } 230 231 234 public Constructor getConstructor(AjType<?>... parameterTypes) throws NoSuchMethodException { 235 return clazz.getConstructor(toClassArray(parameterTypes)); 236 } 237 238 241 public Constructor [] getConstructors() { 242 return clazz.getConstructors(); 243 } 244 245 248 public Constructor getDeclaredConstructor(AjType<?>... parameterTypes) throws NoSuchMethodException { 249 return clazz.getDeclaredConstructor(toClassArray(parameterTypes)); 250 } 251 252 255 public Constructor [] getDeclaredConstructors() { 256 return clazz.getDeclaredConstructors(); 257 } 258 259 262 public Field getDeclaredField(String name) throws NoSuchFieldException { 263 Field f = clazz.getDeclaredField(name); 264 if (f.getName().startsWith(ajcMagic)) throw new NoSuchFieldException (name); 265 return f; 266 } 267 268 271 public Field [] getDeclaredFields() { 272 Field [] fields = clazz.getDeclaredFields(); 273 List <Field > filteredFields = new ArrayList <Field >(); 274 for (Field field : fields) 275 if (!field.getName().startsWith(ajcMagic) 276 && !field.isAnnotationPresent(DeclareWarning.class) 277 && !field.isAnnotationPresent(DeclareError.class)) { 278 filteredFields.add(field); 279 } 280 Field [] ret = new Field [filteredFields.size()]; 281 filteredFields.toArray(ret); 282 return ret; 283 } 284 285 288 public Field getField(String name) throws NoSuchFieldException { 289 Field f = clazz.getField(name); 290 if (f.getName().startsWith(ajcMagic)) throw new NoSuchFieldException (name); 291 return f; 292 } 293 294 297 public Field [] getFields() { 298 Field [] fields = clazz.getFields(); 299 List <Field > filteredFields = new ArrayList <Field >(); 300 for (Field field : fields) 301 if (!field.getName().startsWith(ajcMagic) 302 && !field.isAnnotationPresent(DeclareWarning.class) 303 && !field.isAnnotationPresent(DeclareError.class)) { 304 filteredFields.add(field); 305 } 306 Field [] ret = new Field [filteredFields.size()]; 307 filteredFields.toArray(ret); 308 return ret; 309 } 310 311 314 public Method getDeclaredMethod(String name, AjType<?>... parameterTypes) throws NoSuchMethodException { 315 Method m = clazz.getDeclaredMethod(name,toClassArray(parameterTypes)); 316 if (!isReallyAMethod(m)) throw new NoSuchMethodException (name); 317 return m; 318 } 319 320 323 public Method getMethod(String name, AjType<?>... parameterTypes) throws NoSuchMethodException { 324 Method m = clazz.getMethod(name,toClassArray(parameterTypes)); 325 if (!isReallyAMethod(m)) throw new NoSuchMethodException (name); 326 return m; 327 } 328 329 332 public Method [] getDeclaredMethods() { 333 Method [] methods = clazz.getDeclaredMethods(); 334 List <Method > filteredMethods = new ArrayList <Method >(); 335 for (Method method : methods) { 336 if (isReallyAMethod(method)) filteredMethods.add(method); 337 } 338 Method [] ret = new Method [filteredMethods.size()]; 339 filteredMethods.toArray(ret); 340 return ret; 341 } 342 343 346 public Method [] getMethods() { 347 Method [] methods = clazz.getMethods(); 348 List <Method > filteredMethods = new ArrayList <Method >(); 349 for (Method method : methods) { 350 if (isReallyAMethod(method)) filteredMethods.add(method); 351 } 352 Method [] ret = new Method [filteredMethods.size()]; 353 filteredMethods.toArray(ret); 354 return ret; 355 } 356 357 private boolean isReallyAMethod(Method method) { 358 if (method.getName().startsWith(ajcMagic)) return false; 359 if (method.isAnnotationPresent(org.aspectj.lang.annotation.Pointcut.class)) return false; 360 if (method.isAnnotationPresent(Before.class)) return false; 361 if (method.isAnnotationPresent(After.class)) return false; 362 if (method.isAnnotationPresent(AfterReturning.class)) return false; 363 if (method.isAnnotationPresent(AfterThrowing.class)) return false; 364 if (method.isAnnotationPresent(Around.class)) return false; 365 return true; 366 } 367 368 371 public Pointcut getDeclaredPointcut(String name) throws NoSuchPointcutException { 372 Pointcut[] pcs = getDeclaredPointcuts(); 373 for (Pointcut pc : pcs) 374 if (pc.getName().equals(name)) return pc; 375 throw new NoSuchPointcutException(name); 376 } 377 378 381 public Pointcut getPointcut(String name) throws NoSuchPointcutException { 382 Pointcut[] pcs = getPointcuts(); 383 for (Pointcut pc : pcs) 384 if (pc.getName().equals(name)) return pc; 385 throw new NoSuchPointcutException(name); 386 } 387 388 391 public Pointcut[] getDeclaredPointcuts() { 392 if (declaredPointcuts != null) return declaredPointcuts; 393 List <Pointcut> pointcuts = new ArrayList <Pointcut>(); 394 Method [] methods = clazz.getDeclaredMethods(); 395 for (Method method : methods) { 396 Pointcut pc = asPointcut(method); 397 if (pc != null) pointcuts.add(pc); 398 } 399 Pointcut[] ret = new Pointcut[pointcuts.size()]; 400 pointcuts.toArray(ret); 401 declaredPointcuts = ret; 402 return ret; 403 } 404 405 408 public Pointcut[] getPointcuts() { 409 if (pointcuts != null) return pointcuts; 410 List <Pointcut> pcuts = new ArrayList <Pointcut>(); 411 Method [] methods = clazz.getMethods(); 412 for (Method method : methods) { 413 Pointcut pc = asPointcut(method); 414 if (pc != null) pcuts.add(pc); 415 } 416 Pointcut[] ret = new Pointcut[pcuts.size()]; 417 pcuts.toArray(ret); 418 pointcuts = ret; 419 return ret; 420 } 421 422 private Pointcut asPointcut(Method method) { 423 org.aspectj.lang.annotation.Pointcut pcAnn = method.getAnnotation(org.aspectj.lang.annotation.Pointcut.class); 424 if (pcAnn != null) { 425 String name = method.getName(); 426 if (name.startsWith(ajcMagic)) { 427 int nameStart = name.indexOf("$$"); 429 name = name.substring(nameStart +2,name.length()); 430 int nextDollar = name.indexOf("$"); 431 if (nextDollar != -1) name = name.substring(0,nextDollar); 432 } 433 return new PointcutImpl(name,pcAnn.value(),method,AjTypeSystem.getAjType(method.getDeclaringClass()),pcAnn.argNames()); 434 } else { 435 return null; 436 } 437 } 438 439 440 public Advice[] getDeclaredAdvice(AdviceKind... ofType) { 441 Set <AdviceKind> types; 442 if (ofType.length == 0) { 443 types = EnumSet.allOf(AdviceKind.class); 444 } else { 445 types = EnumSet.noneOf(AdviceKind.class); 446 types.addAll(Arrays.asList(ofType)); 447 } 448 return getDeclaredAdvice(types); 449 } 450 451 public Advice[] getAdvice(AdviceKind... ofType) { 452 Set <AdviceKind> types; 453 if (ofType.length == 0) { 454 types = EnumSet.allOf(AdviceKind.class); 455 } else { 456 types = EnumSet.noneOf(AdviceKind.class); 457 types.addAll(Arrays.asList(ofType)); 458 } 459 return getAdvice(types); 460 } 461 462 465 private Advice[] getDeclaredAdvice(Set ofAdviceTypes) { 466 if (declaredAdvice == null) initDeclaredAdvice(); 467 List <Advice> adviceList = new ArrayList <Advice>(); 468 for (Advice a : declaredAdvice) { 469 if (ofAdviceTypes.contains(a.getKind())) adviceList.add(a); 470 } 471 Advice[] ret = new Advice[adviceList.size()]; 472 adviceList.toArray(ret); 473 return ret; 474 } 475 476 private void initDeclaredAdvice() { 477 Method [] methods = clazz.getDeclaredMethods(); 478 List <Advice> adviceList = new ArrayList <Advice>(); 479 for (Method method : methods) { 480 Advice advice = asAdvice(method); 481 if (advice != null) adviceList.add(advice); 482 } 483 declaredAdvice = new Advice[adviceList.size()]; 484 adviceList.toArray(declaredAdvice); 485 } 486 487 490 private Advice[] getAdvice(Set ofAdviceTypes) { 491 if (advice == null) initAdvice(); 492 List <Advice> adviceList = new ArrayList <Advice>(); 493 for (Advice a : advice) { 494 if (ofAdviceTypes.contains(a.getKind())) adviceList.add(a); 495 } 496 Advice[] ret = new Advice[adviceList.size()]; 497 adviceList.toArray(ret); 498 return ret; 499 } 500 501 private void initAdvice() { 502 Method [] methods = clazz.getMethods(); 503 List <Advice> adviceList = new ArrayList <Advice>(); 504 for (Method method : methods) { 505 Advice advice = asAdvice(method); 506 if (advice != null) adviceList.add(advice); 507 } 508 advice = new Advice[adviceList.size()]; 509 adviceList.toArray(advice); 510 } 511 512 513 public Advice getAdvice(String name) throws NoSuchAdviceException { 514 if (name.equals("")) throw new IllegalArgumentException ("use getAdvice(AdviceType...) instead for un-named advice"); 515 if (advice == null) initAdvice(); 516 for (Advice a : advice) { 517 if (a.getName().equals(name)) return a; 518 } 519 throw new NoSuchAdviceException(name); 520 } 521 522 public Advice getDeclaredAdvice(String name) throws NoSuchAdviceException { 523 if (name.equals("")) throw new IllegalArgumentException ("use getAdvice(AdviceType...) instead for un-named advice"); 524 if (declaredAdvice == null) initDeclaredAdvice(); 525 for (Advice a : declaredAdvice) { 526 if (a.getName().equals(name)) return a; 527 } 528 throw new NoSuchAdviceException(name); 529 } 530 531 private Advice asAdvice(Method method) { 532 if (method.getAnnotations().length == 0) return null; 533 Before beforeAnn = method.getAnnotation(Before.class); 534 if (beforeAnn != null) return new AdviceImpl(method,beforeAnn.value(),AdviceKind.BEFORE); 535 After afterAnn = method.getAnnotation(After.class); 536 if (afterAnn != null) return new AdviceImpl(method,afterAnn.value(),AdviceKind.AFTER); 537 AfterReturning afterReturningAnn = method.getAnnotation(AfterReturning.class); 538 if (afterReturningAnn != null) { 539 String pcExpr = afterReturningAnn.pointcut(); 540 if (pcExpr.equals("")) pcExpr = afterReturningAnn.value(); 541 return new AdviceImpl(method,pcExpr,AdviceKind.AFTER_RETURNING,afterReturningAnn.returning()); 542 } 543 AfterThrowing afterThrowingAnn = method.getAnnotation(AfterThrowing.class); 544 if (afterThrowingAnn != null) { 545 String pcExpr = afterThrowingAnn.pointcut(); 546 if (pcExpr == null) pcExpr = afterThrowingAnn.value(); 547 return new AdviceImpl(method,pcExpr,AdviceKind.AFTER_THROWING,afterThrowingAnn.throwing()); 548 } 549 Around aroundAnn = method.getAnnotation(Around.class); 550 if (aroundAnn != null) return new AdviceImpl(method,aroundAnn.value(),AdviceKind.AROUND); 551 return null; 552 } 553 554 557 public InterTypeMethodDeclaration getDeclaredITDMethod(String name, 558 AjType<?> target, AjType<?>... parameterTypes) throws NoSuchMethodException { 559 InterTypeMethodDeclaration[] itdms = getDeclaredITDMethods(); 560 outer: for (InterTypeMethodDeclaration itdm : itdms) { 561 try { 562 if (!itdm.getName().equals(name)) continue; 563 AjType<?> itdTarget = itdm.getTargetType(); 564 if (itdTarget.equals(target)) { 565 AjType<?>[] ptypes = itdm.getParameterTypes(); 566 if (ptypes.length == parameterTypes.length) { 567 for (int i = 0; i < ptypes.length; i++) { 568 if (!ptypes[i].equals(parameterTypes[i])) 569 continue outer; 570 } 571 return itdm; 572 } 573 } 574 } catch (ClassNotFoundException cnf) { 575 } 577 } 578 throw new NoSuchMethodException (name); 579 } 580 581 584 public InterTypeMethodDeclaration[] getDeclaredITDMethods() { 585 if (this.declaredITDMethods == null) { 586 List <InterTypeMethodDeclaration> itdms = new ArrayList <InterTypeMethodDeclaration>(); 587 Method [] baseMethods = clazz.getDeclaredMethods(); 588 for (Method m : baseMethods) { 589 if (!m.getName().contains("ajc$interMethodDispatch1$")) continue; 590 if (m.isAnnotationPresent(ajcITD.class)) { 591 ajcITD ann = m.getAnnotation(ajcITD.class); 592 InterTypeMethodDeclaration itdm = 593 new InterTypeMethodDeclarationImpl( 594 this,ann.targetType(),ann.modifiers(), 595 ann.name(),m); 596 itdms.add(itdm); 597 } 598 } 599 addAnnotationStyleITDMethods(itdms,false); 600 this.declaredITDMethods = new InterTypeMethodDeclaration[itdms.size()]; 601 itdms.toArray(this.declaredITDMethods); 602 } 603 return this.declaredITDMethods; 604 } 605 606 609 public InterTypeMethodDeclaration getITDMethod(String name, AjType<?> target, 610 AjType<?>... parameterTypes) 611 throws NoSuchMethodException { 612 InterTypeMethodDeclaration[] itdms = getITDMethods(); 613 outer: for (InterTypeMethodDeclaration itdm : itdms) { 614 try { 615 if (!itdm.getName().equals(name)) continue; 616 AjType<?> itdTarget = itdm.getTargetType(); 617 if (itdTarget.equals(target)) { 618 AjType<?>[] ptypes = itdm.getParameterTypes(); 619 if (ptypes.length == parameterTypes.length) { 620 for (int i = 0; i < ptypes.length; i++) { 621 if (!ptypes[i].equals(parameterTypes[i])) 622 continue outer; 623 } 624 return itdm; 625 } 626 } 627 } catch (ClassNotFoundException cnf) { 628 } 630 } 631 throw new NoSuchMethodException (name); 632 } 633 634 637 public InterTypeMethodDeclaration[] getITDMethods() { 638 if (this.itdMethods == null) { 639 List <InterTypeMethodDeclaration> itdms = new ArrayList <InterTypeMethodDeclaration>(); 640 Method [] baseMethods = clazz.getDeclaredMethods(); 641 for (Method m : baseMethods) { 642 if (!m.getName().contains("ajc$interMethod$")) continue; 643 if (m.isAnnotationPresent(ajcITD.class)) { 644 ajcITD ann = m.getAnnotation(ajcITD.class); 645 if (!Modifier.isPublic(ann.modifiers())) continue; 646 InterTypeMethodDeclaration itdm = 647 new InterTypeMethodDeclarationImpl( 648 this,ann.targetType(),ann.modifiers(), 649 ann.name(),m); 650 itdms.add(itdm); 651 } 652 } 653 addAnnotationStyleITDMethods(itdms,true); 654 this.itdMethods = new InterTypeMethodDeclaration[itdms.size()]; 655 itdms.toArray(this.itdMethods); 656 } 657 return this.itdMethods; 658 } 659 660 private void addAnnotationStyleITDMethods(List <InterTypeMethodDeclaration> toList, boolean publicOnly) { 661 if (isAspect()) { 662 for (Field f : clazz.getDeclaredFields()) { 663 if (!f.getType().isInterface()) continue; 664 if (f.isAnnotationPresent(org.aspectj.lang.annotation.DeclareParents.class)) { 665 Class <org.aspectj.lang.annotation.DeclareParents> decPAnnClass = org.aspectj.lang.annotation.DeclareParents.class; 666 org.aspectj.lang.annotation.DeclareParents decPAnn = f.getAnnotation(decPAnnClass); 667 if (decPAnn.defaultImpl() == decPAnnClass) continue; for (Method itdM : f.getType().getDeclaredMethods()) { 669 if (!Modifier.isPublic(itdM.getModifiers()) && publicOnly) continue; 670 InterTypeMethodDeclaration itdm = new InterTypeMethodDeclarationImpl( 671 this, AjTypeSystem.getAjType(f.getType()), itdM, 672 Modifier.PUBLIC 673 ); 674 toList.add(itdm); 675 } 676 } 677 } 678 } 679 } 680 681 private void addAnnotationStyleITDFields(List <InterTypeFieldDeclaration> toList, boolean publicOnly) { 682 return; 685 } 686 687 690 public InterTypeConstructorDeclaration getDeclaredITDConstructor( 691 AjType<?> target, AjType<?>... parameterTypes) throws NoSuchMethodException { 692 InterTypeConstructorDeclaration[] itdcs = getDeclaredITDConstructors(); 693 outer: for (InterTypeConstructorDeclaration itdc : itdcs) { 694 try { 695 AjType<?> itdTarget = itdc.getTargetType(); 696 if (itdTarget.equals(target)) { 697 AjType<?>[] ptypes = itdc.getParameterTypes(); 698 if (ptypes.length == parameterTypes.length) { 699 for (int i = 0; i < ptypes.length; i++) { 700 if (!ptypes[i].equals(parameterTypes[i])) 701 continue outer; 702 } 703 return itdc; 704 } 705 } 706 } catch (ClassNotFoundException cnf) { 707 } 709 } 710 throw new NoSuchMethodException (); 711 } 712 713 716 public InterTypeConstructorDeclaration[] getDeclaredITDConstructors() { 717 if (this.declaredITDCons == null) { 718 List <InterTypeConstructorDeclaration> itdcs = new ArrayList <InterTypeConstructorDeclaration>(); 719 Method [] baseMethods = clazz.getDeclaredMethods(); 720 for (Method m : baseMethods) { 721 if (!m.getName().contains("ajc$postInterConstructor")) continue; 722 if (m.isAnnotationPresent(ajcITD.class)) { 723 ajcITD ann = m.getAnnotation(ajcITD.class); 724 InterTypeConstructorDeclaration itdc = 725 new InterTypeConstructorDeclarationImpl(this,ann.targetType(),ann.modifiers(),m); 726 itdcs.add(itdc); 727 } 728 } 729 this.declaredITDCons = new InterTypeConstructorDeclaration[itdcs.size()]; 730 itdcs.toArray(this.declaredITDCons); 731 } 732 return this.declaredITDCons; 733 } 734 735 738 public InterTypeConstructorDeclaration getITDConstructor(AjType<?> target, 739 AjType<?>... parameterTypes) throws NoSuchMethodException { 740 InterTypeConstructorDeclaration[] itdcs = getITDConstructors(); 741 outer: for (InterTypeConstructorDeclaration itdc : itdcs) { 742 try { 743 AjType<?> itdTarget = itdc.getTargetType(); 744 if (itdTarget.equals(target)) { 745 AjType<?>[] ptypes = itdc.getParameterTypes(); 746 if (ptypes.length == parameterTypes.length) { 747 for (int i = 0; i < ptypes.length; i++) { 748 if (!ptypes[i].equals(parameterTypes[i])) 749 continue outer; 750 } 751 return itdc; 752 } 753 } 754 } catch (ClassNotFoundException cnf) { 755 } 757 } 758 throw new NoSuchMethodException (); 759 } 760 761 764 public InterTypeConstructorDeclaration[] getITDConstructors() { 765 if (this.itdCons == null) { 766 List <InterTypeConstructorDeclaration> itdcs = new ArrayList <InterTypeConstructorDeclaration>(); 767 Method [] baseMethods = clazz.getMethods(); 768 for (Method m : baseMethods) { 769 if (!m.getName().contains("ajc$postInterConstructor")) continue; 770 if (m.isAnnotationPresent(ajcITD.class)) { 771 ajcITD ann = m.getAnnotation(ajcITD.class); 772 if (!Modifier.isPublic(ann.modifiers())) continue; 773 InterTypeConstructorDeclaration itdc = 774 new InterTypeConstructorDeclarationImpl(this,ann.targetType(),ann.modifiers(),m); 775 itdcs.add(itdc); 776 } 777 } 778 this.itdCons = new InterTypeConstructorDeclaration[itdcs.size()]; 779 itdcs.toArray(this.itdCons); 780 } 781 return this.itdCons; } 782 783 786 public InterTypeFieldDeclaration getDeclaredITDField(String name, 787 AjType<?> target) throws NoSuchFieldException { 788 InterTypeFieldDeclaration[] itdfs = getDeclaredITDFields(); 789 for (InterTypeFieldDeclaration itdf : itdfs) { 790 if (itdf.getName().equals(name)) { 791 try { 792 AjType<?> itdTarget = itdf.getTargetType(); 793 if (itdTarget.equals(target)) return itdf; 794 } catch (ClassNotFoundException cnfEx) { 795 } 797 } 798 } 799 throw new NoSuchFieldException (name); 800 } 801 802 805 public InterTypeFieldDeclaration[] getDeclaredITDFields() { 806 List <InterTypeFieldDeclaration> itdfs = new ArrayList <InterTypeFieldDeclaration>(); 807 if (this.declaredITDFields == null) { 808 Method [] baseMethods = clazz.getDeclaredMethods(); 809 for(Method m : baseMethods) { 810 if (m.isAnnotationPresent(ajcITD.class)) { 811 if (!m.getName().contains("ajc$interFieldInit")) continue; 812 ajcITD ann = m.getAnnotation(ajcITD.class); 813 String interFieldInitMethodName = m.getName(); 814 String interFieldGetDispatchMethodName = 815 interFieldInitMethodName.replace("FieldInit","FieldGetDispatch"); 816 try { 817 Method dispatch = clazz.getDeclaredMethod(interFieldGetDispatchMethodName, m.getParameterTypes()); 818 InterTypeFieldDeclaration itdf = new InterTypeFieldDeclarationImpl( 819 this,ann.targetType(),ann.modifiers(),ann.name(), 820 AjTypeSystem.getAjType(dispatch.getReturnType()), 821 dispatch.getGenericReturnType()); 822 itdfs.add(itdf); 823 } catch (NoSuchMethodException nsmEx) { 824 throw new IllegalStateException ("Can't find field get dispatch method for " + m.getName()); 825 } 826 } 827 } 828 addAnnotationStyleITDFields(itdfs, false); 829 this.declaredITDFields = new InterTypeFieldDeclaration[itdfs.size()]; 830 itdfs.toArray(this.declaredITDFields); 831 } 832 return this.declaredITDFields; 833 } 834 835 838 public InterTypeFieldDeclaration getITDField(String name, AjType<?> target) 839 throws NoSuchFieldException { 840 InterTypeFieldDeclaration[] itdfs = getITDFields(); 841 for (InterTypeFieldDeclaration itdf : itdfs) { 842 if (itdf.getName().equals(name)) { 843 try { 844 AjType<?> itdTarget = itdf.getTargetType(); 845 if (itdTarget.equals(target)) return itdf; 846 } catch (ClassNotFoundException cnfEx) { 847 } 849 } 850 } 851 throw new NoSuchFieldException (name); 852 } 853 854 857 public InterTypeFieldDeclaration[] getITDFields() { 858 List <InterTypeFieldDeclaration> itdfs = new ArrayList <InterTypeFieldDeclaration>(); 859 if (this.itdFields == null) { 860 Method [] baseMethods = clazz.getMethods(); 861 for(Method m : baseMethods) { 862 if (m.isAnnotationPresent(ajcITD.class)) { 863 ajcITD ann = m.getAnnotation(ajcITD.class); 864 if (!m.getName().contains("ajc$interFieldInit")) continue; 865 if (!Modifier.isPublic(ann.modifiers())) continue; 866 String interFieldInitMethodName = m.getName(); 867 String interFieldGetDispatchMethodName = 868 interFieldInitMethodName.replace("FieldInit","FieldGetDispatch"); 869 try { 870 Method dispatch = m.getDeclaringClass().getDeclaredMethod(interFieldGetDispatchMethodName, m.getParameterTypes()); 871 InterTypeFieldDeclaration itdf = new InterTypeFieldDeclarationImpl( 872 this,ann.targetType(),ann.modifiers(),ann.name(), 873 AjTypeSystem.getAjType(dispatch.getReturnType()), 874 dispatch.getGenericReturnType()); 875 itdfs.add(itdf); 876 } catch (NoSuchMethodException nsmEx) { 877 throw new IllegalStateException ("Can't find field get dispatch method for " + m.getName()); 878 } 879 } 880 } 881 addAnnotationStyleITDFields(itdfs, true); 882 this.itdFields = new InterTypeFieldDeclaration[itdfs.size()]; 883 itdfs.toArray(this.itdFields); 884 } 885 return this.itdFields; 886 } 887 888 891 public DeclareErrorOrWarning[] getDeclareErrorOrWarnings() { 892 List <DeclareErrorOrWarning> deows = new ArrayList <DeclareErrorOrWarning>(); 893 for (Field field : clazz.getDeclaredFields()) { 894 try { 895 if (field.isAnnotationPresent(DeclareWarning.class)) { 896 DeclareWarning dw = field.getAnnotation(DeclareWarning.class); 897 if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) { 898 String message = (String ) field.get(null); 899 DeclareErrorOrWarningImpl deow = new DeclareErrorOrWarningImpl(dw.value(),message,false,this); 900 deows.add(deow); 901 } 902 } else if (field.isAnnotationPresent(DeclareError.class)) { 903 DeclareError de = field.getAnnotation(DeclareError.class); 904 if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) { 905 String message = (String ) field.get(null); 906 DeclareErrorOrWarningImpl deow = new DeclareErrorOrWarningImpl(de.value(),message,true,this); 907 deows.add(deow); 908 } 909 } 910 } catch (IllegalArgumentException e) { 911 } catch (IllegalAccessException e) { 913 } 915 } 916 for (Method method : clazz.getDeclaredMethods()) { 917 if (method.isAnnotationPresent(ajcDeclareEoW.class)) { 918 ajcDeclareEoW deowAnn = method.getAnnotation(ajcDeclareEoW.class); 919 DeclareErrorOrWarning deow = new DeclareErrorOrWarningImpl(deowAnn.pointcut(),deowAnn.message(),deowAnn.isError(),this); 920 deows.add(deow); 921 } 922 } 923 DeclareErrorOrWarning[] ret = new DeclareErrorOrWarning[deows.size()]; 924 deows.toArray(ret); 925 return ret; 926 } 927 928 931 public DeclareParents[] getDeclareParents() { 932 List <DeclareParents> decps = new ArrayList <DeclareParents>(); 933 for (Method method : clazz.getDeclaredMethods()) { 934 if (method.isAnnotationPresent(ajcDeclareParents.class)) { 935 ajcDeclareParents decPAnn = method.getAnnotation(ajcDeclareParents.class); 936 DeclareParentsImpl decp = new DeclareParentsImpl( 937 decPAnn.targetTypePattern(), 938 decPAnn.parentTypes(), 939 decPAnn.isExtends(), 940 this 941 ); 942 decps.add(decp); 943 } 944 } 945 addAnnotationStyleDeclareParents(decps); 946 if (getSupertype().isAspect()) { 947 decps.addAll(Arrays.asList(getSupertype().getDeclareParents())); 948 } 949 DeclareParents[] ret = new DeclareParents[decps.size()]; 950 decps.toArray(ret); 951 return ret; 952 } 953 954 private void addAnnotationStyleDeclareParents(List <DeclareParents> toList) { 955 for (Field f : clazz.getDeclaredFields()) { 956 if (f.isAnnotationPresent(org.aspectj.lang.annotation.DeclareParents.class)) { 957 if (!f.getType().isInterface()) continue; 958 org.aspectj.lang.annotation.DeclareParents ann = f.getAnnotation(org.aspectj.lang.annotation.DeclareParents.class); 959 String parentType = f.getType().getName(); 960 DeclareParentsImpl decp = new DeclareParentsImpl( 961 ann.value(), 962 parentType, 963 false, 964 this 965 ); 966 toList.add(decp); 967 } 968 } 969 } 970 971 974 public DeclareSoft[] getDeclareSofts() { 975 List <DeclareSoft> decs = new ArrayList <DeclareSoft>(); 976 for (Method method : clazz.getDeclaredMethods()) { 977 if (method.isAnnotationPresent(ajcDeclareSoft.class)) { 978 ajcDeclareSoft decSAnn = method.getAnnotation(ajcDeclareSoft.class); 979 DeclareSoftImpl ds = new DeclareSoftImpl( 980 this, 981 decSAnn.pointcut(), 982 decSAnn.exceptionType() 983 ); 984 decs.add(ds); 985 } 986 } 987 if (getSupertype().isAspect()) { 988 decs.addAll(Arrays.asList(getSupertype().getDeclareSofts())); 989 } 990 DeclareSoft[] ret = new DeclareSoft[decs.size()]; 991 decs.toArray(ret); 992 return ret; 993 } 994 995 998 public DeclareAnnotation[] getDeclareAnnotations() { 999 List <DeclareAnnotation> decAs = new ArrayList <DeclareAnnotation>(); 1000 for (Method method : clazz.getDeclaredMethods()) { 1001 if (method.isAnnotationPresent(ajcDeclareAnnotation.class)) { 1002 ajcDeclareAnnotation decAnn = method.getAnnotation(ajcDeclareAnnotation.class); 1003 Annotation targetAnnotation = null; 1005 Annotation [] anns = method.getAnnotations(); 1006 for (Annotation ann: anns) { 1007 if (ann.annotationType() != ajcDeclareAnnotation.class) { 1008 targetAnnotation = ann; 1010 break; 1011 } 1012 } 1013 DeclareAnnotationImpl da = new DeclareAnnotationImpl( 1014 this, 1015 decAnn.kind(), 1016 decAnn.pattern(), 1017 targetAnnotation, 1018 decAnn.annotation() 1019 ); 1020 decAs.add(da); 1021 } 1022 } 1023 if (getSupertype().isAspect()) { 1024 decAs.addAll(Arrays.asList(getSupertype().getDeclareAnnotations())); 1025 } 1026 DeclareAnnotation[] ret = new DeclareAnnotation[decAs.size()]; 1027 decAs.toArray(ret); 1028 return ret; 1029 } 1030 1031 1034 public DeclarePrecedence[] getDeclarePrecedence() { 1035 List <DeclarePrecedence> decps = new ArrayList <DeclarePrecedence>(); 1036 1037 if (clazz.isAnnotationPresent(org.aspectj.lang.annotation.DeclarePrecedence.class)) { 1039 org.aspectj.lang.annotation.DeclarePrecedence ann = 1040 clazz.getAnnotation(org.aspectj.lang.annotation.DeclarePrecedence.class); 1041 DeclarePrecedenceImpl decp = new DeclarePrecedenceImpl( 1042 ann.value(), 1043 this 1044 ); 1045 decps.add(decp); 1046 } 1047 1048 for (Method method : clazz.getDeclaredMethods()) { 1050 if (method.isAnnotationPresent(ajcDeclarePrecedence.class)) { 1051 ajcDeclarePrecedence decPAnn = method.getAnnotation(ajcDeclarePrecedence.class); 1052 DeclarePrecedenceImpl decp = new DeclarePrecedenceImpl( 1053 decPAnn.value(), 1054 this 1055 ); 1056 decps.add(decp); 1057 } 1058 } 1059 if (getSupertype().isAspect()) { 1060 decps.addAll(Arrays.asList(getSupertype().getDeclarePrecedence())); 1061 } 1062 DeclarePrecedence[] ret = new DeclarePrecedence[decps.size()]; 1063 decps.toArray(ret); 1064 return ret; 1065 } 1066 1067 1070 public T[] getEnumConstants() { 1071 return clazz.getEnumConstants(); 1072 } 1073 1074 1077 public TypeVariable <Class <T>>[] getTypeParameters() { 1078 return clazz.getTypeParameters(); 1079 } 1080 1081 1084 public boolean isEnum() { 1085 return clazz.isEnum(); 1086 } 1087 1088 1091 public boolean isInstance(Object o) { 1092 return clazz.isInstance(o); 1093 } 1094 1095 1098 public boolean isInterface() { 1099 return clazz.isInterface(); 1100 } 1101 1102 1105 public boolean isLocalClass() { 1106 return clazz.isLocalClass() && !isAspect(); 1107 } 1108 1109 1112 public boolean isMemberClass() { 1113 return clazz.isMemberClass() && !isAspect(); 1114 } 1115 1116 1119 public boolean isArray() { 1120 return clazz.isArray(); 1121 } 1122 1123 1126 public boolean isPrimitive() { 1127 return clazz.isPrimitive(); 1128 } 1129 1130 1133 public boolean isAspect() { 1134 return clazz.getAnnotation(Aspect.class) != null; 1135 } 1136 1137 1140 public boolean isMemberAspect() { 1141 return clazz.isMemberClass() && isAspect(); 1142 } 1143 1144 public boolean isPrivileged() { 1145 return isAspect() && clazz.isAnnotationPresent(ajcPrivileged.class); 1146 } 1147 1148 @Override 1149 public boolean equals(Object obj) { 1150 if (!(obj instanceof AjTypeImpl)) return false; 1151 AjTypeImpl other = (AjTypeImpl) obj; 1152 return other.clazz.equals(clazz); 1153 } 1154 1155 @Override 1156 public int hashCode() { 1157 return clazz.hashCode(); 1158 } 1159 1160 private AjType<?>[] toAjTypeArray(Class <?>[] classes) { 1161 AjType<?>[] ajtypes = new AjType<?>[classes.length]; 1162 for (int i = 0; i < ajtypes.length; i++) { 1163 ajtypes[i] = AjTypeSystem.getAjType(classes[i]); 1164 } 1165 return ajtypes; 1166 } 1167 1168 private Class <?>[] toClassArray(AjType<?>[] ajTypes) { 1169 Class <?>[] classes = new Class <?>[ajTypes.length]; 1170 for (int i = 0; i < classes.length; i++) { 1171 classes[i] = ajTypes[i].getJavaClass(); 1172 } 1173 return classes; 1174 } 1175 1176 public String toString() { return getName(); } 1177 1178} 1179 | Popular Tags |