1 24 25 package org.aspectj.compiler.crosscuts; 26 27 import org.aspectj.compiler.base.*; 28 import org.aspectj.compiler.base.ast.*; 29 import org.aspectj.compiler.crosscuts.ast.*; 30 import org.aspectj.util.CollectionUtil; 31 32 import org.aspectj.tools.ide.Declaration; 33 import org.aspectj.tools.ide.SymbolManager; 34 35 import org.aspectj.compiler.base.JavaCompiler; 36 import org.aspectj.compiler.base.CompilerObject; 37 38 39 40 import java.io.*; 41 47 48 import java.util.*; 49 50 import java.util.Vector ; 53 54 55 58 class CompilationUnitCorrespondences extends CompilerObject { 59 private CompilationUnit compilationUnit; 63 public CompilationUnitCorrespondences(CompilationUnit cu) { 64 super(cu.getCompiler()); 65 compilationUnit = cu; 66 } 67 68 70 72 74 78 private Map pointsTo = new HashMap(); 79 private Map pointedToBy = new HashMap(); 80 81 public Set getPointsToSet(ASTObject fromNode) { 82 return CollectionUtil.getSetInMap(pointsTo, fromNode); 83 } 84 85 public Set getPointedToBySet(ASTObject fromNode) { 86 return CollectionUtil.getSetInMap(pointedToBy, fromNode); 87 } 88 89 public void addPointsTo(ASTObject fromNode, ASTObject toNode) { 90 93 CollectionUtil.getSetInMap(pointsTo, fromNode).add(toNode); 94 } 95 96 public void addPointedToBy(ASTObject toNode, ASTObject fromNode) { 97 100 CollectionUtil.getSetInMap(pointedToBy, toNode).add(fromNode); 101 } 102 103 private String quoteString(String s) { 104 return "\""+s+"\" "; 105 } 106 107 private String getKind(ASTObject node) { 108 if (node instanceof TypeDec) { 109 TypeDec typeDec = (TypeDec)node; 110 if (typeDec instanceof InterfaceDec) return "interface"; 111 if (typeDec instanceof ClassDec) return "class"; 112 if (typeDec instanceof AspectDec) return "aspect"; 113 } 114 115 if (node instanceof VarDec) return "field"; 116 if (node instanceof InitializerDec) return "initializer"; 117 if (node instanceof ConstructorDec) return "constructor"; 118 if (node instanceof MethodDec) return "method"; 119 120 121 if (node instanceof IntroducedDec) return "introduction"; 123 if (node instanceof IntroducedSuperDec) return "introduction"; 124 125 127 if (node instanceof AdviceDec) return "advice"; 128 if (node instanceof PointcutDec) return "pointcut"; 129 130 return "unknown"; 132 } 133 134 private String getDeclaringType(ASTObject node) { 135 Type declaringType = node.getDeclaringType(); 136 if (declaringType == null) return null; 137 return declaringType.getTypeDec().getExtendedId().replace('$', '.'); 138 } 139 140 private String getSignature(ASTObject node) { 141 if (node instanceof TypeDec) { 142 return ((TypeDec)node).getId(); 143 } 144 145 146 150 if (node instanceof PointcutDec) { 151 PointcutDec ccDec = (PointcutDec)node; 152 String name = ccDec.getName(); 153 154 return name+ccDec.getFormals().toShortString(); 155 } 156 157 158 if (node instanceof AdviceDec) { 159 AdviceDec aDec = (AdviceDec)node; 160 return aDec.toShortString(); 161 } 162 163 164 if (node instanceof VarDec) { 165 return ((VarDec)node).getName(); 166 } 167 168 if (node instanceof CodeDec) { 169 CodeDec codeDec = (CodeDec)node; 170 String name = codeDec.getName(); 171 if (name.equals("<init>")) name = getDeclaringType(node); 172 173 return name+codeDec.getFormals().toShortString(); 174 } 175 176 if (node instanceof IntroducedDec) { 177 IntroducedDec idec = (IntroducedDec)node; 178 return idec.getTargets().toShortString()+"."+getSignature(idec.getDec()); 179 } 180 if (node instanceof IntroducedSuperDec) { 181 IntroducedSuperDec idec = (IntroducedSuperDec)node; 182 183 return idec.getTargets().toShortString()+"+" + 184 ( idec.getIsImplements() ? "implements" : "extends" ); 185 } 186 187 return "..."; 188 } 189 190 public String trimSig(String sig) { 191 int index = sig.length()-1; 192 while (index > 0) { 193 char ch = sig.charAt(index); 194 if (ch == '{' || ch == '}' || ch == ';' || Character.isWhitespace(ch)) { 195 index--; 196 continue; 197 } 198 break; 199 } 200 return sig.substring(0, index+1); 201 } 202 203 public String getModifiers(ASTObject node) { 204 if (node instanceof IntroducedDec) { 205 return ((IntroducedDec)node).getDec().getModifiers().toShortString(); 206 } else if (node instanceof Dec) { 207 return ((Dec)node).getModifiers().toShortString(); 208 } else { 209 return ""; 210 } 211 } 212 213 241 242 public Pcd getPcd(ASTObject node) { 243 if (node instanceof AdviceDec) { 244 AdviceDec adviceDec = (AdviceDec)node; 245 return adviceDec.getPcd(); 246 } else if (node instanceof PointcutDec) { 247 return ((PointcutDec)node).getPcd(); 248 } else { 249 return null; 250 } 251 } 252 253 public String getPcdString(ASTObject node) { 254 Pcd cc = getPcd(node); 255 if (cc == null) return null; 256 return cc.toShortString(); 257 } 258 259 public Declaration getPointcutDeclaration(ASTObject node) { 260 Pcd cc = getPcd(node); 261 if (cc == null) return null; 262 if (!(cc instanceof NamePcd)) return null; 263 264 NamePcd nameCC = (NamePcd)cc; 265 return makeBaseDeclaration(nameCC.getPointcutDec()); 266 } 267 268 269 public String getFullSignature(ASTObject node) { 270 if (node instanceof Dec) { 271 Dec dec = (Dec)node; 272 return dec.getSignatureString(); 273 } else { 274 return null; 275 } 276 } 277 278 public void doIndent(Writer writer, int size) throws IOException { 279 for(int i=0; i<size; i++) writer.write(" "); 280 } 281 282 public Declaration makeBaseDeclaration(ASTObject node) { 283 if (node == null) return null; 284 285 if (node instanceof InitializerDec) return null; 287 288 if (node instanceof Dec) { 290 if ( ((Dec)node).isIntroduced()) return null; 291 } 292 293 String kindPrefix = ""; 294 if (node.getSourceFile() == null) { 298 return null; 300 } 301 302 Declaration dec = 303 new Declaration(node.getBeginLine(), node.getEndLine(), 304 node.getBeginColumn(), node.getEndColumn(), 305 getModifiers(node), 306 getSignature(node), 307 getFullSignature(node), 308 getPcdString(node), 309 getDeclaringType(node), 310 kindPrefix+getKind(node), 311 node.getSourceFile().getAbsolutePath(), 312 node.getFormalComment(), 313 node.getCompilationUnit().getPackageName() 314 ); 315 dec.setCrosscutDeclaration(getPointcutDeclaration(node)); 316 return dec; 317 } 318 319 private Declaration[] makeDeclarations(Collection nodes) { 320 if (nodes == null) return new Declaration[0]; 321 322 List newDeclarations = new ArrayList(); 323 for (Iterator iter = nodes.iterator(); iter.hasNext(); ) { 324 Declaration dec = makeBaseDeclaration((ASTObject)iter.next()); 325 if (dec == null) continue; 327 newDeclarations.add(dec); 328 } 329 330 return (Declaration[])newDeclarations.toArray(new Declaration[0]); 331 } 332 333 private Declaration[] decsToDeclarations(Decs decs) { 334 if (decs == null) return new Declaration[0]; 335 336 List newDeclarations = new ArrayList(); 337 for (int i=0; i<decs.size(); i++) { 338 if (!decs.get(i).isLanguageVisible()) { 339 continue; 340 } 341 Declaration dec = makeDeclaration(decs.get(i)); 342 if (dec == null) continue; 344 newDeclarations.add(dec); 345 } 346 347 return (Declaration[])newDeclarations.toArray(new Declaration[newDeclarations.size()]); 348 } 349 350 private Declaration makeDeclaration(ASTObject node) { 351 Declaration declaration = makeBaseDeclaration(node); 352 if (declaration == null) return null; 353 354 declaration.setPointsTo(makeDeclarations((Collection)pointsTo.get(node))); 355 declaration.setPointedToBy(makeDeclarations((Collection)pointedToBy.get(node))); 356 357 if (node instanceof TypeDec) { 358 Decs enclosedDecs = ((TypeDec)node).getBody(); 359 declaration.setDeclarations(decsToDeclarations(enclosedDecs)); 360 } 361 366 return declaration; 367 } 368 369 public void dumpSymbolFile(OutputStream stream) throws IOException { 370 Declaration[] declarations = decsToDeclarations(compilationUnit.getDecs()); 371 373 ObjectOutputStream objectStream = new ObjectOutputStream(stream); 374 objectStream.writeObject(declarations); 375 objectStream.close(); 376 } 377 } 378 | Popular Tags |