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.

SourceLocation.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.bridge;
  14. import java.io.File;
  15. import org.aspectj.util.LangUtil;
  16. /**
  17. * Immutable source location. This guarantees that the source file is not null and that the numeric values are positive and line ≤
  18. * endLine.
  19. *
  20. * @see org.aspectj.lang.reflect.SourceLocation
  21. */
  22. public class SourceLocation implements ISourceLocation {
  23. private static final long serialVersionUID = -5434765814401009794L;
  24. private transient int cachedHashcode = -1;
  25. /** used when SourceLocation is not available */
  26. public static final ISourceLocation UNKNOWN = new SourceLocation(ISourceLocation.NO_FILE, 0, 0, 0);
  27. private final File sourceFile;
  28. private final int startLine;
  29. private final int column;
  30. private final int endLine;
  31. private int offset;
  32. private final String context;
  33. private boolean noColumn;
  34. private String sourceFileName;
  35. /** @throws IllegalArgumentException if the input would not be a valid line */
  36. public static final void validLine(int line) {
  37. if (line < 0) {
  38. throw new IllegalArgumentException("negative line: " + line);
  39. } else if (line > ISourceLocation.MAX_LINE) {
  40. throw new IllegalArgumentException("line too large: " + line);
  41. }
  42. }
  43. /** @throws IllegalArgumentException if the input would not be a valid column */
  44. public static final void validColumn(int column) {
  45. if (column < 0) {
  46. throw new IllegalArgumentException("negative column: " + column);
  47. } else if (column > ISourceLocation.MAX_COLUMN) {
  48. throw new IllegalArgumentException("column too large: " + column);
  49. }
  50. }
  51. /**
  52. * Same as SourceLocation(file, line, line, 0), except that column is not rendered during toString()
  53. */
  54. public SourceLocation(File file, int line) {
  55. this(file, line, line, NO_COLUMN);
  56. }
  57. /** same as SourceLocation(file, line, endLine, ISourceLocation.NO_COLUMN) */
  58. public SourceLocation(File file, int line, int endLine) {
  59. this(file, line, endLine, ISourceLocation.NO_COLUMN);
  60. }
  61. /**
  62. * @param file File of the source; if null, use ISourceLocation.NO_FILE, not null
  63. * @param line int starting line of the location - positive number
  64. * @param endLine int ending line of the location - &le; starting line
  65. * @param column int character position of starting location - positive number
  66. */
  67. public SourceLocation(File file, int line, int endLine, int column) {
  68. this(file, line, endLine, column, (String) null);
  69. }
  70. public SourceLocation(File file, int line, int endLine, int column, String context) {
  71. if (column == NO_COLUMN) {
  72. column = 0;
  73. noColumn = true;
  74. }
  75. if (null == file) {
  76. file = ISourceLocation.NO_FILE;
  77. }
  78. validLine(line);
  79. validLine(endLine);
  80. LangUtil.throwIaxIfFalse(line <= endLine, line + " > " + endLine);
  81. LangUtil.throwIaxIfFalse(column >= 0, "negative column: " + column);
  82. this.sourceFile = file;
  83. this.startLine = line;
  84. this.column = column;
  85. this.endLine = endLine;
  86. this.context = context;
  87. }
  88. public SourceLocation(File file, int line, int endLine, int column, String context, String sourceFileName) {
  89. this(file, line, endLine, column, context);
  90. this.sourceFileName = sourceFileName;
  91. }
  92. public File getSourceFile() {
  93. return sourceFile;
  94. }
  95. public int getLine() {
  96. return startLine;
  97. }
  98. /**
  99. * @return int actual column or 0 if not available per constructor treatment of ISourceLocation.NO_COLUMN
  100. */
  101. public int getColumn() {
  102. return column;
  103. }
  104. public int getEndLine() {
  105. return endLine;
  106. }
  107. /** @return null String or application-specific context */
  108. public String getContext() {
  109. return context;
  110. }
  111. /** @return String {context\n}{file:}line{:column} */
  112. public String toString() {
  113. StringBuilder sb = new StringBuilder();
  114. if (null != context) {
  115. sb.append(context);
  116. sb.append(LangUtil.EOL);
  117. }
  118. if (sourceFile != ISourceLocation.NO_FILE) {
  119. sb.append(sourceFile.getPath());
  120. }
  121. if (startLine > 0) {
  122. sb.append(":");
  123. sb.append(startLine); // "" + startLine + "-" + endLine);
  124. }
  125. if (!noColumn) {
  126. sb.append(":" + column);
  127. }
  128. if (offset >= 0) {
  129. sb.append("::" + offset);
  130. }
  131. return sb.toString();
  132. }
  133. // XXX Ctors for this type should know about an offset, rather than
  134. // it being set through these methods - but there are just too many
  135. // ctors at the moment! It needs sorting out.
  136. public int getOffset() {
  137. return offset;
  138. }
  139. public void setOffset(int i) {
  140. cachedHashcode = -1;
  141. offset = i;
  142. }
  143. public String getSourceFileName() {
  144. return sourceFileName;
  145. }
  146. public boolean equals(Object obj) {
  147. if (!(obj instanceof SourceLocation)) {
  148. return false;
  149. }
  150. SourceLocation o = (SourceLocation) obj;
  151. return startLine == o.startLine && column == o.column && endLine == o.endLine && offset == o.offset
  152. && (sourceFile == null ? o.sourceFile == null : sourceFile.equals(o.sourceFile))
  153. && (context == null ? o.context == null : context.equals(o.context)) && noColumn == o.noColumn
  154. && (sourceFileName == null ? o.sourceFileName == null : sourceFileName.equals(o.sourceFileName));
  155. }
  156. public int hashCode() {
  157. if (cachedHashcode == -1) {
  158. cachedHashcode = (sourceFile == null ? 0 : sourceFile.hashCode());
  159. cachedHashcode = cachedHashcode * 37 + startLine;
  160. cachedHashcode = cachedHashcode * 37 + column;
  161. cachedHashcode = cachedHashcode * 37 + endLine;
  162. cachedHashcode = cachedHashcode * 37 + offset;
  163. cachedHashcode = cachedHashcode * 37 + (context == null ? 0 : context.hashCode());
  164. cachedHashcode = cachedHashcode * 37 + (noColumn ? 0 : 1);
  165. cachedHashcode = cachedHashcode * 37 + (sourceFileName == null ? 0 : sourceFileName.hashCode());
  166. }
  167. return cachedHashcode;
  168. }
  169. }