1 24 25 package org.aspectj.tools.ide; 26 27 28 import java.util.Vector ; 29 import java.util.Enumeration ; 30 import java.lang.String ; 31 import java.io.BufferedReader ; 32 import java.io.FileReader ; 33 import java.io.IOException ; 34 import java.io.FileNotFoundException ; 35 36 public class StringBasedSymbolManager { 37 38 public static void main(String [] args) { 39 fetchDeclarations(args[0]); 40 } 42 43 public static void fetchDeclarations(String filename) { 44 try { 45 SymbolManager mgr = SymbolManager.getSymbolManager(); 46 Declaration[] decls = mgr.getDeclarations(filename); 47 if ( decls == null ) 48 throw new Error ("Can't find declarations file for " + filename); 49 printDecls(decls, true); 50 } 51 catch (Error e) { 52 lose(e); 53 } 54 } 55 56 public static void fetchSourceLine(String filename, int line) { 57 try { 58 SymbolManager mgr = SymbolManager.getSymbolManager(); 59 SourceLine srl = mgr.mapToSourceLine(filename, line); 60 if ( srl == null ) 61 print("nil"); 62 else { 63 String fn = srl.filename; 64 int ln = srl.line; 65 print("(\"" + fixFilename(fn) + "\" . " + ln + ")"); 66 } 67 } 68 catch (Error e) { 69 lose(e); 70 } 71 } 72 73 static void lose(Error e) { 74 try { 75 print("(ERROR \"" + e.toString() + "\")"); 76 } 77 catch(Error ex) { } 78 } 79 80 static void printDecls(Declaration[] decls, boolean recurse) { 81 print("("); 82 for (int i = 0; i < decls.length; i++) { 83 Declaration decl = decls[i]; 84 90 if ( decl != null ) 91 printDecl(decl, recurse); 92 } 93 print(") "); 94 } 95 96 static void printDecl(Declaration decl, boolean recurse) { 97 String crosscutDesignator = decl.getCrosscutDesignator(); 98 String kind = decl.getKind().toLowerCase(); 99 Declaration mappedDecl = nearestMappedDecl(decl); 100 print("("); 101 print("(" + mappedDecl.getBeginLine() + " . " + mappedDecl.getBeginColumn() + ") "); 102 print("(" + mappedDecl.getEndLine() + " . " + mappedDecl.getEndColumn() + ") "); 103 print(kind + " "); if (crosscutDesignator == null) 105 print("\"" + decl.getSignature() + "\" "); else if (kind.equals("introduction")) 107 print("\"" + decl.getSignature() + " " + crosscutDesignator + "\" "); else 109 print("\"" + decl.getSignature() + ": " + crosscutDesignator + "\" "); print("\"" + fixFilename(decl.getFilename()) + "\""); print("\"" + decl.getDeclaringType() + "\" "); if (recurse) { 113 printDecls(decl.getTargets(), false); printDecls(decl.getPointedToBy(), false); printDecls(decl.getDeclarations(), true); } 117 else { 118 print("nil"); 119 print("nil"); 120 print("nil"); 121 } 122 print(fixBoolean(decl.isType())); print(fixBoolean(decl.isIntroduced())); print(fixBoolean(decl.hasBody())); print(fixBoolean(decl.hasSignature())); 127 print(")"); 128 } 129 130 133 134 static boolean legalLineNumber(int ln) { return (ln != -1); } 135 136 140 static Declaration nearestMappedDecl(Declaration decl) { 141 Declaration parentDecl; 142 if (legalLineNumber(decl.getBeginLine())) 143 return decl; 144 else if ((parentDecl = decl.getParentDeclaration()) != null) 145 return nearestMappedDecl(parentDecl); 146 else 147 return decl; } 149 150 154 155 static String fixBoolean(boolean flag) { 156 if ( flag ) { return "t "; } else { return "nil ";} 157 } 158 159 160 static String fixFilename(String filename) { 161 return subst("\\\\", "\\", filename); 162 } 163 164 static int convertLineToPosition(String filename, int lineNumber) 165 throws FileNotFoundException { 166 167 FileReader freader = new FileReader (filename); 168 BufferedReader breader = new BufferedReader (freader); 169 int position = 0; 170 try { 171 for(int n = 0; n < lineNumber; n++) { 172 String line = breader.readLine(); 173 position += line.length(); 174 } 175 } 176 catch (IOException e) {} 177 return position; 178 } 179 180 181 static void print(String string) { 182 System.out.println(string); 183 } 184 185 static String subst(String n, String o, String in) { 186 int pos = in.indexOf(o); 187 if (pos == -1) 188 return in; 189 return in.substring(0, pos) + 190 n + 191 subst(n, o, (in.substring(pos + o.length()))); 192 } 193 194 195 } 196 | Popular Tags |