author | jjg |
Fri, 11 Jun 2010 17:24:23 -0700 | |
changeset 5850 | 6f095ff5b469 |
parent 5847 | 1908176fd6e3 |
child 6143 | 79b7dee406cc |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.util; |
|
27 |
||
28 |
import java.io.*; |
|
1866 | 29 |
import java.util.Arrays; |
10 | 30 |
import java.util.HashSet; |
31 |
import java.util.Map; |
|
32 |
import java.util.Set; |
|
33 |
import javax.tools.DiagnosticListener; |
|
34 |
import javax.tools.JavaFileObject; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
167
diff
changeset
|
35 |
|
10 | 36 |
import com.sun.tools.javac.tree.JCTree; |
942 | 37 |
import com.sun.tools.javac.api.DiagnosticFormatter; |
10 | 38 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
39 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; |
|
40 |
||
41 |
/** A class for error logs. Reports errors and warnings, and |
|
42 |
* keeps track of error numbers and positions. |
|
43 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
44 |
* <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
|
45 |
* If you write code that depends on this, you do so at your own risk. |
10 | 46 |
* This code and its internal interfaces are subject to change or |
47 |
* deletion without notice.</b> |
|
48 |
*/ |
|
867 | 49 |
public class Log extends AbstractLog { |
10 | 50 |
/** The context key for the log. */ |
51 |
public static final Context.Key<Log> logKey |
|
52 |
= new Context.Key<Log>(); |
|
53 |
||
54 |
/** The context key for the output PrintWriter. */ |
|
55 |
public static final Context.Key<PrintWriter> outKey = |
|
56 |
new Context.Key<PrintWriter>(); |
|
57 |
||
58 |
//@Deprecated |
|
59 |
public final PrintWriter errWriter; |
|
60 |
||
61 |
//@Deprecated |
|
62 |
public final PrintWriter warnWriter; |
|
63 |
||
64 |
//@Deprecated |
|
65 |
public final PrintWriter noticeWriter; |
|
66 |
||
67 |
/** The maximum number of errors/warnings that are reported. |
|
68 |
*/ |
|
69 |
public final int MaxErrors; |
|
70 |
public final int MaxWarnings; |
|
71 |
||
72 |
/** Switch: prompt user on each error. |
|
73 |
*/ |
|
74 |
public boolean promptOnError; |
|
75 |
||
76 |
/** Switch: emit warning messages. |
|
77 |
*/ |
|
78 |
public boolean emitWarnings; |
|
79 |
||
3660 | 80 |
/** Switch: suppress note messages. |
81 |
*/ |
|
82 |
public boolean suppressNotes; |
|
83 |
||
10 | 84 |
/** Print stack trace on errors? |
85 |
*/ |
|
86 |
public boolean dumpOnError; |
|
87 |
||
88 |
/** Print multiple errors for same source locations. |
|
89 |
*/ |
|
90 |
public boolean multipleErrors; |
|
91 |
||
92 |
/** |
|
93 |
* Diagnostic listener, if provided through programmatic |
|
94 |
* interface to javac (JSR 199). |
|
95 |
*/ |
|
96 |
protected DiagnosticListener<? super JavaFileObject> diagListener; |
|
867 | 97 |
|
10 | 98 |
/** |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
99 |
* Formatter for diagnostics. |
10 | 100 |
*/ |
942 | 101 |
private DiagnosticFormatter<JCDiagnostic> diagFormatter; |
10 | 102 |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
103 |
/** |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
104 |
* Keys for expected diagnostics. |
1866 | 105 |
*/ |
106 |
public Set<String> expectDiagKeys; |
|
107 |
||
108 |
/** |
|
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
109 |
* JavacMessages object used for localization. |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
110 |
*/ |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
111 |
private JavacMessages messages; |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
112 |
|
10 | 113 |
/** Construct a log with given I/O redirections. |
114 |
*/ |
|
115 |
@Deprecated |
|
116 |
protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { |
|
867 | 117 |
super(JCDiagnostic.Factory.instance(context)); |
10 | 118 |
context.put(logKey, this); |
119 |
this.errWriter = errWriter; |
|
120 |
this.warnWriter = warnWriter; |
|
121 |
this.noticeWriter = noticeWriter; |
|
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; |
|
3660 | 127 |
this.suppressNotes = options.get("suppressNotes") != null; |
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
128 |
this.MaxErrors = getIntOption(options, "-Xmaxerrs", getDefaultMaxErrors()); |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
129 |
this.MaxWarnings = getIntOption(options, "-Xmaxwarns", getDefaultMaxWarnings()); |
10 | 130 |
|
942 | 131 |
boolean rawDiagnostics = options.get("rawDiagnostics") != null; |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
132 |
messages = JavacMessages.instance(context); |
1472
1e09e143438c
6731573: diagnostic output should optionally include source line
mcimadamore
parents:
1471
diff
changeset
|
133 |
this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) : |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
134 |
new BasicDiagnosticFormatter(options, messages); |
10 | 135 |
@SuppressWarnings("unchecked") // FIXME |
1866 | 136 |
DiagnosticListener<? super JavaFileObject> dl = |
10 | 137 |
context.get(DiagnosticListener.class); |
1866 | 138 |
this.diagListener = dl; |
139 |
||
140 |
String ek = options.get("expectKeys"); |
|
141 |
if (ek != null) |
|
142 |
expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *"))); |
|
10 | 143 |
} |
144 |
// where |
|
145 |
private int getIntOption(Options options, String optionName, int defaultValue) { |
|
146 |
String s = options.get(optionName); |
|
147 |
try { |
|
4699
e58edf3542b3
6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents:
3995
diff
changeset
|
148 |
if (s != null) { |
e58edf3542b3
6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents:
3995
diff
changeset
|
149 |
int n = Integer.parseInt(s); |
e58edf3542b3
6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents:
3995
diff
changeset
|
150 |
return (n <= 0 ? Integer.MAX_VALUE : n); |
e58edf3542b3
6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents:
3995
diff
changeset
|
151 |
} |
10 | 152 |
} catch (NumberFormatException e) { |
153 |
// silently ignore ill-formed numbers |
|
154 |
} |
|
155 |
return defaultValue; |
|
156 |
} |
|
157 |
||
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
158 |
/** Default value for -Xmaxerrs. |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
159 |
*/ |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
160 |
protected int getDefaultMaxErrors() { |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
161 |
return 100; |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
162 |
} |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
163 |
|
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
164 |
/** Default value for -Xmaxwarns. |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
165 |
*/ |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
166 |
protected int getDefaultMaxWarnings() { |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
167 |
return 100; |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
168 |
} |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
169 |
|
10 | 170 |
/** The default writer for diagnostics |
171 |
*/ |
|
172 |
static final PrintWriter defaultWriter(Context context) { |
|
173 |
PrintWriter result = context.get(outKey); |
|
174 |
if (result == null) |
|
175 |
context.put(outKey, result = new PrintWriter(System.err)); |
|
176 |
return result; |
|
177 |
} |
|
178 |
||
179 |
/** Construct a log with default settings. |
|
180 |
*/ |
|
181 |
protected Log(Context context) { |
|
182 |
this(context, defaultWriter(context)); |
|
183 |
} |
|
184 |
||
185 |
/** Construct a log with all output redirected. |
|
186 |
*/ |
|
187 |
protected Log(Context context, PrintWriter defaultWriter) { |
|
188 |
this(context, defaultWriter, defaultWriter, defaultWriter); |
|
189 |
} |
|
190 |
||
191 |
/** Get the Log instance for this context. */ |
|
192 |
public static Log instance(Context context) { |
|
193 |
Log instance = context.get(logKey); |
|
194 |
if (instance == null) |
|
195 |
instance = new Log(context); |
|
196 |
return instance; |
|
197 |
} |
|
198 |
||
199 |
/** The number of errors encountered so far. |
|
200 |
*/ |
|
201 |
public int nerrors = 0; |
|
202 |
||
203 |
/** The number of warnings encountered so far. |
|
204 |
*/ |
|
205 |
public int nwarnings = 0; |
|
206 |
||
5001
71ed0cc22974
6511613: javac unexpectedly doesn't fail in some cases if an annotation processor specified
jjg
parents:
4699
diff
changeset
|
207 |
/** |
71ed0cc22974
6511613: javac unexpectedly doesn't fail in some cases if an annotation processor specified
jjg
parents:
4699
diff
changeset
|
208 |
* Whether or not an unrecoverable error has been seen. |
71ed0cc22974
6511613: javac unexpectedly doesn't fail in some cases if an annotation processor specified
jjg
parents:
4699
diff
changeset
|
209 |
* Unrecoverable errors prevent subsequent annotation processing. |
71ed0cc22974
6511613: javac unexpectedly doesn't fail in some cases if an annotation processor specified
jjg
parents:
4699
diff
changeset
|
210 |
*/ |
71ed0cc22974
6511613: javac unexpectedly doesn't fail in some cases if an annotation processor specified
jjg
parents:
4699
diff
changeset
|
211 |
public boolean unrecoverableError; |
71ed0cc22974
6511613: javac unexpectedly doesn't fail in some cases if an annotation processor specified
jjg
parents:
4699
diff
changeset
|
212 |
|
10 | 213 |
/** A set of all errors generated so far. This is used to avoid printing an |
214 |
* error message more than once. For each error, a pair consisting of the |
|
215 |
* source file name and source code position of the error is added to the set. |
|
216 |
*/ |
|
217 |
private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>(); |
|
218 |
||
219 |
public boolean hasDiagnosticListener() { |
|
220 |
return diagListener != null; |
|
221 |
} |
|
222 |
||
223 |
public void setEndPosTable(JavaFileObject name, Map<JCTree, Integer> table) { |
|
867 | 224 |
name.getClass(); // null check |
225 |
getSource(name).setEndPosTable(table); |
|
10 | 226 |
} |
227 |
||
1591
e5a618442f5f
6768932: Add support for multiline diagnostics
mcimadamore
parents:
1472
diff
changeset
|
228 |
/** Return current sourcefile. |
10 | 229 |
*/ |
1591
e5a618442f5f
6768932: Add support for multiline diagnostics
mcimadamore
parents:
1472
diff
changeset
|
230 |
public JavaFileObject currentSourceFile() { |
10 | 231 |
return source == null ? null : source.getFile(); |
232 |
} |
|
233 |
||
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
234 |
/** Get the current diagnostic formatter. |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
235 |
*/ |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
236 |
public DiagnosticFormatter<JCDiagnostic> getDiagnosticFormatter() { |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
237 |
return diagFormatter; |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
238 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
239 |
|
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
240 |
/** Set the current diagnostic formatter. |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
241 |
*/ |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
242 |
public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) { |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
243 |
this.diagFormatter = diagFormatter; |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
244 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
245 |
|
10 | 246 |
/** Flush the logs |
247 |
*/ |
|
248 |
public void flush() { |
|
249 |
errWriter.flush(); |
|
250 |
warnWriter.flush(); |
|
251 |
noticeWriter.flush(); |
|
252 |
} |
|
253 |
||
254 |
/** Returns true if an error needs to be reported for a given |
|
255 |
* source name and pos. |
|
256 |
*/ |
|
257 |
protected boolean shouldReport(JavaFileObject file, int pos) { |
|
258 |
if (multipleErrors || file == null) |
|
259 |
return true; |
|
260 |
||
261 |
Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos); |
|
262 |
boolean shouldReport = !recorded.contains(coords); |
|
263 |
if (shouldReport) |
|
264 |
recorded.add(coords); |
|
265 |
return shouldReport; |
|
266 |
} |
|
267 |
||
268 |
/** Prompt user after an error. |
|
269 |
*/ |
|
270 |
public void prompt() { |
|
271 |
if (promptOnError) { |
|
272 |
System.err.println(getLocalizedString("resume.abort")); |
|
273 |
char ch; |
|
274 |
try { |
|
275 |
while (true) { |
|
276 |
switch (System.in.read()) { |
|
277 |
case 'a': case 'A': |
|
278 |
System.exit(-1); |
|
279 |
return; |
|
280 |
case 'r': case 'R': |
|
281 |
return; |
|
282 |
case 'x': case 'X': |
|
283 |
throw new AssertionError("user abort"); |
|
284 |
default: |
|
285 |
} |
|
286 |
} |
|
287 |
} catch (IOException e) {} |
|
288 |
} |
|
289 |
} |
|
290 |
||
291 |
/** Print the faulty source code line and point to the error. |
|
292 |
* @param pos Buffer index of the error position, must be on current line |
|
293 |
*/ |
|
294 |
private void printErrLine(int pos, PrintWriter writer) { |
|
867 | 295 |
String line = (source == null ? null : source.getLine(pos)); |
296 |
if (line == null) |
|
10 | 297 |
return; |
1109
853d8c191eac
6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents:
942
diff
changeset
|
298 |
int col = source.getColumnNumber(pos, false); |
10 | 299 |
|
867 | 300 |
printLines(writer, line); |
301 |
for (int i = 0; i < col - 1; i++) { |
|
302 |
writer.print((line.charAt(i) == '\t') ? "\t" : " "); |
|
10 | 303 |
} |
304 |
writer.println("^"); |
|
305 |
writer.flush(); |
|
306 |
} |
|
307 |
||
308 |
/** Print the text of a message, translating newlines appropriately |
|
309 |
* for the platform. |
|
310 |
*/ |
|
311 |
public static void printLines(PrintWriter writer, String msg) { |
|
312 |
int nl; |
|
313 |
while ((nl = msg.indexOf('\n')) != -1) { |
|
314 |
writer.println(msg.substring(0, nl)); |
|
315 |
msg = msg.substring(nl+1); |
|
316 |
} |
|
317 |
if (msg.length() != 0) writer.println(msg); |
|
318 |
} |
|
319 |
||
867 | 320 |
protected void directError(String key, Object... args) { |
321 |
printLines(errWriter, getLocalizedString(key, args)); |
|
322 |
errWriter.flush(); |
|
10 | 323 |
} |
324 |
||
325 |
/** Report a warning that cannot be suppressed. |
|
326 |
* @param pos The source position at which to report the warning. |
|
327 |
* @param key The key for the localized warning message. |
|
328 |
* @param args Fields of the warning message. |
|
329 |
*/ |
|
330 |
public void strictWarning(DiagnosticPosition pos, String key, Object ... args) { |
|
331 |
writeDiagnostic(diags.warning(source, pos, key, args)); |
|
332 |
nwarnings++; |
|
333 |
} |
|
334 |
||
335 |
/** |
|
336 |
* Common diagnostic handling. |
|
337 |
* The diagnostic is counted, and depending on the options and how many diagnostics have been |
|
338 |
* reported so far, the diagnostic may be handed off to writeDiagnostic. |
|
339 |
*/ |
|
340 |
public void report(JCDiagnostic diagnostic) { |
|
1866 | 341 |
if (expectDiagKeys != null) |
342 |
expectDiagKeys.remove(diagnostic.getCode()); |
|
343 |
||
10 | 344 |
switch (diagnostic.getType()) { |
345 |
case FRAGMENT: |
|
346 |
throw new IllegalArgumentException(); |
|
347 |
||
348 |
case NOTE: |
|
349 |
// Print out notes only when we are permitted to report warnings |
|
350 |
// Notes are only generated at the end of a compilation, so should be small |
|
351 |
// in number. |
|
3660 | 352 |
if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) { |
10 | 353 |
writeDiagnostic(diagnostic); |
354 |
} |
|
355 |
break; |
|
356 |
||
357 |
case WARNING: |
|
358 |
if (emitWarnings || diagnostic.isMandatory()) { |
|
359 |
if (nwarnings < MaxWarnings) { |
|
360 |
writeDiagnostic(diagnostic); |
|
361 |
nwarnings++; |
|
362 |
} |
|
363 |
} |
|
364 |
break; |
|
365 |
||
366 |
case ERROR: |
|
367 |
if (nerrors < MaxErrors |
|
368 |
&& shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) { |
|
369 |
writeDiagnostic(diagnostic); |
|
370 |
nerrors++; |
|
371 |
} |
|
372 |
break; |
|
373 |
} |
|
374 |
} |
|
375 |
||
376 |
/** |
|
377 |
* Write out a diagnostic. |
|
378 |
*/ |
|
379 |
protected void writeDiagnostic(JCDiagnostic diag) { |
|
380 |
if (diagListener != null) { |
|
381 |
try { |
|
382 |
diagListener.report(diag); |
|
383 |
return; |
|
384 |
} |
|
385 |
catch (Throwable t) { |
|
386 |
throw new ClientCodeException(t); |
|
387 |
} |
|
388 |
} |
|
389 |
||
390 |
PrintWriter writer = getWriterForDiagnosticType(diag.getType()); |
|
391 |
||
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
392 |
printLines(writer, diagFormatter.format(diag, messages.getCurrentLocale())); |
10 | 393 |
|
394 |
if (promptOnError) { |
|
395 |
switch (diag.getType()) { |
|
396 |
case ERROR: |
|
397 |
case WARNING: |
|
398 |
prompt(); |
|
399 |
} |
|
400 |
} |
|
401 |
||
402 |
if (dumpOnError) |
|
403 |
new RuntimeException().printStackTrace(writer); |
|
404 |
||
405 |
writer.flush(); |
|
406 |
} |
|
407 |
||
408 |
@Deprecated |
|
409 |
protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) { |
|
410 |
switch (dt) { |
|
411 |
case FRAGMENT: |
|
412 |
throw new IllegalArgumentException(); |
|
413 |
||
414 |
case NOTE: |
|
415 |
return noticeWriter; |
|
416 |
||
417 |
case WARNING: |
|
418 |
return warnWriter; |
|
419 |
||
420 |
case ERROR: |
|
421 |
return errWriter; |
|
422 |
||
423 |
default: |
|
424 |
throw new Error(); |
|
425 |
} |
|
426 |
} |
|
427 |
||
428 |
/** Find a localized string in the resource bundle. |
|
429 |
* @param key The key for the localized string. |
|
430 |
* @param args Fields to substitute into the string. |
|
431 |
*/ |
|
432 |
public static String getLocalizedString(String key, Object ... args) { |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
433 |
return JavacMessages.getDefaultLocalizedString("compiler.misc." + key, args); |
10 | 434 |
} |
435 |
||
436 |
/*************************************************************************** |
|
437 |
* raw error messages without internationalization; used for experimentation |
|
438 |
* and quick prototyping |
|
439 |
***************************************************************************/ |
|
440 |
||
867 | 441 |
/** print an error or warning message: |
442 |
*/ |
|
10 | 443 |
private void printRawError(int pos, String msg) { |
867 | 444 |
if (source == null || pos == Position.NOPOS) { |
10 | 445 |
printLines(errWriter, "error: " + msg); |
446 |
} else { |
|
867 | 447 |
int line = source.getLineNumber(pos); |
1591
e5a618442f5f
6768932: Add support for multiline diagnostics
mcimadamore
parents:
1472
diff
changeset
|
448 |
JavaFileObject file = source.getFile(); |
10 | 449 |
if (file != null) |
450 |
printLines(errWriter, |
|
3995
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3660
diff
changeset
|
451 |
file.getName() + ":" + |
10 | 452 |
line + ": " + msg); |
453 |
printErrLine(pos, errWriter); |
|
454 |
} |
|
455 |
errWriter.flush(); |
|
456 |
} |
|
457 |
||
867 | 458 |
/** report an error: |
459 |
*/ |
|
10 | 460 |
public void rawError(int pos, String msg) { |
1591
e5a618442f5f
6768932: Add support for multiline diagnostics
mcimadamore
parents:
1472
diff
changeset
|
461 |
if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) { |
10 | 462 |
printRawError(pos, msg); |
463 |
prompt(); |
|
464 |
nerrors++; |
|
465 |
} |
|
466 |
errWriter.flush(); |
|
467 |
} |
|
468 |
||
867 | 469 |
/** report a warning: |
470 |
*/ |
|
10 | 471 |
public void rawWarning(int pos, String msg) { |
472 |
if (nwarnings < MaxWarnings && emitWarnings) { |
|
473 |
printRawError(pos, "warning: " + msg); |
|
474 |
} |
|
475 |
prompt(); |
|
476 |
nwarnings++; |
|
477 |
errWriter.flush(); |
|
478 |
} |
|
479 |
||
480 |
public static String format(String fmt, Object... args) { |
|
481 |
return String.format((java.util.Locale)null, fmt, args); |
|
482 |
} |
|
483 |
||
484 |
} |