1 22 23 package org.aspectj.debugger.gui; 24 25 import org.aspectj.debugger.base.*; 26 27 import java.io.*; 28 import java.util.*; 29 import java.util.List ; 30 import org.aspectj.tools.ide.*; 31 import com.sun.jdi.*; 32 33 public class AJLineMapper { 34 35 36 private static HashMap noAJCHash = new HashMap(); 37 38 public final static String NO_SOURCE_STRING = "<not-available>"; 39 public final static String NON_MAPPING_SOURCE_STRING = "<non-mapping-source>"; 40 public final static String UNKNOWN_SOURCE_STRING = "<unknown>"; 41 public final static int NO_SOURCE_LINE = -1; 42 public final static int NON_MAPPING_SOURCE_LINE = -2; 43 44 public static SymbolManager symbolManager = new SymbolManager(); 45 46 54 public boolean hasBody() { 55 return true; 56 } 57 58 public static SymbolManager sm() { 59 return symbolManager; 60 } 61 62 public static int getCorrespondingOutputLineNo(String classID, int line) { 63 return AJLineMapper.getCorrespondingOutputLine(classID, line).line; 64 } 65 66 public static SourceLine getCorrespondingOutputLine(String classID, int line) { 67 String filePath = getSourceFilePathFromAJCClass(classID); 68 return getCorrespondingOutputLineFromFullSource(filePath, line); 69 } 70 71 public static SourceLine getCorrespondingOutputLineFromSource(String source, 72 int line) { 73 String filePath = getFullFileNameFromFile(source, false); 74 return getCorrespondingOutputLineFromFullSource(filePath, line); 75 } 76 77 83 static SourceLine getCorrespondingOutputLineFromFullSource(String filePath, 84 int line) { 85 if (filePath == null) { 89 return AJLineMapper.unknownFileSourceLine(line); 90 } 91 92 int lineNo = -1; 98 String newFile = UNKNOWN_SOURCE_STRING; 99 SourceLine sl = null; 100 line++; 101 while (--line >= 0 && lineNo == -1) { 102 sl = symbolManager.mapToOutputLine(filePath, line); 103 if (sl != null) { 105 lineNo = sl.line; 106 newFile = sl.filename; 107 } 108 } 109 110 if (lineNo == -1) { 111 return nonMappingSourceLine(); 112 } else { 113 return new SourceLine(newFile, lineNo + 1); 115 } 116 } 117 118 124 public static String translateGeneratedMethodName(String methodName) { 125 String sigToken = "$signature"; 126 String memberToken = "$member"; 127 String staticMemberToken = "$static$member"; 128 String adviceToken = "$reception"; 129 String aroundToken = "$around$reception"; 130 int sigTokenStart = methodName.indexOf(sigToken); 131 int memberTokenStart = methodName.indexOf(memberToken); 132 int staticMemberTokenStart = methodName.indexOf(staticMemberToken); 133 int adviceTokenStart = methodName.indexOf(adviceToken); 134 int aroundTokenStart = methodName.indexOf(aroundToken); 135 int indexNameStart = methodName.lastIndexOf("$"); 136 if (sigTokenStart > -1 && indexNameStart > -1) { 137 return methodName.substring(methodName.indexOf("$")+1, sigTokenStart); 138 } 139 else if (staticMemberTokenStart > -1 && indexNameStart > -1) { 140 return methodName.substring(methodName.indexOf("$")+1, staticMemberTokenStart); 141 } 142 else if (memberTokenStart > -1 && indexNameStart > -1) { 143 return methodName.substring(methodName.indexOf("$")+1, memberTokenStart); 144 } 145 else if (adviceTokenStart > -1 && indexNameStart > -1) { 146 String adviceNumber = methodName.substring(adviceTokenStart + adviceToken.length()); 147 return "<advice #" + adviceNumber +">"; 148 } 149 else if (aroundTokenStart > -1 && indexNameStart > -1) { 150 String adviceNumber = methodName.substring(aroundTokenStart + aroundToken.length()); 151 return "<advice #" + adviceNumber +">"; 152 } 153 else { 154 return methodName; 155 } 156 } 157 158 public static String getCorrespondingFile(Location loc) { 159 String filename = ""; 160 String qualifiedName = null; 161 try { 162 String locName = loc.declaringType().name(); 163 String sourceName = loc.declaringType().sourceName(); 164 String strippedSourceName = sourceName.substring(0, sourceName.indexOf(".java")); 165 166 String packageName = locName.substring(0, locName.indexOf(strippedSourceName)); 167 qualifiedName = packageName + strippedSourceName; 168 filename = getFileNameFromQualified(qualifiedName, true); 169 170 } catch (AbsentInformationException aie) { 171 172 } return filename; 174 } 175 176 180 public static String getSourceFilePathFromAJCClass(String classID) { 181 182 if (noAJCHash.get(classID) != null) { 185 return null; 186 } 187 188 String ajFile = getFileNameFromQualified(classID, true); 192 193 SourceLine sourceLine = null; 194 String filePath = null; 195 if (new File(ajFile).exists()) { 196 for (int i = 0; i < 1000 && sourceLine == null; i++) { 197 if ( (sourceLine = symbolManager.mapToSourceLine(ajFile, i)) != null) { 198 filePath = sourceLine.filename; 199 } 200 } 201 } else { 202 noAJCHash.put(classID, classID); 204 return null; 205 } 206 return filePath; 207 } 208 209 212 public static SourceLine getCorrespondingLine(Location loc) { 213 SourceLine ajline = null; 214 String qualifiedName = null; 215 try { 216 qualifiedName = getQualifiedNameForSource(loc); 217 int locLineNumber = loc.lineNumber(); 218 ajline = getCorrespondingLine(qualifiedName, locLineNumber); 219 220 222 if (ajline != null && 224 ajline.line == AJLineMapper.NON_MAPPING_SOURCE_LINE) { 225 String classMethodString = loc.method() + ""; 226 classMethodString = classMethodString.substring 227 (0, classMethodString.indexOf(")")+1); 228 int i2dot = classMethodString.indexOf("("); 229 int numDots = 0; 230 while (i2dot > -1) { 231 if (classMethodString.charAt(i2dot) == '.') { 232 if ((++numDots) == 2) { 233 break; 234 } 235 } 236 i2dot--; 237 } 238 if (i2dot != -1) { 239 classMethodString = classMethodString.substring(i2dot + 1); 240 } 241 String ajFile = AJLineMapper.getSourceFilePathFromAJCClass 242 (loc.declaringType().name()); 243 if (ajFile != null) { 244 int line = AJDecParser.getMethodLineNumber 245 (ajFile, classMethodString); 246 ajline = new SourceLine(ajFile, line); 247 } else { 248 ajline = new SourceLine(loc.sourceName(), loc.lineNumber()); 249 } 250 } 251 } catch (AbsentInformationException aie) { 252 } 253 return (ajline == null) ? emptySourceLine() : ajline; 254 } 255 256 259 public static boolean isUnknownSource(String source) { 260 return source.equals(AJLineMapper.UNKNOWN_SOURCE_STRING); 261 } 262 263 static String getQualifiedNameForSource(Location loc) 264 throws AbsentInformationException { 265 ReferenceType refType = loc.declaringType(); 266 if (refType == null) { 267 throw new AbsentInformationException(); 268 } 269 String locName = refType.name(); 270 String sourceName = loc.sourceName(); 271 String strippedSourceName = 272 sourceName.substring(0, sourceName.indexOf(".java")); 273 int lastDot = locName.lastIndexOf('.'); 274 String packageName = ""; 275 if (lastDot != -1) { 276 packageName = locName.substring(0, lastDot+1); 277 } 278 String qualifiedName = packageName + strippedSourceName; 279 return qualifiedName; 280 } 281 282 private static SourceLine emptySourceLine() { 283 return new SourceLine(NO_SOURCE_STRING, NO_SOURCE_LINE); 284 } 285 286 private static SourceLine nonMappingSourceLine() { 287 return new SourceLine(NON_MAPPING_SOURCE_STRING, NON_MAPPING_SOURCE_LINE); 288 } 289 290 private static SourceLine unknownFileSourceLine(int line) { 291 return new SourceLine(UNKNOWN_SOURCE_STRING, line); 292 } 293 294 public static SourceLine getCorrespondLineFromFullSource(String filePath, 295 int line) { 296 SourceLine newLine = null; 297 if (new File(filePath).exists()) { 298 int offsetLine = -1; 299 try { 300 offsetLine = line - 1; newLine = symbolManager.mapToSourceLine 302 (new File(filePath).getCanonicalPath(), offsetLine); 303 } catch (IOException ioe) { 306 } 307 if (newLine == null) { 308 return nonMappingSourceLine(); 312 } 313 return newLine; 317 } 318 return new SourceLine(AJLineMapper.UNKNOWN_SOURCE_STRING, line); 322 323 } 324 325 328 public static SourceLine getCorrespondingLine(String qualifiedName, int line) { 329 String filePath = getFileNameFromQualified(qualifiedName, true); 330 return getCorrespondLineFromFullSource(filePath, line); 331 } 332 333 336 public static SourceLine getCorrespondingLineFromSource(String sourceName, 337 int line) { 338 String filePath = AJLineMapper.insertWorkingDir(sourceName); 339 return getCorrespondLineFromFullSource(filePath, line); 340 } 341 342 static String getFileNameFromQualified(String qualifiedName, boolean inAJWorkingDir) { 344 String qualifiedNameNoDollars = AJLineMapper.stripDollarsFrom(qualifiedName); 346 347 String offset = ComponentRepository.getSourcePane().getSourcePath() + ""; 349 if (inAJWorkingDir) { 350 offset += "/ajworkingdir"; 351 } 352 String sourcePath = new File(offset).getAbsolutePath(); 353 354 String filename = qualifiedNameNoDollars.replace('.', File.separatorChar) + ".java"; 356 return sourcePath + File.separator + filename; 357 } 358 359 static String getFullFileNameFromFile(String fullName, boolean inAJWorkingDir) { 361 String offset = ComponentRepository.getSourcePane().getSourcePath() + ""; 363 if (inAJWorkingDir) { 364 offset += "/ajworkingdir"; 365 } 366 String sourcePath = new File(offset).getAbsolutePath(); 367 if (fullName.indexOf(offset) == -1) { 368 return new File(sourcePath + File.separator + fullName).getAbsolutePath(); 369 } else { 370 return new File(fullName).getAbsolutePath(); 371 } 372 } 373 374 static String getFullFileNameFromLocation(Location loc, boolean inAJWorkingDir) 376 throws AbsentInformationException { 377 String offset = ComponentRepository.getSourcePane().getSourcePath() + ""; 379 if (inAJWorkingDir) { 380 offset += "/ajworkingdir"; 381 } 382 String fullName = loc.declaringType().name(); 383 String srcName = loc.sourceName(); 384 String pkg = ""; 385 int iDot = fullName.lastIndexOf("."); 386 if (iDot != -1) { 387 pkg = fullName.substring(0, iDot); 388 pkg = pkg.replace('.', File.separatorChar); 389 } 390 String srcPath = pkg + File.separator + srcName; 391 392 String sourcePath = new File(offset).getAbsolutePath(); 393 if (srcPath.indexOf(offset) == -1) { 394 return new File(sourcePath + File.separator + srcPath).getAbsolutePath(); 395 } else { 396 return new File(srcPath).getAbsolutePath(); 397 } 398 } 399 400 static String removeRootAndAJWorkingDir(String fullPath) { 401 String offset = 402 ComponentRepository.getSourcePane().getSourcePath() + File.separator + 403 "ajworkingdir" + File.separator; 404 int index = offset.length(); 405 if (!fullPath.startsWith(offset)) { 406 return fullPath; 407 } 408 String returnString = fullPath.substring(index); 409 return returnString; 410 } 411 412 static String removeRoot(String fullPath) { 413 String offset = ""; 414 int index; 415 String path = ComponentRepository.getSourcePane().getSourcePath() + ""; 417 path += File.separator + "ajworkingdir" + File.separator; 418 File file = new File(path); 419 offset = file.getAbsolutePath(); 421 if (fullPath.startsWith(offset)) { 425 index = offset.length(); 426 return fullPath.substring(index+1); 427 } 428 offset = ""; 429 file = new File(ComponentRepository.getSourcePane().getSourcePath() + ""); 431 offset = file.getAbsolutePath(); 433 if (fullPath.startsWith(offset)) { 437 index = offset.length(); 438 return fullPath.substring(index+1); 439 } 440 return fullPath; 441 } 442 443 static String stripDollarsFrom(String str) { 444 int index = str.indexOf("$"); 445 if (index != -1) { 446 return str.substring(0, index); 447 } 448 return str; 449 } 450 451 static String insertWorkingDir(String fullPath) { 452 String path = ComponentRepository.getSourcePane().getSourcePath() + ""; 453 if (fullPath.indexOf("ajworkingdir") != -1) { 457 return fullPath; 458 } 459 int ipath = fullPath.indexOf(path); 463 if (ipath == -1) { 464 return AJLineMapper.getFullFileNameFromFile(fullPath, true); 465 } 466 String rest = fullPath.substring(ipath + path.length() + 1); 470 return path + File.separator + 471 "ajworkingdir" + File.separator + 472 rest; 473 } 474 } | Popular Tags |