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