author | jjg |
Tue, 16 Sep 2008 18:35:18 -0700 | |
changeset 1260 | a772ba9ba43d |
parent 1109 | 853d8c191eac |
child 3144 | 202fa249dc34 |
permissions | -rw-r--r-- |
867 | 1 |
/* |
2 |
* Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.javac.util; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.lang.ref.SoftReference; |
|
30 |
import java.nio.CharBuffer; |
|
31 |
import java.util.Map; |
|
32 |
import javax.tools.JavaFileObject; |
|
33 |
||
34 |
import com.sun.tools.javac.file.JavacFileManager; |
|
35 |
import com.sun.tools.javac.tree.JCTree; |
|
36 |
||
37 |
import static com.sun.tools.javac.util.LayoutCharacters.*; |
|
38 |
||
39 |
/** |
|
40 |
* A simple abstraction of a source file, as needed for use in a diagnostic message. |
|
41 |
* Provides access to the line and position in a line for any given character offset. |
|
42 |
* |
|
43 |
* <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
44 |
* you write code that depends on this, you do so at your own risk. |
|
45 |
* This code and its internal interfaces are subject to change or |
|
46 |
* deletion without notice.</b> |
|
47 |
*/ |
|
48 |
public class DiagnosticSource { |
|
49 |
public DiagnosticSource(JavaFileObject fo, AbstractLog log) { |
|
50 |
this.fileObject = fo; |
|
51 |
this.log = log; |
|
52 |
} |
|
53 |
||
54 |
/** Return the underlying file object handled by this |
|
55 |
* DiagnosticSource object. |
|
56 |
*/ |
|
57 |
public JavaFileObject getFile() { |
|
58 |
return fileObject; |
|
59 |
} |
|
60 |
||
61 |
public CharSequence getName() { |
|
62 |
return JavacFileManager.getJavacBaseFileName(fileObject); |
|
63 |
} |
|
64 |
||
65 |
/** Return the one-based line number associated with a given pos |
|
66 |
* for the current source file. Zero is returned if no line exists |
|
67 |
* for the given position. |
|
68 |
*/ |
|
69 |
public int getLineNumber(int pos) { |
|
70 |
try { |
|
71 |
if (findLine(pos)) { |
|
72 |
return line; |
|
73 |
} |
|
74 |
return 0; |
|
75 |
} finally { |
|
76 |
buf = null; |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
/** Return the one-based column number associated with a given pos |
|
81 |
* for the current source file. Zero is returned if no column exists |
|
82 |
* for the given position. |
|
83 |
*/ |
|
1109
853d8c191eac
6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents:
867
diff
changeset
|
84 |
public int getColumnNumber(int pos, boolean expandTabs) { |
867 | 85 |
try { |
86 |
if (findLine(pos)) { |
|
87 |
int column = 0; |
|
88 |
for (int bp = lineStart; bp < pos; bp++) { |
|
89 |
if (bp >= bufLen) { |
|
90 |
return 0; |
|
91 |
} |
|
1109
853d8c191eac
6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents:
867
diff
changeset
|
92 |
if (buf[bp] == '\t' && expandTabs) { |
867 | 93 |
column = (column / TabInc * TabInc) + TabInc; |
94 |
} else { |
|
95 |
column++; |
|
96 |
} |
|
97 |
} |
|
98 |
return column + 1; // positions are one-based |
|
99 |
} |
|
100 |
return 0; |
|
101 |
} finally { |
|
102 |
buf = null; |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
/** Return the content of the line containing a given pos. |
|
107 |
*/ |
|
108 |
public String getLine(int pos) { |
|
109 |
try { |
|
110 |
if (!findLine(pos)) |
|
111 |
return null; |
|
112 |
||
113 |
int lineEnd = lineStart; |
|
114 |
while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF) |
|
115 |
lineEnd++; |
|
116 |
if (lineEnd - lineStart == 0) |
|
117 |
return null; |
|
118 |
return new String(buf, lineStart, lineEnd - lineStart); |
|
119 |
} finally { |
|
120 |
buf = null; |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
public Map<JCTree, Integer> getEndPosTable() { |
|
125 |
return endPosTable; |
|
126 |
} |
|
127 |
||
128 |
public void setEndPosTable(Map<JCTree, Integer> t) { |
|
129 |
if (endPosTable != null && endPosTable != t) |
|
130 |
throw new IllegalStateException("endPosTable already set"); |
|
131 |
endPosTable = t; |
|
132 |
} |
|
133 |
||
134 |
/** Find the line in the buffer that contains the current position |
|
135 |
* @param pos Character offset into the buffer |
|
136 |
*/ |
|
137 |
private boolean findLine(int pos) { |
|
138 |
if (pos == Position.NOPOS) |
|
139 |
return false; |
|
140 |
||
141 |
try { |
|
142 |
// try and recover buffer from soft reference cache |
|
143 |
if (buf == null && refBuf != null) |
|
144 |
buf = refBuf.get(); |
|
145 |
||
146 |
if (buf == null) { |
|
147 |
buf = initBuf(fileObject); |
|
148 |
lineStart = 0; |
|
149 |
line = 1; |
|
150 |
} else if (lineStart > pos) { // messages don't come in order |
|
151 |
lineStart = 0; |
|
152 |
line = 1; |
|
153 |
} |
|
154 |
||
155 |
int bp = lineStart; |
|
156 |
while (bp < bufLen && bp < pos) { |
|
157 |
switch (buf[bp++]) { |
|
158 |
case CR: |
|
159 |
if (bp < bufLen && buf[bp] == LF) bp++; |
|
160 |
line++; |
|
161 |
lineStart = bp; |
|
162 |
break; |
|
163 |
case LF: |
|
164 |
line++; |
|
165 |
lineStart = bp; |
|
166 |
break; |
|
167 |
} |
|
168 |
} |
|
169 |
return bp <= bufLen; |
|
170 |
} catch (IOException e) { |
|
171 |
log.directError("source.unavailable"); |
|
172 |
buf = new char[0]; |
|
173 |
return false; |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
protected char[] initBuf(JavaFileObject fileObject) throws IOException { |
|
178 |
char[] buf; |
|
179 |
CharSequence cs = fileObject.getCharContent(true); |
|
180 |
if (cs instanceof CharBuffer) { |
|
181 |
CharBuffer cb = (CharBuffer) cs; |
|
182 |
buf = JavacFileManager.toArray(cb); |
|
183 |
bufLen = cb.limit(); |
|
184 |
} else { |
|
185 |
buf = cs.toString().toCharArray(); |
|
186 |
bufLen = buf.length; |
|
187 |
} |
|
188 |
refBuf = new SoftReference<char[]>(buf); |
|
189 |
return buf; |
|
190 |
} |
|
191 |
||
192 |
/** The underlying file object. */ |
|
193 |
protected JavaFileObject fileObject; |
|
194 |
||
195 |
protected Map<JCTree, Integer> endPosTable; |
|
196 |
||
197 |
/** A soft reference to the content of the file object. */ |
|
198 |
protected SoftReference<char[]> refBuf; |
|
199 |
||
200 |
/** A temporary hard reference to the content of the file object. */ |
|
201 |
protected char[] buf; |
|
202 |
||
203 |
/** The length of the content. */ |
|
204 |
protected int bufLen; |
|
205 |
||
206 |
/** The start of a line found by findLine. */ |
|
207 |
protected int lineStart; |
|
208 |
||
209 |
/** The line number of a line found by findLine. */ |
|
210 |
protected int line; |
|
211 |
||
212 |
/** A log for reporting errors, such as errors accessing the content. */ |
|
213 |
protected AbstractLog log; |
|
214 |
} |