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