KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > reflect > declaration > SourcePositionImpl


1 /*
2  * @(#)SourcePositionImpl.java 1.3 04/07/13
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package spoon.support.reflect.declaration;
9
10 import java.io.File JavaDoc;
11 import java.io.Serializable JavaDoc;
12
13 import spoon.reflect.declaration.SourcePosition;
14
15 /**
16  * This class represents the position of a Java program element in a source
17  * file.
18  */

19
20 public class SourcePositionImpl implements SourcePosition, Cloneable JavaDoc,
21         Serializable JavaDoc {
22     
23     private static final long serialVersionUID = 1L;
24
25     /**
26      * Search the line number corresponding to a specific position
27      */

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 JavaDoc file;
54
55     int[] lineSeparatorPositions;
56
57     private int sourceStart, sourceEnd;
58
59     public SourcePositionImpl(File JavaDoc 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 JavaDoc
69     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
70         return super.clone();
71     }
72
73     public int getColumn() {
74         return 0;
75     }
76
77     public File JavaDoc 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     /**
94      * Returns a string representation of this position in the form
95      * "sourcefile:line", or "sourcefile" if no line number is available.
96      */

97     public String JavaDoc toString() {
98         int ln = getLine();
99         return (ln >= 1) ? file.getAbsolutePath() : file.getAbsolutePath()
100                 + ":" + ln;
101     }
102
103     @Override JavaDoc
104     public boolean equals(Object JavaDoc 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