You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BcelSourceContext.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import java.io.File;
  14. import java.util.Arrays;
  15. import org.aspectj.bridge.ISourceLocation;
  16. import org.aspectj.bridge.SourceLocation;
  17. import org.aspectj.weaver.IHasPosition;
  18. import org.aspectj.weaver.ISourceContext;
  19. import org.aspectj.weaver.AjAttribute.SourceContextAttribute;
  20. public class BcelSourceContext implements ISourceContext {
  21. private BcelObjectType inObject;
  22. private String sourceFileName;
  23. private int[] lineBreaks;
  24. public BcelSourceContext(BcelObjectType inObject) {
  25. this.inObject = inObject;
  26. sourceFileName = inObject.getJavaClass().getSourceFileName();
  27. // <Unknown> is the default when we don't know where it came from (see JavaClass.source_file_name)
  28. if (sourceFileName!=null && sourceFileName.equals("<Unknown>")) {
  29. sourceFileName = "Type '"+ inObject.getResolvedTypeX().getName()+"' (no debug info available)";
  30. } else {
  31. String pname = inObject.getResolvedTypeX().getPackageName();
  32. if (pname != null) {
  33. sourceFileName = pname.replace('.', '/') + '/' + sourceFileName;
  34. }
  35. }
  36. }
  37. private File getSourceFile() {
  38. //XXX make this work better borrowing code from below
  39. String fileName = sourceFileName;
  40. if (fileName == null) inObject.getJavaClass().getFileName();
  41. if (fileName == null) fileName = inObject.getResolvedTypeX().getName() + ".class";
  42. return new File(fileName);
  43. }
  44. public int getOffset() { return 0; }
  45. /*
  46. // AMC - a temporary "fudge" to give as much information as possible about the identity of the
  47. // source file this source location points to.
  48. String internalClassName = getEnclosingClass().getInternalClassName();
  49. String fileName = getEnclosingClass().getFileName();
  50. String extension = fileName.substring( fileName.lastIndexOf("."), fileName.length());
  51. String filePrefix = fileName.substring( 0, fileName.lastIndexOf("."));
  52. // internal class name is e.g. figures/Point, we don't know whether the file was
  53. // .aj or .java so we put it together with the file extension of the enclosing class
  54. // BUT... sometimes internalClassName is a different class (an aspect), so we only use it if it
  55. // matches the file name.
  56. String mostAccurateFileNameGuess;
  57. if ( internalClassName.endsWith(filePrefix)) {
  58. mostAccurateFileNameGuess = internalClassName + extension;
  59. } else {
  60. mostAccurateFileNameGuess = fileName;
  61. }
  62. return new SourceLocation(new File(mostAccurateFileNameGuess), getSourceLine());
  63. */
  64. public ISourceLocation makeSourceLocation(IHasPosition position) {
  65. if (lineBreaks != null) {
  66. int line = Arrays.binarySearch(lineBreaks, position.getStart());
  67. if (line < 0) line = -line;
  68. return new SourceLocation(getSourceFile(), line); //??? have more info
  69. } else {
  70. return new SourceLocation(getSourceFile(), 0);
  71. }
  72. }
  73. public ISourceLocation makeSourceLocation(int line) {
  74. if (line < 0) line = 0;
  75. SourceLocation sl = new SourceLocation(getSourceFile(), line);
  76. if (lineBreaks != null) {
  77. int likelyOffset = 0;
  78. if (line > 0 && line < lineBreaks.length) {
  79. //1st char of given line is next char after previous end of line
  80. likelyOffset = lineBreaks[line-1] + 1;
  81. }
  82. sl.setOffset(likelyOffset);
  83. }
  84. return sl;
  85. }
  86. public void addAttributeInfo(SourceContextAttribute sourceContextAttribute) {
  87. this.sourceFileName = sourceContextAttribute.getSourceFileName();
  88. this.lineBreaks = sourceContextAttribute.getLineBreaks();
  89. }
  90. }