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.

EclipseSourceLocation.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.internal.compiler.lookup;
  13. import java.io.File;
  14. import org.aspectj.ajdt.internal.core.builder.EclipseAdapterUtils;
  15. import org.aspectj.bridge.ISourceLocation;
  16. import org.aspectj.org.eclipse.jdt.core.compiler.IProblem;
  17. import org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult;
  18. import org.aspectj.org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.util.Util;
  20. public class EclipseSourceLocation implements ISourceLocation {
  21. private static String NO_CONTEXT = "USE_NULL--NO_CONTEXT_AVAILABLE";
  22. CompilationResult result;
  23. // EclipseSourceContext eclipseContext;
  24. int startPos, endPos;
  25. String filename;
  26. // lazy but final
  27. File file;
  28. int startLine = -1;
  29. int endLine = -1;
  30. int column = -1;
  31. String context;
  32. public EclipseSourceLocation(CompilationResult result, int startPos, int endPos) {
  33. super();
  34. this.result = result;
  35. if (result!=null && result.fileName!=null) this.filename = new String(result.fileName);
  36. this.startPos = startPos;
  37. this.endPos = endPos;
  38. }
  39. public CompilationResult getCompilationResult() {
  40. return result;
  41. }
  42. public int getOffset() {
  43. return startPos;
  44. }
  45. public int getStartPos() {
  46. return startPos;
  47. }
  48. public int getEndPos() {
  49. return endPos;
  50. }
  51. public File getSourceFile() {
  52. if (null == file) {
  53. if (filename==null) {
  54. // if ((null == result)
  55. // || (null == result.fileName)
  56. // || (0 == result.fileName.length)) {
  57. file = ISourceLocation.NO_FILE;
  58. } else {
  59. file = new File(filename);//new String(result.fileName));
  60. }
  61. }
  62. return file;
  63. }
  64. public int getLine() {
  65. if (-1 == startLine && result!=null) {
  66. startLine = Util.getLineNumber(startPos,result.lineSeparatorPositions,0,result.lineSeparatorPositions.length-1);
  67. }
  68. return startLine;
  69. }
  70. public int getColumn() {
  71. if (-1 == column) {
  72. int lineNumber = getLine();
  73. // JJH added check that lineNumber is in legal range to avoid exceptions
  74. if (0 < lineNumber && lineNumber < result.lineSeparatorPositions.length) {
  75. int lineStart = result.lineSeparatorPositions[lineNumber];
  76. int col = startPos - lineStart; // 1-based
  77. if (0 <= col) {
  78. column = col;
  79. } else {
  80. column = 0;
  81. }
  82. } else if (0 < lineNumber && lineNumber == result.lineSeparatorPositions.length) {
  83. column = 0;
  84. }
  85. }
  86. return column;
  87. }
  88. public int getEndLine() {
  89. if (-1 == endLine) {
  90. endLine = Util.getLineNumber(endPos,result.lineSeparatorPositions,0,result.lineSeparatorPositions.length-1);
  91. }
  92. return endLine;
  93. }
  94. public String getContext() {
  95. if (null == context) {
  96. ICompilationUnit compilationUnit = result.compilationUnit;
  97. IProblem[] problems = result.problems;
  98. if ((null == compilationUnit) || (null == problems)
  99. || (1 != problems.length)) { // ?? which of n>1 problems?
  100. context = NO_CONTEXT;
  101. } else {
  102. context = EclipseAdapterUtils.makeLocationContext(compilationUnit, problems[0]);
  103. }
  104. }
  105. return (NO_CONTEXT == context ? null : context);
  106. }
  107. /** @return String {file:}line{:column} */
  108. public String toString() {
  109. StringBuilder sb = new StringBuilder();
  110. if (getSourceFile() != ISourceLocation.NO_FILE) {
  111. sb.append(getSourceFile().getPath());
  112. sb.append(":");
  113. }
  114. sb.append("" + getLine());
  115. if (getColumn() != 0) {
  116. sb.append(":" + getColumn());
  117. }
  118. if (getOffset()>=0) { sb.append("::").append(getOffset()); }
  119. return sb.toString();
  120. }
  121. private volatile int hashCode = -1;
  122. public int hashCode() {
  123. if (hashCode == -1) {
  124. int result = 17;
  125. // other parts important?
  126. result = 37*result + getLine();
  127. result = 37*result + getOffset();
  128. result = 37*result + (filename==null?0:filename.hashCode());
  129. hashCode = result;
  130. }
  131. return hashCode;
  132. }
  133. public boolean equals(Object other) {
  134. if (! (other instanceof EclipseSourceLocation)) return super.equals(other);
  135. EclipseSourceLocation o = (EclipseSourceLocation) other;
  136. return
  137. getLine()==o.getLine() &&
  138. getOffset()==o.getOffset() &&
  139. ((filename==null)?(o.filename==null):o.filename.equals(filename));
  140. }
  141. public String getSourceFileName() {
  142. return null;
  143. }
  144. }