1 22 package org.aspectj.tools.ajdoc; 23 24 import org.aspectj.ajdoc.IntroducedSuperDoc; 25 import org.aspectj.ajdoc.PointcutDoc; 26 import org.aspectj.compiler.base.ast.ClassDec; 27 import org.aspectj.compiler.base.ast.CodeDec; 28 import org.aspectj.compiler.base.ast.CompilationUnit; 29 import org.aspectj.compiler.base.ast.Constructor; 30 import org.aspectj.compiler.base.ast.ConstructorDec; 31 import org.aspectj.compiler.base.ast.Dec; 32 import org.aspectj.compiler.base.ast.Field; 33 import org.aspectj.compiler.base.ast.FieldDec; 34 import org.aspectj.compiler.base.ast.Import; 35 import org.aspectj.compiler.base.ast.Imports; 36 import org.aspectj.compiler.base.ast.InterfaceDec; 37 import org.aspectj.compiler.base.ast.Method; 38 import org.aspectj.compiler.base.ast.NameType; 39 import org.aspectj.compiler.base.ast.SourceLocation; 40 import org.aspectj.compiler.base.ast.TextSourceLocation; 41 import org.aspectj.compiler.base.ast.Type; 42 import org.aspectj.compiler.base.ast.TypeDec; 43 import org.aspectj.compiler.crosscuts.AspectJCompiler; 44 import org.aspectj.compiler.crosscuts.ast.AspectDec; 45 import org.aspectj.compiler.crosscuts.ast.IntroducedSuperDec; 46 import org.aspectj.compiler.crosscuts.ast.PointcutSO; 47 48 import org.aspectj.ajdoc.ClassDoc; 49 import com.sun.javadoc.ConstructorDoc; 50 import com.sun.javadoc.FieldDoc; 51 import com.sun.javadoc.MethodDoc; 52 import com.sun.javadoc.PackageDoc; 53 54 import java.lang.reflect.Modifier ; 55 import java.util.ArrayList ; 56 import java.util.Collection ; 57 import java.util.Collections ; 58 import java.util.HashMap ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 import java.util.Map ; 62 import java.util.Set ; 63 64 72 public class ClassDocImpl 73 extends ProgramElementDocImpl 74 implements org.aspectj.ajdoc.ClassDoc { 75 76 85 public final static ClassDocImpl getInstance(TypeDec typeDec) { 86 return factory.getInstance(typeDec); 87 } 88 89 99 public final static ClassDocImpl getInstance(ClassDoc outerDoc, TypeDec typeDec) { 100 return factory.getInstance(outerDoc, typeDec); 101 } 102 103 110 public final static ClassDocImpl find(String qualifiedName) { 111 return factory.find(qualifiedName); 112 } 113 114 115 private final static Factory factory = new Factory(); 116 117 118 private final TypeDec typeDec; 119 120 122 private Collection introducers; 123 124 125 private Collection fieldDocs; 126 127 128 private Collection methodDocs; 129 130 133 private Collection constructorDocs; 134 135 136 private Collection innerclassDocs; 137 138 139 private Collection interfaceDocs; 140 141 142 private Collection importedClasses; 143 144 145 private Collection importedPackages; 146 147 148 private Collection pointcutDocs; 149 150 151 private AccessChecker filter; 152 153 165 protected ClassDocImpl(com.sun.javadoc.ClassDoc containingClass, 166 TypeDec typeDec) { 167 super(containingClass); 168 this.typeDec = typeDec; 169 setIncluded(false); 172 factory.put(this, typeDec); 174 createImports(importedClasses = new ArrayList (), 175 importedPackages = new ArrayList ()); 176 } 177 178 184 public MemberDocImpl docForDec(Dec dec) { 185 if (dec instanceof FieldDec) { 186 return docForDec((FieldDec)dec); 187 } 188 if (dec instanceof CodeDec) { 189 return docForDec((CodeDec)dec); 190 } 191 return null; } 194 195 200 public FieldDocImpl docForDec(FieldDec dec) { 201 FieldDoc[] fs = fields(); 202 for (int i = 0; i < fs.length; i++) { 203 FieldDocImpl fd = (FieldDocImpl)fs[i]; 204 if (fd.dec() == dec) return fd; 205 } 206 return null; 207 } 208 209 214 public CodeDocImpl docForDec(CodeDec dec) { 215 MethodDoc[] ms = methods(); 216 for (int i = 0; i < ms.length; i++) { 217 CodeDocImpl cd = (CodeDocImpl)ms[i]; 218 if (cd.dec() == dec) return cd; 219 } 220 ConstructorDoc[] cs = constructors(); 221 for (int i = 0; i < cs.length; i++) { 222 CodeDocImpl cd = (CodeDocImpl)cs[i]; 223 if (cd.dec() == dec) return cd; 224 } 225 return null; 226 } 227 228 231 public TypeDec nonNullTypeDec() { 232 if (typeDec().getLexicalType() == null) return typeDec(); 233 return super.nonNullTypeDec(); 234 } 235 236 243 private Collection createInnerTypes() { 244 Collection items = ((NameType)typeDec.getType()).getInnerTypes(); 245 FilteredDecList result = 246 new FilteredDecList(getFilter(), this); 247 if (items != null) { 248 for (Iterator i = items.iterator(); i.hasNext();) { 249 result.add(((NameType)i.next()).getTypeDec()); 250 } 251 } 252 Collections.sort(result); 253 return result; 254 } 255 256 263 private void createImports(final Collection importClasses, 264 final Collection importPkgs) { 265 CompilationUnit cu = typeDec.getCompilationUnit(); 266 if (cu != null) { 267 Imports imports = cu.getImports(); 268 if (imports != null) { 269 for (int i = 0; i < imports.size(); i++) { 270 Import imprt = imports.get(i); 271 if (imprt != null) { 272 if (imprt.getStar()) { 273 PackageDoc importedPkg = 274 PackageDocImpl.getPackageDoc 275 (imprt.getName()); 276 if (importedPkg != null) { 277 importPkgs.add(importedPkg); 278 } 279 } else { 280 com.sun.javadoc.ClassDoc importedClass = 281 findClass(imprt.getName()); 282 if (importedClass != null) { 283 importClasses.add(importedClass); 284 } 285 } 286 } 287 } 288 } 289 } 290 } 291 292 299 private Collection createInterfaces() { 300 Collection items = typeDec.getSuperInterfaceTypes(); 302 FilteredDecList result = 303 new FilteredDecList(getFilter(), this); 304 if (items != null) { 305 for (Iterator i = items.iterator(); i.hasNext();) { 306 result.add(((NameType)i.next()).getTypeDec()); 307 } 308 } 309 Collections.sort(result); 310 return result; 311 } 312 313 319 void postProcess() { 320 final SourceLocation parentLoc = dec().getSourceLocation(); 323 final int parentLine = (null == parentLoc ? -1 : parentLoc.getBeginLine()); 324 final int parentColumn = (null == parentLoc ? -1 : parentLoc.getBeginColumn()); 325 if (null == constructorDocs) { 326 if (null == constructors()) { 327 return; 329 } 330 } 331 try { 332 for (Iterator i = constructorDocs.iterator(); i.hasNext(); ) { 334 ConstructorDocImpl cdi = (ConstructorDocImpl) i.next(); 335 CodeDec cd = cdi.codeDec(); 336 SourceLocation sl = (null == cd ? null : cd.getSourceLocation()); 337 if ((null != sl) 339 && (parentColumn == sl.getBeginColumn()) 340 && (parentLine == sl.getBeginLine())) { 341 Object [] advice = cdi.advice(); 342 if ((null != advice) && (1 > advice.length)) { 343 i.remove(); 344 } else { 347 } 349 } else { 350 } 352 } 353 } catch (UnsupportedOperationException e) { 360 System.err.println("Warning: ClassDocImpl.constructorDocs not removable"); 361 } 362 } 363 364 371 private Collection createConstructors() { 372 NameType type = (NameType)typeDec.getType(); 373 Collection items = type.getConstructors(); 374 final SourceLocation parentLoc = dec().getSourceLocation(); 375 final int parentLine = (null == parentLoc ? -1 : parentLoc.getBeginLine()); 376 final int parentColumn = (null == parentLoc ? -1 : parentLoc.getBeginColumn()); 377 FilteredDecList result = 378 new FilteredDecList(getFilter(), this); 379 if (items != null) { 380 for (Iterator i = items.iterator(); i.hasNext();) { 381 Constructor c = (Constructor) i.next(); 382 ConstructorDec cd = c.getConstructorDec(); 383 ConstructorDocImpl impl = new ConstructorDocImpl(this, cd); 384 SourceLocation sl = (null == cd ? null : cd.getSourceLocation()); 386 if ((null != sl) 388 && (parentColumn == sl.getBeginColumn()) 389 && (parentLine == sl.getBeginLine())) { 390 TextSourceLocation tsl = new TextSourceLocation(sl.getCompilationUnit(), 392 sl.getStartPosition(), sl.getEndPosition()); 393 tsl.clearComment(); 394 cd.setSourceLocation(tsl); 395 } 396 result.add(impl); 397 } 398 } 399 Collections.sort(result); 400 return result; 401 } 402 403 410 private Collection createPointcuts() { 411 NameType type = (NameType)typeDec.getType(); 412 Collection items = type.getPointcuts(); 413 FilteredDecList result = 414 new FilteredDecList(getFilter(), this); 415 if (items != null) { 416 for (Iterator i = items.iterator(); i.hasNext();) { 417 result.add(((PointcutSO)i.next()).getPointcutDec()); 418 } 419 } 420 Collections.sort(result); 421 return result; 422 } 423 424 431 private Collection createMethods() { 432 NameType type = (NameType)typeDec.getType(); 433 Collection methods = type.getMethods(); 434 FilteredDecList result = 435 new FilteredDecList(getFilter(), this); 436 if (methods != null) { 437 for (Iterator i = methods.iterator(); i.hasNext();) { 438 result.add(((Method)i.next()).getMethodDec()); 439 } 440 } 441 Collections.sort(result); 442 return result; 443 } 444 445 452 private Collection createFields() { 453 NameType type = (NameType)typeDec.getType(); 454 Collection fields = type.getFields(); 455 FilteredDecList result = 456 new FilteredDecList(getFilter(), this); 457 if (fields != null) { 458 for (Iterator i = fields.iterator(); i.hasNext();) { 459 result.add(((Field)i.next()).getFieldDec()); 460 } 461 } 462 Collections.sort(result); 463 return result; 464 } 465 466 467 protected AccessChecker getFilter() { 468 if (null == filter) { 470 AspectJCompiler ajc = ajc(); 471 if (ajc instanceof AjdocCompiler) { 472 filter = ((AjdocCompiler) ajc).getFilter(); 473 } 474 } 475 return filter; } 477 478 483 protected final Dec dec() { 484 return typeDec(); 485 } 486 487 492 public final TypeDec typeDec() { 493 return typeDec; 494 } 495 496 503 public String qualifiedName() { 504 return qualifiedTypeName().replace('$','.'); 505 } 506 507 512 public String toString() { 513 return qualifiedName(); 514 } 515 516 524 public String name() { 525 return ((NameType)typeDec().getType()). 526 getExtendedId().replace('$','.'); 527 } 528 529 549 public com.sun.javadoc.ClassDoc findClass(String classname) { 550 if (classname == null || classname.length() < 1) { 552 return null; 553 } 554 555 com.sun.javadoc.ClassDoc desired; 557 558 if (classname.equals(name()) || 560 classname.equals(qualifiedName())) { 561 return this; 562 } 563 564 if ((desired = ClassDocImpl.find(classname)) != null) { 566 return desired; 567 } 568 569 String innername = name() + '.' + classname; 589 com.sun.javadoc.ClassDoc[] inners = innerClasses(); 590 if (inners != null) { 591 for (int i = 0; i < inners.length; i++) { 592 if (classname.equals(inners[i].name()) || 593 innername.equals(inners[i].name())) { 594 return inners[i]; 595 } 596 } 597 } 598 599 if ((desired = containingPackage().findClass(classname)) != null) { 601 return desired; 602 } 603 604 com.sun.javadoc.ClassDoc[] imports = importedClasses(); 611 if (imports != null) { 612 for (int i = 0; i < imports.length; i++) { 613 if (classname.equals(imports[i].name())) { 614 return imports[i]; 615 } 616 } 617 } 618 619 PackageDoc[] pkgs = importedPackages(); 621 if (pkgs != null) { 622 for (int i = 0; i < pkgs.length; i++) { 623 if ((desired = pkgs[i].findClass(classname)) != null) { 624 return desired; 625 } 626 } 627 } 628 629 String [] pkgnames = {"java.lang", "org.aspectj.lang"}; 635 for (int i = 0; i < pkgnames.length; i++) { 636 PackageDoc pkg = PackageDocImpl.getPackageDoc(pkgnames[i]); 637 if (pkg != null) { 638 if ((desired = pkg.findClass(classname)) != null) { 639 return desired; 640 } 641 } 642 } 643 return null; 645 } 646 647 653 public FieldDoc[] fields() { 654 if (fieldDocs == null) { 655 fieldDocs = createFields(); 656 } 657 return (FieldDoc[])fieldDocs.toArray 658 (new org.aspectj.ajdoc.FieldDoc[fieldDocs.size()]); 659 } 660 661 666 public boolean isExternalizable() { 667 return false; } 669 670 675 public boolean isSerializable() { 676 for (Iterator i = typeDec().getSuperInterfaceTypes().iterator(); 677 i.hasNext();) { 678 if (((Type)i.next()).getId().equals("java.io.Serializable")) { 679 return true; 680 } 681 } 682 return false; 683 } 684 685 691 public MethodDoc[] methods() { 692 if (methodDocs == null) { 693 methodDocs = createMethods(); 694 } 695 return (MethodDoc[])methodDocs.toArray 696 (new org.aspectj.ajdoc.MethodDoc[methodDocs.size()]); 697 } 698 699 705 public MethodDoc[] serializationMethods() { 706 List ser = new ArrayList (); 707 MethodDoc[] mds = methods(); 708 for (int i = 0, N = mds.length; i < N; i++) { 709 if (mds[i].tags("@serialized").length > 1) ser.add(mds[i]); 710 } 711 return (MethodDoc[])ser.toArray(new MethodDoc[ser.size()]); 712 } 713 714 720 public FieldDoc[] serializableFields() { 721 List ser = new ArrayList (); 722 FieldDoc[] fds = fields(); 723 for (int i = 0, N = fds.length; i < N; i++) { 724 if (fds[i].serialFieldTags().length > 1) ser.add(fds[i]); 725 } 726 return (FieldDoc[])ser.toArray(new FieldDoc[ser.size()]); 727 } 728 729 736 public boolean definesSerializableFields() { 737 return serializableFields().length > 0; 738 } 739 740 748 public com.sun.javadoc.ClassDoc superclass() { 749 if ("java.lang.Object".equals(qualifiedTypeName())) { 750 return null; 751 } else { 752 TypeDec superType = typeDec().getSuperClassType().getTypeDec(); 753 return ClassDocImpl.getInstance(superType); 754 } 755 } 756 757 764 public boolean subclassOf(com.sun.javadoc.ClassDoc c) { 765 return c != null && c.equals(superclass()); 766 } 767 768 774 public com.sun.javadoc.ClassDoc[] interfaces() { 775 if (interfaceDocs == null) { 776 interfaceDocs = createInterfaces(); 777 } 778 return (ClassDoc[])interfaceDocs.toArray 779 (new org.aspectj.ajdoc.ClassDoc[interfaceDocs.size()]); 780 } 781 782 788 public ConstructorDoc[] constructors() { 789 if (constructorDocs == null) { 790 constructorDocs = createConstructors(); 791 } 792 return (ConstructorDoc[])constructorDocs.toArray 793 (new org.aspectj.ajdoc.ConstructorDoc[constructorDocs.size()]); 794 } 795 796 802 public com.sun.javadoc.ClassDoc[] innerClasses() { 803 if (innerclassDocs == null) { 804 innerclassDocs = createInnerTypes(); 805 } 806 final int size = innerclassDocs.size(); 807 return (ClassDoc[])innerclassDocs.toArray 808 (new org.aspectj.ajdoc.ClassDoc[size]); 809 } 810 811 817 public com.sun.javadoc.ClassDoc[] importedClasses() { 818 return (ClassDoc[])importedClasses.toArray 819 (new org.aspectj.ajdoc.ClassDoc[importedClasses.size()]); 820 } 821 822 828 public PackageDoc[] importedPackages() { 829 return (PackageDoc[])importedPackages.toArray 830 (new org.aspectj.ajdoc.PackageDoc[importedPackages.size()]); 831 } 832 833 839 public PointcutDoc[] pointcuts() { 840 if (pointcutDocs == null) { 841 pointcutDocs = createPointcuts(); 842 } 843 return (PointcutDoc[])pointcutDocs.toArray 844 (new PointcutDoc[pointcutDocs.size()]); 845 } 846 847 852 public boolean isAbstract() { 853 return typeDec().isAbstract(); 854 } 855 856 862 public boolean isException() { 863 for (com.sun.javadoc.ClassDoc superclass = superclass(); 865 superclass != null && 866 !superclass.qualifiedTypeName().equals("java.lang.Object"); 867 superclass = superclass.superclass()) { 868 if (superclass.qualifiedTypeName().equals("java.lang.Exception")) { 869 return true; 870 } 871 } 872 return false; 873 } 874 875 881 public boolean isError() { 882 for (com.sun.javadoc.ClassDoc superclass = superclass(); 884 superclass != null && 885 !superclass.qualifiedTypeName().equals("java.lang.Object"); 886 superclass = superclass.superclass()) { 887 if (superclass.qualifiedTypeName().equals("java.lang.Error")) { 888 return true; 889 } 890 } 891 return false; 892 } 893 894 902 public IntroducedSuperDoc[] introducers() { 903 if (introducers == null) { 904 introducers = createIntroducers(); 905 } 906 return (IntroducedSuperDoc[])introducers.toArray 907 (new IntroducedSuperDoc[introducers.size()]); 908 } 909 910 915 public boolean isInterface() { 916 return typeDec() instanceof InterfaceDec; 917 } 918 919 924 public boolean isClass() { 925 return typeDec() instanceof ClassDec; 926 } 927 928 933 public boolean isAspect() { 934 return typeDec() instanceof AspectDec; 935 } 936 937 946 public boolean isOrdinaryClass() { 947 return isClass() && !(isError() || isException()); 948 } 949 950 956 public int modifierSpecifier() { 957 return super.modifierSpecifier() 958 | (isInterface() ? Modifier.INTERFACE : 0); 959 } 960 961 962 966 967 974 public com.sun.javadoc.ClassDoc asClassDoc() { 975 return isIncluded() ? this : null; 976 } 977 978 983 public String dimension() { 984 return ""; 985 } 986 987 994 public String qualifiedTypeName() { 995 return typeDec().getFullName().replace('$', '.'); 996 } 997 998 1005 public String typeName() { 1006 return typeDec().getId().replace('$', '.'); 1007 } 1008 1009 1018 private Collection createIntroducers() { 1019 Set affectedBy = ajc().getCorrespondences().getAffectedBy(typeDec); 1020 if (affectedBy.size() < 1) { 1021 return Collections.EMPTY_LIST; 1022 } 1023 Collection list = new ArrayList (); 1024 for (Iterator i = affectedBy.iterator(); i.hasNext();) { 1025 Object o = i.next(); 1026 if (o instanceof IntroducedSuperDec) { 1027 IntroducedSuperDec dec = (IntroducedSuperDec)o; 1028 TypeDec owner = ((NameType)dec.getDeclaringType()).getTypeDec(); 1029 AspectDocImpl ad = (AspectDocImpl)ClassDocImpl.getInstance(owner); 1030 IntroducedSuperDocImpl id = (IntroducedSuperDocImpl)ad.introDocForDec(dec); 1031 list.add(id); 1032 id.addTarget(this); 1033 1034 } 1035 } 1036 return list; 1037 } 1038 1039 1040 1044 1045 1048 private final static class Factory { 1049 1050 private final Map typeDecsToClassDocs = new HashMap (); 1051 private final Map qualifiedNamesToClassDocs = new HashMap (); 1052 1053 public final ClassDocImpl find(String qualifiedName) { 1054 return (ClassDocImpl)qualifiedNamesToClassDocs.get(qualifiedName); 1055 } 1056 1057 public final ClassDocImpl getInstance(TypeDec typeDec) { 1058 if (typeDec == null) return null; 1059 ClassDocImpl outerDoc = getInstance(typeDec.getEnclosingTypeDec()); 1060 1061 return getInstance(outerDoc, typeDec); 1062 } 1063 public final ClassDocImpl getInstance(ClassDoc outerDoc, 1064 TypeDec typeDec) { 1065 if (typeDec == null) return null; 1066 ClassDocImpl cd = (ClassDocImpl)typeDecsToClassDocs.get(typeDec); 1067 if (cd == null) { 1068 cd = makeInstance(outerDoc, typeDec); 1069 1075 } 1076 return cd; 1077 } 1078 private final ClassDocImpl makeInstance(ClassDoc outerDoc, 1079 TypeDec typeDec) { 1080 ClassDocImpl result = null; 1081 if (typeDec instanceof AspectDec) { 1082 result = new AspectDocImpl(outerDoc, (AspectDec)typeDec); 1083 } else { 1084 result = new ClassDocImpl(outerDoc, typeDec); 1085 } 1086 if (null == result.containingPackage()) { 1087 System.err.println("Warning: unable to add " 1088 + result + " to package"); 1089 } 1090 return result; 1091 } 1092 1093 1099 private final Object put(ClassDocImpl classdoc, TypeDec typeDec) { 1100 Object result = typeDecsToClassDocs.put(typeDec, classdoc); 1101 if (null == result) { 1102 result = qualifiedNamesToClassDocs.put(classdoc.qualifiedName(), classdoc); 1103 } 1104 return result; 1105 } 1106 } } 1108 | Popular Tags |