1 22 23 package org.aspectj.debugger.gui; 24 25 import org.aspectj.debugger.base.*; 26 import java.util.*; 27 import com.sun.jdi.*; 28 import org.aspectj.tools.ide.*; 29 import java.io.File ; 30 31 public class AJStackFrameFormatter { 32 33 public final static String INVALID = "Invalid Frame"; 34 public final static String reception = "$reception"; 35 public final static String execution = "$execution"; 36 public final static String around = "$around"; 37 38 public static MethodAndSource format(StackFrame frame) { 39 String methodStr = ""; 40 String sourceStr = ""; 41 int lineNumber = -1; 42 String path = ""; 43 44 try { 45 if (frame == null) { 46 return new MethodAndSource 47 ("<BAD FRAME>", AJLineMapper.UNKNOWN_SOURCE_STRING, 48 -1, path); 49 } 50 51 Location loc = frame.location(); 52 Method meth = loc.method(); 53 String decName = meth.declaringType().name(); 54 String methName = ComponentRepository.getAJDebugger().method(loc); 55 56 int idot = decName.lastIndexOf('.'); 58 if (idot != -1) { 59 path = decName.substring(0, idot).replace('.', File.separatorChar); 60 } else { 61 path = ""; 62 } 63 64 methodStr = decName + "." + methName; 65 sourceStr = ""; 66 67 int locLine = ComponentRepository.getAJDebugger().lineNumber(loc); 68 lineNumber = locLine; 69 if (meth instanceof Method && ((Method)meth).isNative()) { 70 sourceStr += "native method"; 71 } else if (locLine == AJLineMapper.NON_MAPPING_SOURCE_LINE) { 72 try { 73 sourceStr = 74 ComponentRepository.getAJDebugger(). 75 sourceName(loc); 76 } catch (AbsentInformationException e) { 77 sourceStr += "<unknown>"; 78 } 79 } else if (locLine != AJLineMapper.NO_SOURCE_LINE) { 80 try { 81 sourceStr = 82 ComponentRepository.getAJDebugger(). 83 sourceName(loc); 84 } catch (AbsentInformationException e) { 85 sourceStr += "<unknown>"; 86 } 87 lineNumber = 88 ComponentRepository.getAJDebugger(). 89 lineNumber(loc); 90 } else { 91 sourceStr += "<unknown>"; 92 } 93 } catch (InvalidStackFrameException e) { 94 methodStr = "<THREAD RESUMED>"; 95 sourceStr = AJLineMapper.UNKNOWN_SOURCE_STRING; 96 } 97 return new MethodAndSource(methodStr, sourceStr, lineNumber, path); 98 } 99 100 public static class MethodAndSource { 101 String method; 102 String sourceName; 103 int lineNumber = -1; 104 String path; 105 String shortSourceName; 106 107 MethodAndSource(String method, String sourceName, 108 int lineNumber, String path) { 109 this.method = method; 110 this.sourceName = sourceName; 111 this.shortSourceName = shortSourceName(sourceName); 112 this.lineNumber = lineNumber; 113 this.path = path; 114 } 115 116 public String getProto() { 117 return getSourceName() + ":" + getLineNumber(); 118 } 119 120 public String getSourceName() { 121 return sourceName; 122 } 123 124 public int getLineNumber() { 125 return lineNumber; 126 } 127 128 public boolean isValid() { 129 return (lineNumber > -1); 130 } 131 132 public String getFullPath() { 133 String pfx = ""; 134 if (!path.equals("") && !sourceName.startsWith(path)) { 135 pfx = path + java.io.File.separatorChar; 136 } 137 return pfx + sourceName; 138 } 139 140 public String toString() { 141 return method + " (" + shortSourceName + 142 (isValid() ? ":" + lineNumber : "") + ")"; 143 } 144 145 private String shortSourceName(String sourceName) { 146 int ilastSlash = sourceName.lastIndexOf('\\'); 147 int ilastBackSlash = sourceName.lastIndexOf('/'); 148 int ilastFileSeparator = sourceName.lastIndexOf(File.separatorChar); 149 int ilastSep = ilastSlash; 150 if (ilastBackSlash > ilastSep) { 151 ilastSep = ilastBackSlash; 152 } 153 if (ilastFileSeparator > ilastSep) { 154 ilastSep = ilastFileSeparator; 155 } 156 if (ilastSep != -1) { 157 return sourceName.substring(ilastSep+1); 158 } 159 return sourceName; 160 } 161 } 162 163 static String formatMethod(Method method) { 164 String decName = method.declaringType().name(); 165 String methName = method.name(); 166 String methString = method + ""; 167 String ajFile = AJLineMapper.getSourceFilePathFromAJCClass(decName); 168 if (ajFile == null) { 169 return methName; 170 } 171 172 String classMethodString = methString; 174 int i2dot = methString.indexOf("("); 175 int numDots = 0; 176 while (i2dot > -1) { 177 if (methString.charAt(i2dot) == '.') { 178 if ((++numDots) == 2) { 179 break; 180 } 181 } 182 i2dot--; 183 } 184 if (i2dot != -1) { 185 classMethodString = methString.substring(i2dot + 1); 186 } 187 int line = AJDecParser.getMethodLineNumber(ajFile, classMethodString); 188 189 int iDollarOne = methName.indexOf("$"); 191 String baseName = null; 192 if (iDollarOne != -1) { 193 baseName = methName.substring(0, iDollarOne); 194 } else { 195 baseName = methName; 196 } 197 198 String ext = ""; 199 String name = baseName; 200 201 Declaration dec = AJLineMapper.sm().getDeclarationAtLine(ajFile, line); 209 if (AJTranslator.isAdvice(methName)) { 210 name = AJTranslator.translateWorkingDirMethodName(methName); 211 } else if (AJTranslator.isReception(methName)) { 214 name += " [reception]"; 215 } else if (dec != null && dec.getKind().equals("method")) { 216 name += " [call]"; 217 } else if (!AJTranslator.isNonMappingMethod(method)) { 218 name += " [body]"; 219 } else { 220 name += " [intermediate call]"; 221 } 222 return name; 223 } 224 225 public static String format(StackFrame frame, 226 int index, 227 boolean showPC) { 228 return format(frame, index, showPC, null); 229 } 230 231 public static String format(AJStackFrameNode node, 232 boolean showPC) { 233 return format(node.getStackFrame(), 234 node.index()+1, 235 showPC, 236 node.getOriginalLocation()); 237 } 238 239 private static String format(StackFrame frame, 240 int index, 241 boolean showPC, 242 Location originalLocation) { 243 String frameString = ""; 244 Location loc = null; 245 int lineNum = -1; 246 String sourceName = "<not available>"; 247 try { 248 loc = frame.location(); 249 lineNum = loc.lineNumber(); 250 sourceName = loc.sourceName(); 251 } catch (InvalidStackFrameException e) { 252 return INVALID + ": " + e.getMessage(); 253 }catch (AbsentInformationException e) { 254 } 256 257 Method meth = loc.method(); 258 frameString += " [" + index + "] "; 259 frameString += meth; 260 frameString += " ("; 261 262 if (meth instanceof Method && ((Method)meth).isNative()) { 263 frameString += "native method"; 264 } else { 265 if (lineNum == 2 ) { frameString += "receiving"; 267 } 268 else { 269 frameString += new File (sourceName).getName(); 270 } 271 if (lineNum != -1) { 272 frameString += ':'; 273 frameString += lineNum; } 275 } 276 frameString += ')'; 277 if (showPC) { long pc = loc.codeIndex(); 279 if (pc != -1) { 280 frameString += ", pc = " + pc; 281 } 282 } 283 284 return frameString; 285 } 286 } 287 | Popular Tags |