1 24 25 package org.aspectj.tools.ide; 26 27 28 import java.util.*; 29 import java.util.Enumeration ; 30 import java.util.Vector ; 31 import java.util.Hashtable ; 32 import java.io.*; 33 34 35 36 37 public class SymbolManager { 38 private static final String SYMBOL_FILE_EXTENSION = ".ajsym"; 39 private static final String SOURCE_LINES_FILE_EXTENSION = ".ajsline"; 40 41 public static File mapFilenameToSymbolFile(String filename) { 42 return mapFilenameToNewExtensionFile(filename, SYMBOL_FILE_EXTENSION); 43 } 44 45 public static File mapFilenameToSourceLinesFile(String filename) { 46 return mapFilenameToNewExtensionFile(filename, SOURCE_LINES_FILE_EXTENSION); 47 } 48 49 public static File getSourceToOutputFile(String dirname) { 50 return new File(dirname, ".ajsline"); 51 } 52 53 public static File getOutputToSourceFile(String dirname) { 54 return new File(dirname, ".ajoline"); 55 } 56 57 58 private static File mapFilenameToNewExtensionFile(String filename, String ext) { 59 int lastDot = filename.lastIndexOf('.'); 60 String basename = filename; 61 if (lastDot != -1) { 62 basename = basename.substring(0, lastDot); 63 } 64 65 return new File(basename+ext); 66 } 67 68 private static SymbolManager symbolManagerInstance = new SymbolManager(); 69 70 public static SymbolManager getSymbolManager() { 71 return symbolManagerInstance; 72 } 73 74 80 public SourceLine mapToSourceLine(String filePath, int lineNumber) { 81 Map map = lookupOutputToSource(filePath); 82 83 if (map == null) return null; 84 85 return (SourceLine)map.get(new SourceLine(filePath, lineNumber)); 86 } 87 88 89 95 public SourceLine mapToOutputLine(String filePath, int lineNumber) { 96 Map map = lookupSourceToOutput(filePath); 97 98 if (map == null) return null; 99 100 return (SourceLine)map.get(new SourceLine(filePath, lineNumber)); 101 } 102 103 104 105 126 127 128 public Declaration[] getDeclarations(String filename) { 129 return lookupDeclarations(filename); 130 } 131 132 public Declaration getDeclarationAtLine(String filename, int line) { 135 return getDeclarationAtPoint(filename, line, -1); 136 } 137 138 public Declaration getDeclarationAtPoint(String filename, int line, int column) { 139 140 Declaration[] declarations = lookupDeclarations(filename); 141 return getDeclarationAtPoint(declarations, line, column); 144 } 145 146 public Declaration getDeclarationAtPoint(Declaration[] declarations, int line, int column) { 147 if (declarations == null) return null; 151 152 for(int i=0; i<declarations.length; i++) { 153 Declaration dec = declarations[i]; 154 if (dec.getBeginLine() == line) { if (column == -1) return dec; 156 if (dec.getBeginColumn() == column) { return dec; 158 } 159 } 160 Declaration[] enclosedDecs = dec.getDeclarations(); 161 if (enclosedDecs.length == 0) continue; 162 163 Declaration dec1 = getDeclarationAtPoint(enclosedDecs, line, column); 164 if (dec1 != null) return dec1; 165 } 166 167 return null; 169 } 170 171 private Hashtable symbolFileEntryCache = new Hashtable (); 172 173 private Declaration[] lookupDeclarations(String filename) { 174 CorrFileEntry entry = lookup(filename, mapFilenameToSymbolFile(filename), 175 symbolFileEntryCache); 176 return (Declaration[])entry.data; 177 } 178 179 private Hashtable sourceToOutputCache = new Hashtable (); 180 private Hashtable outputToSourceCache = new Hashtable (); 181 182 private Map lookupSourceToOutput(String filename) { 183 CorrFileEntry entry = lookup(filename, 184 getSourceToOutputFile(new File(filename).getParent()), 185 sourceToOutputCache); 186 return (Map)entry.data; 187 } 188 189 private Map lookupOutputToSource(String filename) { 190 CorrFileEntry entry = lookup(filename, 191 getOutputToSourceFile(new File(filename).getParent()), 192 outputToSourceCache); 193 return (Map)entry.data; 194 } 195 196 197 private static class CorrFileEntry { 198 public long lastModified; 199 public Object data; 200 201 public CorrFileEntry(long lastModified, Object data) { 202 this.lastModified = lastModified; 203 this.data = data; 204 } 205 } 206 207 private CorrFileEntry lookup(String filename, File file, Hashtable cache) { 208 CorrFileEntry entry = (CorrFileEntry)cache.get(filename); 209 if (entry != null && entry.lastModified == file.lastModified()) { 210 return entry; 211 } 212 213 entry = createCorrFileEntry(file); 214 cache.put(filename, entry); 215 return entry; 216 } 217 218 private CorrFileEntry createCorrFileEntry(File file) { 219 if (!file.exists()) { 220 return new CorrFileEntry(0l, null); 221 } 222 223 try { 224 long lastModified = file.lastModified(); 225 ObjectInputStream stream = 226 new ObjectInputStream(new BufferedInputStream(new FileInputStream(file))); 227 Object data = stream.readObject(); 228 stream.close(); 229 return new CorrFileEntry(lastModified, data); 230 } catch (IOException ioe) { 231 return new CorrFileEntry(0l, null); 234 } catch (ClassNotFoundException cce) { 235 return new CorrFileEntry(0l, null); 238 } 239 } 240 241 242 247 public static String translateMethodName(String methodName) { 248 int firstDollar = methodName.indexOf('$'); 249 250 if (firstDollar == -1) return methodName; 251 252 String baseName = methodName.substring(firstDollar); 253 254 if (methodName.indexOf("ajc") != -1) { 255 return "<" + baseName + " advice>"; 256 } else { 257 return baseName; 258 } 259 } 260 261 262 265 266 private static final void printIndentation(int indent, String prefix) { 267 for(int i=0; i< indent; i++) System.out.print(" "); 268 System.out.print(prefix); 269 } 270 271 272 private static final void printDeclaration(Declaration dec, int indent, String prefix) { 273 printIndentation(indent, prefix); 274 if (dec == null) { 275 System.out.println("null"); 276 return; 277 } 278 279 System.out.println(dec.getKind()+": "+dec.getDeclaringType()+": "+ 280 dec.getModifiers()+": "+dec.getSignature()+": " + 281 dec.getCrosscutDesignator()+ 283 ": "+dec.isIntroduced()+": "+dec.getPackageName()+": "+dec.getBeginLine()+":"+dec.getBeginColumn() 284 ); 285 286 296 if (prefix.equals("")) { 297 printDeclarations(dec.getTargets(), indent+INDENT, "T> "); 298 printDeclarations(dec.getPointsTo(), indent+INDENT, ">> "); 299 printDeclarations(dec.getPointedToBy(), indent+INDENT, "<< "); 300 printDeclarations(dec.getDeclarations(), indent+INDENT, ""); 301 } 302 } 303 304 private static final void printDeclarations(Declaration[] decs, int indent, String prefix) { 305 for(int i=0; i<decs.length; i++) { 306 printDeclaration(decs[i], indent, prefix); 307 } 308 } 309 310 private static final int INDENT = 2; 311 312 static void printLines(String filename, Map baseMap) throws IOException { 313 if (baseMap == null) return; 314 315 String fullName = new File(filename).getCanonicalPath(); 316 java.util.TreeMap map = new java.util.TreeMap (); 317 318 for (Iterator i = baseMap.entrySet().iterator(); i.hasNext(); ) { 319 Map.Entry entry = (Map.Entry)i.next(); 320 SourceLine keyLine = (SourceLine)entry.getKey(); 321 if (!keyLine.filename.equals(fullName)) continue; 322 323 map.put(new Integer (keyLine.line), entry.getValue()); 324 } 325 326 for (java.util.Iterator j = map.entrySet().iterator(); j.hasNext(); ) { 327 java.util.Map.Entry entry = (java.util.Map.Entry)j.next(); 328 329 System.out.println(entry.getKey() + ":\t" + entry.getValue()); 330 } 331 } 332 333 public static void main(String [] args) throws IOException { 334 for(int i=0; i<args.length; i++) { 335 String filename = args[i]; 336 System.out.println(filename); 337 338 System.out.println("declaration mappings"); 339 System.out.println("kind: declaringType: modifiers: signature: fullSignature: crosscutDesignator: isIntroduced: packageName: parentDeclaration"); 340 341 Declaration[] declarations = getSymbolManager().getDeclarations(filename); 342 if (declarations != null) { 343 printDeclarations(declarations, INDENT, ""); 344 } 345 346 System.out.println("source to output"); 347 printLines(filename, getSymbolManager().lookupSourceToOutput(filename)); 348 System.out.println("output to source"); 349 printLines(filename, getSymbolManager().lookupOutputToSource(filename)); 350 } 351 } 352 } 353 | Popular Tags |