1 24 package org.aspectj.util; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.PrintStream ; 29 import java.io.PrintWriter ; 30 import java.io.StringWriter ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 import java.util.StringTokenizer ; 34 35 import org.aspectj.tools.ide.SymbolManager; 36 import org.aspectj.tools.ide.Declaration; 37 import org.aspectj.tools.ide.SourceLine; 38 39 59 public class ExceptionMapper { 60 61 62 private SymbolManager sm; 63 64 65 private File sourcepath; 66 67 68 private File workingdir; 69 70 78 private ExceptionMapper(File sourcepath) { 79 this.sourcepath = sourcepath; 80 workingdir = new File (sourcepath, "ajworkingdir"); 81 sm = SymbolManager.getSymbolManager(); 82 } 83 84 89 public ExceptionMapper(String sourcepath) { 90 this(validate(sourcepath)); 91 } 92 93 96 public ExceptionMapper() { 97 this("."); 98 } 99 100 106 public void printStackTrace(Throwable t) { 107 System.err.println(map(t)); 108 } 109 110 118 public void printStackTrace(Throwable t,PrintStream out) { 119 out.println(map(t)); 120 } 121 122 129 public void printStackTrace(Throwable t, PrintWriter out) { 130 out.println(map(t)); 131 } 132 133 private static File validate(String fileName) { 134 String path; 135 try { 136 path = new File (fileName).getCanonicalPath(); 137 } catch (IOException ioe) { 138 path = new File (fileName).getAbsolutePath(); 139 } 140 while (!Character.isLetterOrDigit(path.charAt(path.length()-1))) { 141 path = path.substring(0, path.length()-1); 142 } 143 return new File (path); 144 } 145 146 private String map(Throwable t) { 147 return map(collect(t)); 148 } 149 150 private String collect(Throwable t) { 151 StringWriter sout = new StringWriter (); 152 PrintWriter out = new PrintWriter (sout); 153 t.printStackTrace(out); 154 return sout + ""; 155 } 156 157 private final static String newline = System.getProperty("line.separator"); 158 private String map(String backtrace) { 159 String result = ""; 160 StringTokenizer tok = new StringTokenizer (backtrace, newline, false); 161 result += tok.nextToken() + newline; 162 while (tok.hasMoreTokens()) { 163 result += frame(tok.nextToken()) + newline; 164 } 165 return result; 166 } 167 168 private String frame(String frame) { 169 int i, iclass, ilastdot, ilparen, icolon, irparen; 170 char[] chars = frame.toCharArray(); 171 for (i = 0; chars[i] != 'a'; i ++) {} 172 for (iclass = i; chars[iclass++] != ' '; ) {} 173 for (ilparen = iclass; chars[ilparen] != '('; ilparen ++) {} 174 for (ilastdot = ilparen; chars[ilastdot] != '.'; ilastdot--) {} 175 for (icolon = ilparen; chars[icolon] != ':'; icolon ++) {} 176 for (irparen = icolon; chars[irparen] != ')'; irparen ++) {} 177 return map(new String (chars, iclass, ilastdot-iclass), 178 new String (chars, ilastdot+1, ilparen-ilastdot-1), 179 new String (chars, ilparen+1, icolon-ilparen-1), 180 new String (chars, icolon+1, irparen-icolon-1)); 181 } 182 183 private String map(String s0, String s1, String s2, String num) { 184 return map(s0, s1, s2, Integer.parseInt(num)); 185 } 186 187 private String map(String className, String method, String source, int line) { 188 SourceLine sl = mapToSourceLine(className, line); 189 return sl != null 190 ? frame(className, method, strip(sl.filename), sl.line) 191 : frame(className, method, source, line); 192 } 193 194 private String strip(String filename) { 195 int isep = filename.lastIndexOf(File.separator); 196 return isep != -1 ? filename.substring(isep+1) : filename; 197 } 198 199 private String frame(String className, String method, String source, int line) { 200 return "\tat " + className + "." + method + "(" + source + ":" + line + ")"; 201 } 202 203 private Map classToFile = new HashMap (); 204 private File fileForClass(String className) { 205 return (File ) classToFile.get(className); 206 } 207 private File resolve(String className) { 208 File result = fileForClass(className); 209 if (result != null) return result; 210 String resolvedName = className.replace('.', '/'); 211 int idollar = resolvedName.indexOf('$'); 212 if (idollar != -1) { 213 resolvedName = resolvedName.substring(0, idollar); 214 } 215 result = new File (workingdir + File.separator + resolvedName + ".java"); 216 classToFile.put(className, result); 217 return result; 218 } 219 220 private SourceLine mapToSourceLine(String className, int line) { 221 return sm.mapToSourceLine(resolve(className).getAbsolutePath(), line-1); 222 } 223 } 224 | Popular Tags |