1 7 8 package spoon.support.reflect.declaration; 9 10 import java.io.File ; 11 import java.io.Serializable ; 12 13 import spoon.reflect.declaration.SourcePosition; 14 15 19 20 public class SourcePositionImpl implements SourcePosition, Cloneable , 21 Serializable { 22 23 private static final long serialVersionUID = 1L; 24 25 28 public static final int searchLineNumber(int[] startLineIndexes, 29 int position) { 30 if (startLineIndexes == null) 31 return 1; 32 int length = startLineIndexes.length; 33 if (length == 0) 34 return 1; 35 int g = 0, d = length - 1; 36 int m = 0, start; 37 while (g <= d) { 38 m = (g + d) / 2; 39 if (position < (start = startLineIndexes[m])) { 40 d = m - 1; 41 } else if (position > start) { 42 g = m + 1; 43 } else { 44 return m + 1; 45 } 46 } 47 if (position < startLineIndexes[m]) { 48 return m + 1; 49 } 50 return m + 2; 51 } 52 53 File file; 54 55 int[] lineSeparatorPositions; 56 57 private int sourceStart, sourceEnd; 58 59 public SourcePositionImpl(File file, int sourceStart, int sourceEnd, 60 int[] lineSeparatorPositions) { 61 super(); 62 this.file = file; 63 this.sourceStart = sourceStart; 64 this.sourceEnd = sourceEnd; 65 this.lineSeparatorPositions = lineSeparatorPositions; 66 } 67 68 @Override 69 public Object clone() throws CloneNotSupportedException { 70 return super.clone(); 71 } 72 73 public int getColumn() { 74 return 0; 75 } 76 77 public File getFile() { 78 return file; 79 } 80 81 public int getLine() { 82 return searchLineNumber(lineSeparatorPositions, sourceStart); 83 } 84 85 public int getSourceEnd() { 86 return sourceEnd; 87 } 88 89 public int getSourceStart() { 90 return sourceStart; 91 } 92 93 97 public String toString() { 98 int ln = getLine(); 99 return (ln >= 1) ? file.getAbsolutePath() : file.getAbsolutePath() 100 + ":" + ln; 101 } 102 103 @Override 104 public boolean equals(Object obj) { 105 if (!(obj instanceof SourcePosition)) 106 return false; 107 SourcePosition s = (SourcePosition) obj; 108 return (getFile() == null ? s.getFile() == null : getFile().equals( 109 s.getFile())) 110 && getLine() == s.getLine() && getColumn() == s.getColumn(); 111 } 112 } 113 | Popular Tags |