author | jjg |
Wed, 25 Jun 2008 14:24:53 -0700 | |
changeset 813 | ab91293d33f4 |
parent 731 | 1dd22bdb9ca5 |
child 815 | bcb5c0d7c1ab |
permissions | -rw-r--r-- |
10 | 1 |
/* |
2 |
* Copyright 1999-2006 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.*; |
|
29 |
import java.nio.CharBuffer; |
|
30 |
import java.util.HashMap; |
|
31 |
import java.util.HashSet; |
|
32 |
import java.util.Map; |
|
33 |
import java.util.Set; |
|
34 |
import javax.tools.DiagnosticListener; |
|
35 |
import javax.tools.JavaFileObject; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
167
diff
changeset
|
36 |
|
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
167
diff
changeset
|
37 |
import com.sun.tools.javac.file.JavacFileManager; |
10 | 38 |
import com.sun.tools.javac.tree.JCTree; |
39 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
|
40 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; |
|
41 |
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
167
diff
changeset
|
42 |
|
10 | 43 |
import static com.sun.tools.javac.util.LayoutCharacters.*; |
44 |
||
45 |
/** A class for error logs. Reports errors and warnings, and |
|
46 |
* keeps track of error numbers and positions. |
|
47 |
* |
|
48 |
* <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
49 |
* you write code that depends on this, you do so at your own risk. |
|
50 |
* This code and its internal interfaces are subject to change or |
|
51 |
* deletion without notice.</b> |
|
52 |
*/ |
|
53 |
public class Log { |
|
54 |
/** The context key for the log. */ |
|
55 |
public static final Context.Key<Log> logKey |
|
56 |
= new Context.Key<Log>(); |
|
57 |
||
58 |
/** The context key for the output PrintWriter. */ |
|
59 |
public static final Context.Key<PrintWriter> outKey = |
|
60 |
new Context.Key<PrintWriter>(); |
|
61 |
||
62 |
//@Deprecated |
|
63 |
public final PrintWriter errWriter; |
|
64 |
||
65 |
//@Deprecated |
|
66 |
public final PrintWriter warnWriter; |
|
67 |
||
68 |
//@Deprecated |
|
69 |
public final PrintWriter noticeWriter; |
|
70 |
||
71 |
/** The maximum number of errors/warnings that are reported. |
|
72 |
*/ |
|
73 |
public final int MaxErrors; |
|
74 |
public final int MaxWarnings; |
|
75 |
||
76 |
/** Whether or not to display the line of source containing a diagnostic. |
|
77 |
*/ |
|
78 |
private final boolean showSourceLine; |
|
79 |
||
80 |
/** Switch: prompt user on each error. |
|
81 |
*/ |
|
82 |
public boolean promptOnError; |
|
83 |
||
84 |
/** Switch: emit warning messages. |
|
85 |
*/ |
|
86 |
public boolean emitWarnings; |
|
87 |
||
88 |
/** Print stack trace on errors? |
|
89 |
*/ |
|
90 |
public boolean dumpOnError; |
|
91 |
||
92 |
/** Print multiple errors for same source locations. |
|
93 |
*/ |
|
94 |
public boolean multipleErrors; |
|
95 |
||
96 |
/** |
|
97 |
* Diagnostic listener, if provided through programmatic |
|
98 |
* interface to javac (JSR 199). |
|
99 |
*/ |
|
100 |
protected DiagnosticListener<? super JavaFileObject> diagListener; |
|
101 |
/** |
|
102 |
* Formatter for diagnostics |
|
103 |
*/ |
|
104 |
private DiagnosticFormatter diagFormatter; |
|
105 |
||
106 |
/** |
|
107 |
* Factory for diagnostics |
|
108 |
*/ |
|
109 |
private JCDiagnostic.Factory diags; |
|
110 |
||
111 |
||
112 |
/** Construct a log with given I/O redirections. |
|
113 |
*/ |
|
114 |
@Deprecated |
|
115 |
protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { |
|
116 |
context.put(logKey, this); |
|
117 |
this.errWriter = errWriter; |
|
118 |
this.warnWriter = warnWriter; |
|
119 |
this.noticeWriter = noticeWriter; |
|
120 |
||
121 |
this.diags = JCDiagnostic.Factory.instance(context); |
|
122 |
||
123 |
Options options = Options.instance(context); |
|
124 |
this.dumpOnError = options.get("-doe") != null; |
|
125 |
this.promptOnError = options.get("-prompt") != null; |
|
126 |
this.emitWarnings = options.get("-Xlint:none") == null; |
|
127 |
this.MaxErrors = getIntOption(options, "-Xmaxerrs", 100); |
|
128 |
this.MaxWarnings = getIntOption(options, "-Xmaxwarns", 100); |
|
129 |
this.showSourceLine = options.get("rawDiagnostics") == null; |
|
130 |
||
131 |
this.diagFormatter = DiagnosticFormatter.instance(context); |
|
132 |
@SuppressWarnings("unchecked") // FIXME |
|
133 |
DiagnosticListener<? super JavaFileObject> diagListener = |
|
134 |
context.get(DiagnosticListener.class); |
|
135 |
this.diagListener = diagListener; |
|
136 |
} |
|
137 |
// where |
|
138 |
private int getIntOption(Options options, String optionName, int defaultValue) { |
|
139 |
String s = options.get(optionName); |
|
140 |
try { |
|
141 |
if (s != null) return Integer.parseInt(s); |
|
142 |
} catch (NumberFormatException e) { |
|
143 |
// silently ignore ill-formed numbers |
|
144 |
} |
|
145 |
return defaultValue; |
|
146 |
} |
|
147 |
||
148 |
/** The default writer for diagnostics |
|
149 |
*/ |
|
150 |
static final PrintWriter defaultWriter(Context context) { |
|
151 |
PrintWriter result = context.get(outKey); |
|
152 |
if (result == null) |
|
153 |
context.put(outKey, result = new PrintWriter(System.err)); |
|
154 |
return result; |
|
155 |
} |
|
156 |
||
157 |
/** Construct a log with default settings. |
|
158 |
*/ |
|
159 |
protected Log(Context context) { |
|
160 |
this(context, defaultWriter(context)); |
|
161 |
} |
|
162 |
||
163 |
/** Construct a log with all output redirected. |
|
164 |
*/ |
|
165 |
protected Log(Context context, PrintWriter defaultWriter) { |
|
166 |
this(context, defaultWriter, defaultWriter, defaultWriter); |
|
167 |
} |
|
168 |
||
169 |
/** Get the Log instance for this context. */ |
|
170 |
public static Log instance(Context context) { |
|
171 |
Log instance = context.get(logKey); |
|
172 |
if (instance == null) |
|
173 |
instance = new Log(context); |
|
174 |
return instance; |
|
175 |
} |
|
176 |
||
177 |
/** The file that's currently translated. |
|
178 |
*/ |
|
179 |
protected JCDiagnostic.DiagnosticSource source; |
|
180 |
||
181 |
/** The number of errors encountered so far. |
|
182 |
*/ |
|
183 |
public int nerrors = 0; |
|
184 |
||
185 |
/** The number of warnings encountered so far. |
|
186 |
*/ |
|
187 |
public int nwarnings = 0; |
|
188 |
||
189 |
/** A set of all errors generated so far. This is used to avoid printing an |
|
190 |
* error message more than once. For each error, a pair consisting of the |
|
191 |
* source file name and source code position of the error is added to the set. |
|
192 |
*/ |
|
193 |
private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>(); |
|
194 |
||
195 |
private Map<JavaFileObject, Map<JCTree, Integer>> endPosTables; |
|
196 |
||
197 |
/** The buffer containing the file that's currently translated. |
|
198 |
*/ |
|
199 |
private char[] buf = null; |
|
200 |
||
167
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
201 |
/** The length of useful data in buf |
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
202 |
*/ |
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
203 |
private int bufLen = 0; |
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
204 |
|
10 | 205 |
/** The position in the buffer at which last error was reported |
206 |
*/ |
|
207 |
private int bp; |
|
208 |
||
209 |
/** number of the current source line; first line is 1 |
|
210 |
*/ |
|
211 |
private int line; |
|
212 |
||
213 |
/** buffer index of the first character of the current source line |
|
214 |
*/ |
|
215 |
private int lineStart; |
|
216 |
||
217 |
public boolean hasDiagnosticListener() { |
|
218 |
return diagListener != null; |
|
219 |
} |
|
220 |
||
221 |
public void setEndPosTable(JavaFileObject name, Map<JCTree, Integer> table) { |
|
222 |
if (endPosTables == null) |
|
223 |
endPosTables = new HashMap<JavaFileObject, Map<JCTree, Integer>>(); |
|
224 |
endPosTables.put(name, table); |
|
225 |
} |
|
226 |
||
227 |
/** Re-assign source, returning previous setting. |
|
228 |
*/ |
|
229 |
public JavaFileObject useSource(final JavaFileObject name) { |
|
230 |
JavaFileObject prev = currentSource(); |
|
231 |
if (name != prev) { |
|
232 |
source = new JCDiagnostic.DiagnosticSource() { |
|
233 |
public JavaFileObject getFile() { |
|
234 |
return name; |
|
235 |
} |
|
236 |
public CharSequence getName() { |
|
237 |
return JavacFileManager.getJavacBaseFileName(getFile()); |
|
238 |
} |
|
239 |
public int getLineNumber(int pos) { |
|
240 |
return Log.this.getLineNumber(pos); |
|
241 |
} |
|
242 |
public int getColumnNumber(int pos) { |
|
243 |
return Log.this.getColumnNumber(pos); |
|
244 |
} |
|
245 |
public Map<JCTree, Integer> getEndPosTable() { |
|
246 |
return (endPosTables == null ? null : endPosTables.get(name)); |
|
247 |
} |
|
248 |
}; |
|
249 |
buf = null; |
|
250 |
} |
|
251 |
return prev; |
|
252 |
} |
|
253 |
||
254 |
/** Re-assign source buffer for existing source name. |
|
255 |
*/ |
|
256 |
protected void setBuf(char[] newBuf) { |
|
257 |
buf = newBuf; |
|
167
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
258 |
bufLen = buf.length; |
10 | 259 |
bp = 0; |
260 |
lineStart = 0; |
|
261 |
line = 1; |
|
262 |
} |
|
263 |
||
264 |
protected char[] getBuf() { |
|
265 |
return buf; |
|
266 |
} |
|
267 |
||
268 |
/** Return current source name. |
|
269 |
*/ |
|
270 |
public JavaFileObject currentSource() { |
|
271 |
return source == null ? null : source.getFile(); |
|
272 |
} |
|
273 |
||
274 |
/** Flush the logs |
|
275 |
*/ |
|
276 |
public void flush() { |
|
277 |
errWriter.flush(); |
|
278 |
warnWriter.flush(); |
|
279 |
noticeWriter.flush(); |
|
280 |
} |
|
281 |
||
282 |
/** Returns true if an error needs to be reported for a given |
|
283 |
* source name and pos. |
|
284 |
*/ |
|
285 |
protected boolean shouldReport(JavaFileObject file, int pos) { |
|
286 |
if (multipleErrors || file == null) |
|
287 |
return true; |
|
288 |
||
289 |
Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos); |
|
290 |
boolean shouldReport = !recorded.contains(coords); |
|
291 |
if (shouldReport) |
|
292 |
recorded.add(coords); |
|
293 |
return shouldReport; |
|
294 |
} |
|
295 |
||
296 |
/** Prompt user after an error. |
|
297 |
*/ |
|
298 |
public void prompt() { |
|
299 |
if (promptOnError) { |
|
300 |
System.err.println(getLocalizedString("resume.abort")); |
|
301 |
char ch; |
|
302 |
try { |
|
303 |
while (true) { |
|
304 |
switch (System.in.read()) { |
|
305 |
case 'a': case 'A': |
|
306 |
System.exit(-1); |
|
307 |
return; |
|
308 |
case 'r': case 'R': |
|
309 |
return; |
|
310 |
case 'x': case 'X': |
|
311 |
throw new AssertionError("user abort"); |
|
312 |
default: |
|
313 |
} |
|
314 |
} |
|
315 |
} catch (IOException e) {} |
|
316 |
} |
|
317 |
} |
|
318 |
||
319 |
/** Print the faulty source code line and point to the error. |
|
320 |
* @param pos Buffer index of the error position, must be on current line |
|
321 |
*/ |
|
322 |
private void printErrLine(int pos, PrintWriter writer) { |
|
323 |
if (!findLine(pos)) |
|
324 |
return; |
|
325 |
||
326 |
int lineEnd = lineStart; |
|
167
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
327 |
while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF) |
10 | 328 |
lineEnd++; |
329 |
if (lineEnd - lineStart == 0) |
|
330 |
return; |
|
331 |
printLines(writer, new String(buf, lineStart, lineEnd - lineStart)); |
|
332 |
for (bp = lineStart; bp < pos; bp++) { |
|
333 |
writer.print((buf[bp] == '\t') ? "\t" : " "); |
|
334 |
} |
|
335 |
writer.println("^"); |
|
336 |
writer.flush(); |
|
337 |
} |
|
338 |
||
167
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
339 |
protected void initBuf(JavaFileObject fileObject) throws IOException { |
10 | 340 |
CharSequence cs = fileObject.getCharContent(true); |
341 |
if (cs instanceof CharBuffer) { |
|
167
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
342 |
CharBuffer cb = (CharBuffer) cs; |
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
343 |
buf = JavacFileManager.toArray(cb); |
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
344 |
bufLen = cb.limit(); |
10 | 345 |
} else { |
167
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
346 |
buf = cs.toString().toCharArray(); |
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
347 |
bufLen = buf.length; |
10 | 348 |
} |
349 |
} |
|
350 |
||
351 |
/** Find the line in the buffer that contains the current position |
|
352 |
* @param pos Character offset into the buffer |
|
353 |
*/ |
|
354 |
private boolean findLine(int pos) { |
|
355 |
if (pos == Position.NOPOS || currentSource() == null) |
|
356 |
return false; |
|
357 |
try { |
|
358 |
if (buf == null) { |
|
167
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
359 |
initBuf(currentSource()); |
10 | 360 |
lineStart = 0; |
361 |
line = 1; |
|
362 |
} else if (lineStart > pos) { // messages don't come in order |
|
363 |
lineStart = 0; |
|
364 |
line = 1; |
|
365 |
} |
|
366 |
bp = lineStart; |
|
167
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
367 |
while (bp < bufLen && bp < pos) { |
10 | 368 |
switch (buf[bp++]) { |
369 |
case CR: |
|
167
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
370 |
if (bp < bufLen && buf[bp] == LF) bp++; |
10 | 371 |
line++; |
372 |
lineStart = bp; |
|
373 |
break; |
|
374 |
case LF: |
|
375 |
line++; |
|
376 |
lineStart = bp; |
|
377 |
break; |
|
378 |
} |
|
379 |
} |
|
167
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
380 |
return bp <= bufLen; |
10 | 381 |
} catch (IOException e) { |
382 |
//e.printStackTrace(); |
|
383 |
// FIXME: include e.getLocalizedMessage() in error message |
|
384 |
printLines(errWriter, getLocalizedString("source.unavailable")); |
|
385 |
errWriter.flush(); |
|
386 |
buf = new char[0]; |
|
387 |
} |
|
388 |
return false; |
|
389 |
} |
|
390 |
||
391 |
/** Print the text of a message, translating newlines appropriately |
|
392 |
* for the platform. |
|
393 |
*/ |
|
394 |
public static void printLines(PrintWriter writer, String msg) { |
|
395 |
int nl; |
|
396 |
while ((nl = msg.indexOf('\n')) != -1) { |
|
397 |
writer.println(msg.substring(0, nl)); |
|
398 |
msg = msg.substring(nl+1); |
|
399 |
} |
|
400 |
if (msg.length() != 0) writer.println(msg); |
|
401 |
} |
|
402 |
||
403 |
/** Report an error, unless another error was already reported at same |
|
404 |
* source position. |
|
405 |
* @param key The key for the localized error message. |
|
406 |
* @param args Fields of the error message. |
|
407 |
*/ |
|
408 |
public void error(String key, Object ... args) { |
|
409 |
report(diags.error(source, null, key, args)); |
|
410 |
} |
|
411 |
||
412 |
/** Report an error, unless another error was already reported at same |
|
413 |
* source position. |
|
414 |
* @param pos The source position at which to report the error. |
|
415 |
* @param key The key for the localized error message. |
|
416 |
* @param args Fields of the error message. |
|
417 |
*/ |
|
418 |
public void error(DiagnosticPosition pos, String key, Object ... args) { |
|
419 |
report(diags.error(source, pos, key, args)); |
|
420 |
} |
|
421 |
||
422 |
/** Report an error, unless another error was already reported at same |
|
423 |
* source position. |
|
424 |
* @param pos The source position at which to report the error. |
|
425 |
* @param key The key for the localized error message. |
|
426 |
* @param args Fields of the error message. |
|
427 |
*/ |
|
428 |
public void error(int pos, String key, Object ... args) { |
|
429 |
report(diags.error(source, wrap(pos), key, args)); |
|
430 |
} |
|
431 |
||
432 |
/** Report a warning, unless suppressed by the -nowarn option or the |
|
433 |
* maximum number of warnings has been reached. |
|
434 |
* @param pos The source position at which to report the warning. |
|
435 |
* @param key The key for the localized warning message. |
|
436 |
* @param args Fields of the warning message. |
|
437 |
*/ |
|
438 |
public void warning(String key, Object ... args) { |
|
439 |
report(diags.warning(source, null, key, args)); |
|
440 |
} |
|
441 |
||
442 |
/** Report a warning, unless suppressed by the -nowarn option or the |
|
443 |
* maximum number of warnings has been reached. |
|
444 |
* @param pos The source position at which to report the warning. |
|
445 |
* @param key The key for the localized warning message. |
|
446 |
* @param args Fields of the warning message. |
|
447 |
*/ |
|
448 |
public void warning(DiagnosticPosition pos, String key, Object ... args) { |
|
449 |
report(diags.warning(source, pos, key, args)); |
|
450 |
} |
|
451 |
||
452 |
/** Report a warning, unless suppressed by the -nowarn option or the |
|
453 |
* maximum number of warnings has been reached. |
|
454 |
* @param pos The source position at which to report the warning. |
|
455 |
* @param key The key for the localized warning message. |
|
456 |
* @param args Fields of the warning message. |
|
457 |
*/ |
|
458 |
public void warning(int pos, String key, Object ... args) { |
|
459 |
report(diags.warning(source, wrap(pos), key, args)); |
|
460 |
} |
|
461 |
||
462 |
/** Report a warning. |
|
463 |
* @param pos The source position at which to report the warning. |
|
464 |
* @param key The key for the localized warning message. |
|
465 |
* @param args Fields of the warning message. |
|
466 |
*/ |
|
467 |
public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) { |
|
813 | 468 |
report(diags.mandatoryWarning(source, pos, key, args)); |
10 | 469 |
} |
470 |
||
471 |
/** Report a warning that cannot be suppressed. |
|
472 |
* @param pos The source position at which to report the warning. |
|
473 |
* @param key The key for the localized warning message. |
|
474 |
* @param args Fields of the warning message. |
|
475 |
*/ |
|
476 |
public void strictWarning(DiagnosticPosition pos, String key, Object ... args) { |
|
477 |
writeDiagnostic(diags.warning(source, pos, key, args)); |
|
478 |
nwarnings++; |
|
479 |
} |
|
480 |
||
481 |
/** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
|
482 |
* @param key The key for the localized notification message. |
|
483 |
* @param args Fields of the notification message. |
|
484 |
*/ |
|
485 |
public void note(String key, Object ... args) { |
|
486 |
report(diags.note(source, null, key, args)); |
|
487 |
} |
|
488 |
||
489 |
/** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
|
490 |
* @param key The key for the localized notification message. |
|
491 |
* @param args Fields of the notification message. |
|
492 |
*/ |
|
493 |
public void note(DiagnosticPosition pos, String key, Object ... args) { |
|
494 |
report(diags.note(source, pos, key, args)); |
|
495 |
} |
|
496 |
||
497 |
/** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
|
498 |
* @param key The key for the localized notification message. |
|
499 |
* @param args Fields of the notification message. |
|
500 |
*/ |
|
501 |
public void note(int pos, String key, Object ... args) { |
|
502 |
report(diags.note(source, wrap(pos), key, args)); |
|
503 |
} |
|
504 |
||
505 |
/** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
|
813 | 506 |
* @param file The file to which the note applies. |
507 |
* @param key The key for the localized notification message. |
|
508 |
* @param args Fields of the notification message. |
|
509 |
*/ |
|
510 |
public void note(JavaFileObject file, String key, Object ... args) { |
|
511 |
report(diags.note(wrap(file), null, key, args)); |
|
512 |
} |
|
513 |
||
514 |
/** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
|
10 | 515 |
* @param key The key for the localized notification message. |
516 |
* @param args Fields of the notification message. |
|
517 |
*/ |
|
518 |
public void mandatoryNote(final JavaFileObject file, String key, Object ... args) { |
|
813 | 519 |
report(diags.mandatoryNote(wrap(file), key, args)); |
520 |
} |
|
521 |
||
522 |
private JCDiagnostic.DiagnosticSource wrap(final JavaFileObject file) { |
|
523 |
if (file == null) { |
|
524 |
return null; |
|
10 | 525 |
} |
813 | 526 |
return new JCDiagnostic.DiagnosticSource() { |
527 |
public JavaFileObject getFile() { |
|
528 |
return file; |
|
529 |
} |
|
530 |
public CharSequence getName() { |
|
531 |
return JavacFileManager.getJavacBaseFileName(getFile()); |
|
532 |
} |
|
533 |
public int getLineNumber(int pos) { |
|
534 |
return Log.this.getLineNumber(pos); |
|
535 |
} |
|
536 |
public int getColumnNumber(int pos) { |
|
537 |
return Log.this.getColumnNumber(pos); |
|
538 |
} |
|
539 |
public Map<JCTree, Integer> getEndPosTable() { |
|
540 |
return (endPosTables == null ? null : endPosTables.get(file)); |
|
541 |
} |
|
542 |
}; |
|
10 | 543 |
} |
544 |
||
545 |
private DiagnosticPosition wrap(int pos) { |
|
546 |
return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos)); |
|
547 |
} |
|
548 |
||
549 |
/** |
|
550 |
* Common diagnostic handling. |
|
551 |
* The diagnostic is counted, and depending on the options and how many diagnostics have been |
|
552 |
* reported so far, the diagnostic may be handed off to writeDiagnostic. |
|
553 |
*/ |
|
554 |
public void report(JCDiagnostic diagnostic) { |
|
555 |
switch (diagnostic.getType()) { |
|
556 |
case FRAGMENT: |
|
557 |
throw new IllegalArgumentException(); |
|
558 |
||
559 |
case NOTE: |
|
560 |
// Print out notes only when we are permitted to report warnings |
|
561 |
// Notes are only generated at the end of a compilation, so should be small |
|
562 |
// in number. |
|
563 |
if (emitWarnings || diagnostic.isMandatory()) { |
|
564 |
writeDiagnostic(diagnostic); |
|
565 |
} |
|
566 |
break; |
|
567 |
||
568 |
case WARNING: |
|
569 |
if (emitWarnings || diagnostic.isMandatory()) { |
|
570 |
if (nwarnings < MaxWarnings) { |
|
571 |
writeDiagnostic(diagnostic); |
|
572 |
nwarnings++; |
|
573 |
} |
|
574 |
} |
|
575 |
break; |
|
576 |
||
577 |
case ERROR: |
|
578 |
if (nerrors < MaxErrors |
|
579 |
&& shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) { |
|
580 |
writeDiagnostic(diagnostic); |
|
581 |
nerrors++; |
|
582 |
} |
|
583 |
break; |
|
584 |
} |
|
585 |
} |
|
586 |
||
587 |
/** |
|
588 |
* Write out a diagnostic. |
|
589 |
*/ |
|
590 |
protected void writeDiagnostic(JCDiagnostic diag) { |
|
591 |
if (diagListener != null) { |
|
592 |
try { |
|
593 |
diagListener.report(diag); |
|
594 |
return; |
|
595 |
} |
|
596 |
catch (Throwable t) { |
|
597 |
throw new ClientCodeException(t); |
|
598 |
} |
|
599 |
} |
|
600 |
||
601 |
PrintWriter writer = getWriterForDiagnosticType(diag.getType()); |
|
602 |
||
603 |
printLines(writer, diagFormatter.format(diag)); |
|
604 |
if (showSourceLine) { |
|
605 |
int pos = diag.getIntPosition(); |
|
606 |
if (pos != Position.NOPOS) { |
|
607 |
JavaFileObject prev = useSource(diag.getSource()); |
|
608 |
printErrLine(pos, writer); |
|
609 |
useSource(prev); |
|
610 |
} |
|
611 |
} |
|
612 |
||
613 |
if (promptOnError) { |
|
614 |
switch (diag.getType()) { |
|
615 |
case ERROR: |
|
616 |
case WARNING: |
|
617 |
prompt(); |
|
618 |
} |
|
619 |
} |
|
620 |
||
621 |
if (dumpOnError) |
|
622 |
new RuntimeException().printStackTrace(writer); |
|
623 |
||
624 |
writer.flush(); |
|
625 |
} |
|
626 |
||
627 |
@Deprecated |
|
628 |
protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) { |
|
629 |
switch (dt) { |
|
630 |
case FRAGMENT: |
|
631 |
throw new IllegalArgumentException(); |
|
632 |
||
633 |
case NOTE: |
|
634 |
return noticeWriter; |
|
635 |
||
636 |
case WARNING: |
|
637 |
return warnWriter; |
|
638 |
||
639 |
case ERROR: |
|
640 |
return errWriter; |
|
641 |
||
642 |
default: |
|
643 |
throw new Error(); |
|
644 |
} |
|
645 |
} |
|
646 |
||
647 |
/** Find a localized string in the resource bundle. |
|
648 |
* @param key The key for the localized string. |
|
649 |
* @param args Fields to substitute into the string. |
|
650 |
*/ |
|
651 |
public static String getLocalizedString(String key, Object ... args) { |
|
652 |
return Messages.getDefaultLocalizedString("compiler.misc." + key, args); |
|
653 |
} |
|
654 |
||
655 |
/*************************************************************************** |
|
656 |
* raw error messages without internationalization; used for experimentation |
|
657 |
* and quick prototyping |
|
658 |
***************************************************************************/ |
|
659 |
||
660 |
/** print an error or warning message: |
|
661 |
*/ |
|
662 |
private void printRawError(int pos, String msg) { |
|
663 |
if (!findLine(pos)) { |
|
664 |
printLines(errWriter, "error: " + msg); |
|
665 |
} else { |
|
666 |
JavaFileObject file = currentSource(); |
|
667 |
if (file != null) |
|
668 |
printLines(errWriter, |
|
669 |
JavacFileManager.getJavacFileName(file) + ":" + |
|
670 |
line + ": " + msg); |
|
671 |
printErrLine(pos, errWriter); |
|
672 |
} |
|
673 |
errWriter.flush(); |
|
674 |
} |
|
675 |
||
676 |
/** report an error: |
|
677 |
*/ |
|
678 |
public void rawError(int pos, String msg) { |
|
679 |
if (nerrors < MaxErrors && shouldReport(currentSource(), pos)) { |
|
680 |
printRawError(pos, msg); |
|
681 |
prompt(); |
|
682 |
nerrors++; |
|
683 |
} |
|
684 |
errWriter.flush(); |
|
685 |
} |
|
686 |
||
687 |
/** report a warning: |
|
688 |
*/ |
|
689 |
public void rawWarning(int pos, String msg) { |
|
690 |
if (nwarnings < MaxWarnings && emitWarnings) { |
|
691 |
printRawError(pos, "warning: " + msg); |
|
692 |
} |
|
693 |
prompt(); |
|
694 |
nwarnings++; |
|
695 |
errWriter.flush(); |
|
696 |
} |
|
697 |
||
698 |
/** Return the one-based line number associated with a given pos |
|
699 |
* for the current source file. Zero is returned if no line exists |
|
700 |
* for the given position. |
|
701 |
*/ |
|
702 |
protected int getLineNumber(int pos) { |
|
703 |
if (findLine(pos)) |
|
704 |
return line; |
|
705 |
return 0; |
|
706 |
} |
|
707 |
||
708 |
/** Return the one-based column number associated with a given pos |
|
709 |
* for the current source file. Zero is returned if no column exists |
|
710 |
* for the given position. |
|
711 |
*/ |
|
712 |
protected int getColumnNumber(int pos) { |
|
713 |
if (findLine(pos)) { |
|
714 |
int column = 0; |
|
715 |
for (bp = lineStart; bp < pos; bp++) { |
|
167
f4b81c733bea
6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
10
diff
changeset
|
716 |
if (bp >= bufLen) |
10 | 717 |
return 0; |
718 |
if (buf[bp] == '\t') |
|
719 |
column = (column / TabInc * TabInc) + TabInc; |
|
720 |
else |
|
721 |
column++; |
|
722 |
} |
|
723 |
return column + 1; // positions are one-based |
|
724 |
} |
|
725 |
return 0; |
|
726 |
} |
|
727 |
||
728 |
public static String format(String fmt, Object... args) { |
|
729 |
return String.format((java.util.Locale)null, fmt, args); |
|
730 |
} |
|
731 |
||
732 |
} |