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