1 7 8 package com.sun.mirror.util; 9 10 import com.sun.mirror.declaration.*; 11 12 import java.util.SortedSet ; 13 import java.util.TreeSet ; 14 15 32 class SourceOrderDeclScanner extends DeclarationScanner { 33 static class SourceOrderComparator implements java.util.Comparator <Declaration> { 34 SourceOrderComparator(){} 35 36 37 static boolean equals(Declaration d1, Declaration d2) { 38 return d1 == d2 || (d1 != null && d1.equals(d2)); 39 } 40 41 private static class DeclPartialOrder extends com.sun.mirror.util.SimpleDeclarationVisitor { 42 private int value = 1000; 43 private static int staticAdjust(Declaration d) { 44 return d.getModifiers().contains(Modifier.STATIC)?0:1; 45 } 46 47 DeclPartialOrder() {} 48 49 public int getValue() { return value; } 50 51 @Override 52 public void visitTypeParameterDeclaration(TypeParameterDeclaration d) {value = 0;} 53 54 @Override 55 public void visitEnumConstantDeclaration(EnumConstantDeclaration d) {value = 1;} 56 57 @Override 58 public void visitClassDeclaration(ClassDeclaration d) {value = 2 + staticAdjust(d);} 59 60 @Override 61 public void visitInterfaceDeclaration(InterfaceDeclaration d) {value = 4;} 62 63 @Override 64 public void visitEnumDeclaration(EnumDeclaration d) {value = 6;} 65 66 @Override 67 public void visitAnnotationTypeDeclaration(AnnotationTypeDeclaration d) {value = 8;} 68 69 @Override 70 public void visitFieldDeclaration(FieldDeclaration d) {value = 10 + staticAdjust(d);} 71 72 @Override 73 public void visitConstructorDeclaration(ConstructorDeclaration d) {value = 12;} 74 75 @Override 76 public void visitMethodDeclaration(MethodDeclaration d) {value = 14 + staticAdjust(d);} 77 } 78 79 private int compareEqualPosition(Declaration d1, Declaration d2) { 80 assert d1.getPosition() == d2.getPosition(); 81 82 DeclPartialOrder dpo1 = new DeclPartialOrder(); 83 DeclPartialOrder dpo2 = new DeclPartialOrder(); 84 85 d1.accept(dpo1); 86 d2.accept(dpo2); 87 88 int difference = dpo1.getValue() - dpo2.getValue(); 89 if (difference != 0) 90 return difference; 91 else { 92 int result = d1.getSimpleName().compareTo(d2.getSimpleName()); 93 if (result != 0) 94 return result; 95 return (int)( Long.signum((long)System.identityHashCode(d1) - 96 (long)System.identityHashCode(d2))); 97 } 98 } 99 100 public int compare(Declaration d1, Declaration d2) { 101 if (equals(d1, d2)) 102 return 0; 103 104 SourcePosition p1 = d1.getPosition(); 105 SourcePosition p2 = d2.getPosition(); 106 107 if (p1 == null && p2 != null) 108 return 1; 109 else if (p1 != null && p2 == null) 110 return -1; 111 else if(p1 == null && p2 == null) 112 return compareEqualPosition(d1, d2); 113 else { 114 assert p1 != null && p2 != null; 115 int fileComp = p1.file().compareTo(p2.file()) ; 116 if (fileComp == 0) { 117 long diff = (long)p1.line() - (long)p2.line(); 118 if (diff == 0) { 119 diff = Long.signum((long)p1.column() - (long)p2.column()); 120 if (diff != 0) 121 return (int)diff; 122 else { 123 return compareEqualPosition(d1, d2); 127 } 128 } else 129 return (diff<0)? -1:1; 130 } else 131 return fileComp; 132 } 133 } 134 } 135 136 final static java.util.Comparator <Declaration> comparator = new SourceOrderComparator(); 137 138 SourceOrderDeclScanner(DeclarationVisitor pre, DeclarationVisitor post) { 139 super(pre, post); 140 } 141 142 147 public void visitTypeDeclaration(TypeDeclaration d) { 148 d.accept(pre); 149 150 SortedSet <Declaration> decls = new 151 TreeSet <Declaration>(SourceOrderDeclScanner.comparator) ; 152 153 for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) { 154 decls.add(tpDecl); 155 } 156 157 for(FieldDeclaration fieldDecl: d.getFields()) { 158 decls.add(fieldDecl); 159 } 160 161 for(MethodDeclaration methodDecl: d.getMethods()) { 162 decls.add(methodDecl); 163 } 164 165 for(TypeDeclaration typeDecl: d.getNestedTypes()) { 166 decls.add(typeDecl); 167 } 168 169 for(Declaration decl: decls ) 170 decl.accept(this); 171 172 d.accept(post); 173 } 174 175 180 public void visitClassDeclaration(ClassDeclaration d) { 181 d.accept(pre); 182 183 SortedSet <Declaration> decls = new 184 TreeSet <Declaration>(SourceOrderDeclScanner.comparator) ; 185 186 for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) { 187 decls.add(tpDecl); 188 } 189 190 for(FieldDeclaration fieldDecl: d.getFields()) { 191 decls.add(fieldDecl); 192 } 193 194 for(MethodDeclaration methodDecl: d.getMethods()) { 195 decls.add(methodDecl); 196 } 197 198 for(TypeDeclaration typeDecl: d.getNestedTypes()) { 199 decls.add(typeDecl); 200 } 201 202 for(ConstructorDeclaration ctorDecl: d.getConstructors()) { 203 decls.add(ctorDecl); 204 } 205 206 for(Declaration decl: decls ) 207 decl.accept(this); 208 209 d.accept(post); 210 } 211 212 public void visitExecutableDeclaration(ExecutableDeclaration d) { 213 d.accept(pre); 214 215 SortedSet <Declaration> decls = new 216 TreeSet <Declaration>(SourceOrderDeclScanner.comparator) ; 217 218 for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) 219 decls.add(tpDecl); 220 221 for(ParameterDeclaration pDecl: d.getParameters()) 222 decls.add(pDecl); 223 224 for(Declaration decl: decls ) 225 decl.accept(this); 226 227 d.accept(post); 228 } 229 230 } 231 | Popular Tags |