author | jjg |
Thu, 10 Jun 2010 16:08:01 -0700 | |
changeset 5847 | 1908176fd6e3 |
parent 5520 | 86e4b9a9da40 |
child 11055 | ec1418effa77 |
permissions | -rw-r--r-- |
867 | 1 |
/* |
5520 | 2 |
* Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. |
867 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
867 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
867 | 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 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
867 | 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 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
43 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
44 |
* If you write code that depends on this, you do so at your own risk. |
867 | 45 |
* This code and its internal interfaces are subject to change or |
46 |
* deletion without notice.</b> |
|
47 |
*/ |
|
48 |
public class DiagnosticSource { |
|
3144
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
1109
diff
changeset
|
49 |
|
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
1109
diff
changeset
|
50 |
/* constant DiagnosticSource to be used when sourcefile is missing */ |
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
1109
diff
changeset
|
51 |
public static final DiagnosticSource NO_SOURCE = new DiagnosticSource() { |
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
1109
diff
changeset
|
52 |
@Override |
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
1109
diff
changeset
|
53 |
protected boolean findLine(int pos) { |
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
1109
diff
changeset
|
54 |
return false; |
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
1109
diff
changeset
|
55 |
} |
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
1109
diff
changeset
|
56 |
}; |
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
1109
diff
changeset
|
57 |
|
867 | 58 |
public DiagnosticSource(JavaFileObject fo, AbstractLog log) { |
59 |
this.fileObject = fo; |
|
60 |
this.log = log; |
|
61 |
} |
|
62 |
||
3144
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
1109
diff
changeset
|
63 |
private DiagnosticSource() {} |
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
1109
diff
changeset
|
64 |
|
867 | 65 |
/** Return the underlying file object handled by this |
66 |
* DiagnosticSource object. |
|
67 |
*/ |
|
68 |
public JavaFileObject getFile() { |
|
69 |
return fileObject; |
|
70 |
} |
|
71 |
||
72 |
/** Return the one-based line number associated with a given pos |
|
73 |
* for the current source file. Zero is returned if no line exists |
|
74 |
* for the given position. |
|
75 |
*/ |
|
76 |
public int getLineNumber(int pos) { |
|
77 |
try { |
|
78 |
if (findLine(pos)) { |
|
79 |
return line; |
|
80 |
} |
|
81 |
return 0; |
|
82 |
} finally { |
|
83 |
buf = null; |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
/** Return the one-based column number associated with a given pos |
|
88 |
* for the current source file. Zero is returned if no column exists |
|
89 |
* for the given position. |
|
90 |
*/ |
|
1109
853d8c191eac
6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents:
867
diff
changeset
|
91 |
public int getColumnNumber(int pos, boolean expandTabs) { |
867 | 92 |
try { |
93 |
if (findLine(pos)) { |
|
94 |
int column = 0; |
|
95 |
for (int bp = lineStart; bp < pos; bp++) { |
|
96 |
if (bp >= bufLen) { |
|
97 |
return 0; |
|
98 |
} |
|
1109
853d8c191eac
6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents:
867
diff
changeset
|
99 |
if (buf[bp] == '\t' && expandTabs) { |
867 | 100 |
column = (column / TabInc * TabInc) + TabInc; |
101 |
} else { |
|
102 |
column++; |
|
103 |
} |
|
104 |
} |
|
105 |
return column + 1; // positions are one-based |
|
106 |
} |
|
107 |
return 0; |
|
108 |
} finally { |
|
109 |
buf = null; |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
/** Return the content of the line containing a given pos. |
|
114 |
*/ |
|
115 |
public String getLine(int pos) { |
|
116 |
try { |
|
117 |
if (!findLine(pos)) |
|
118 |
return null; |
|
119 |
||
120 |
int lineEnd = lineStart; |
|
121 |
while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF) |
|
122 |
lineEnd++; |
|
123 |
if (lineEnd - lineStart == 0) |
|
124 |
return null; |
|
125 |
return new String(buf, lineStart, lineEnd - lineStart); |
|
126 |
} finally { |
|
127 |
buf = null; |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
public Map<JCTree, Integer> getEndPosTable() { |
|
132 |
return endPosTable; |
|
133 |
} |
|
134 |
||
135 |
public void setEndPosTable(Map<JCTree, Integer> t) { |
|
136 |
if (endPosTable != null && endPosTable != t) |
|
137 |
throw new IllegalStateException("endPosTable already set"); |
|
138 |
endPosTable = t; |
|
139 |
} |
|
140 |
||
141 |
/** Find the line in the buffer that contains the current position |
|
142 |
* @param pos Character offset into the buffer |
|
143 |
*/ |
|
3144
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
1109
diff
changeset
|
144 |
protected boolean findLine(int pos) { |
867 | 145 |
if (pos == Position.NOPOS) |
146 |
return false; |
|
147 |
||
148 |
try { |
|
149 |
// try and recover buffer from soft reference cache |
|
150 |
if (buf == null && refBuf != null) |
|
151 |
buf = refBuf.get(); |
|
152 |
||
153 |
if (buf == null) { |
|
154 |
buf = initBuf(fileObject); |
|
155 |
lineStart = 0; |
|
156 |
line = 1; |
|
157 |
} else if (lineStart > pos) { // messages don't come in order |
|
158 |
lineStart = 0; |
|
159 |
line = 1; |
|
160 |
} |
|
161 |
||
162 |
int bp = lineStart; |
|
163 |
while (bp < bufLen && bp < pos) { |
|
164 |
switch (buf[bp++]) { |
|
165 |
case CR: |
|
166 |
if (bp < bufLen && buf[bp] == LF) bp++; |
|
167 |
line++; |
|
168 |
lineStart = bp; |
|
169 |
break; |
|
170 |
case LF: |
|
171 |
line++; |
|
172 |
lineStart = bp; |
|
173 |
break; |
|
174 |
} |
|
175 |
} |
|
176 |
return bp <= bufLen; |
|
177 |
} catch (IOException e) { |
|
178 |
log.directError("source.unavailable"); |
|
179 |
buf = new char[0]; |
|
180 |
return false; |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
protected char[] initBuf(JavaFileObject fileObject) throws IOException { |
|
185 |
char[] buf; |
|
186 |
CharSequence cs = fileObject.getCharContent(true); |
|
187 |
if (cs instanceof CharBuffer) { |
|
188 |
CharBuffer cb = (CharBuffer) cs; |
|
189 |
buf = JavacFileManager.toArray(cb); |
|
190 |
bufLen = cb.limit(); |
|
191 |
} else { |
|
192 |
buf = cs.toString().toCharArray(); |
|
193 |
bufLen = buf.length; |
|
194 |
} |
|
195 |
refBuf = new SoftReference<char[]>(buf); |
|
196 |
return buf; |
|
197 |
} |
|
198 |
||
199 |
/** The underlying file object. */ |
|
200 |
protected JavaFileObject fileObject; |
|
201 |
||
202 |
protected Map<JCTree, Integer> endPosTable; |
|
203 |
||
204 |
/** A soft reference to the content of the file object. */ |
|
205 |
protected SoftReference<char[]> refBuf; |
|
206 |
||
207 |
/** A temporary hard reference to the content of the file object. */ |
|
208 |
protected char[] buf; |
|
209 |
||
210 |
/** The length of the content. */ |
|
211 |
protected int bufLen; |
|
212 |
||
213 |
/** The start of a line found by findLine. */ |
|
214 |
protected int lineStart; |
|
215 |
||
216 |
/** The line number of a line found by findLine. */ |
|
217 |
protected int line; |
|
218 |
||
219 |
/** A log for reporting errors, such as errors accessing the content. */ |
|
220 |
protected AbstractLog log; |
|
221 |
} |