author | jjg |
Tue, 13 Nov 2012 15:09:15 -0800 | |
changeset 14538 | 384681be798f |
parent 14057 | b4b0377b8dba |
child 15385 | ee1eebe7e210 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
13077 | 2 |
* Copyright (c) 1999, 2012, 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; |
6582
c7a4fb5a2f86
6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents:
6143
diff
changeset
|
32 |
import java.util.Queue; |
10 | 33 |
import java.util.Set; |
34 |
import javax.tools.DiagnosticListener; |
|
35 |
import javax.tools.JavaFileObject; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
167
diff
changeset
|
36 |
|
6721 | 37 |
import com.sun.tools.javac.api.DiagnosticFormatter; |
11314 | 38 |
import com.sun.tools.javac.main.Main; |
39 |
import com.sun.tools.javac.main.Option; |
|
13077 | 40 |
import com.sun.tools.javac.tree.EndPosTable; |
10 | 41 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
42 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; |
|
43 |
||
11314 | 44 |
import static com.sun.tools.javac.main.Option.*; |
6721 | 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 |
||
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
63 |
/* 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
|
64 |
public enum PrefixKind { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
65 |
JAVAC("javac."), |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
66 |
COMPILER_MISC("compiler.misc."); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
67 |
PrefixKind(String v) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
68 |
value = v; |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
69 |
} |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
70 |
public String key(String k) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
71 |
return value + k; |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
72 |
} |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
73 |
final String value; |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
74 |
} |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
75 |
|
14538
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
76 |
/** |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
77 |
* DiagnosticHandler's provide the initial handling for diagnostics. |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
78 |
* When a diagnostic handler is created and has been initialized, it |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
79 |
* should install itself as the current diagnostic handler. When a |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
80 |
* client has finished using a handler, the client should call |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
81 |
* {@code log.removeDiagnosticHandler();} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
82 |
* |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
83 |
* Note that javax.tools.DiagnosticListener (if set) is called later in the |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
84 |
* diagnostic pipeline. |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
85 |
*/ |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
86 |
public static abstract class DiagnosticHandler { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
87 |
/** |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
88 |
* The previously installed diagnostic handler. |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
89 |
*/ |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
90 |
protected DiagnosticHandler prev; |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
91 |
|
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
92 |
/** |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
93 |
* Install this diagnostic handler as the current one, |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
94 |
* recording the previous one. |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
95 |
*/ |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
96 |
protected void install(Log log) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
97 |
prev = log.diagnosticHandler; |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
98 |
log.diagnosticHandler = this; |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
99 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
100 |
|
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
101 |
/** |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
102 |
* Handle a diagnostic. |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
103 |
*/ |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
104 |
public abstract void report(JCDiagnostic diag); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
105 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
106 |
|
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
107 |
/** |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
108 |
* A DiagnosticHandler that discards all diagnostics. |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
109 |
*/ |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
110 |
public static class DiscardDiagnosticHandler extends DiagnosticHandler { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
111 |
public DiscardDiagnosticHandler(Log log) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
112 |
install(log); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
113 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
114 |
|
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
115 |
public void report(JCDiagnostic diag) { } |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
116 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
117 |
|
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
118 |
/** |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
119 |
* A DiagnosticHandler that can defer some or all diagnostics, |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
120 |
* by buffering them for later examination and/or reporting. |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
121 |
* If a diagnostic is not deferred, or is subsequently reported |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
122 |
* with reportAllDiagnostics(), it will be reported to the previously |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
123 |
* active diagnostic handler. |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
124 |
*/ |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
125 |
public static class DeferredDiagnosticHandler extends DiagnosticHandler { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
126 |
private Queue<JCDiagnostic> deferred = ListBuffer.lb(); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
127 |
private final Filter<JCDiagnostic> filter; |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
128 |
|
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
129 |
public DeferredDiagnosticHandler(Log log) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
130 |
this(log, null); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
131 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
132 |
|
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
133 |
public DeferredDiagnosticHandler(Log log, Filter<JCDiagnostic> filter) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
134 |
this.filter = filter; |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
135 |
install(log); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
136 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
137 |
|
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
138 |
public void report(JCDiagnostic diag) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
139 |
if (filter == null || filter.accepts(diag)) |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
140 |
deferred.add(diag); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
141 |
else |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
142 |
prev.report(diag); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
143 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
144 |
|
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
145 |
public Queue<JCDiagnostic> getDiagnostics() { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
146 |
return deferred; |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
147 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
148 |
|
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
149 |
/** Report all deferred diagnostics. */ |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
150 |
public void reportDeferredDiagnostics() { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
151 |
reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class)); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
152 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
153 |
|
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
154 |
/** Report selected deferred diagnostics. */ |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
155 |
public void reportDeferredDiagnostics(Set<JCDiagnostic.Kind> kinds) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
156 |
JCDiagnostic d; |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
157 |
while ((d = deferred.poll()) != null) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
158 |
if (kinds.contains(d.getKind())) |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
159 |
prev.report(d); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
160 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
161 |
deferred = null; // prevent accidental ongoing use |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
162 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
163 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
164 |
|
11052 | 165 |
public enum WriterKind { NOTICE, WARNING, ERROR }; |
166 |
||
167 |
protected PrintWriter errWriter; |
|
10 | 168 |
|
11052 | 169 |
protected PrintWriter warnWriter; |
10 | 170 |
|
11052 | 171 |
protected PrintWriter noticeWriter; |
10 | 172 |
|
173 |
/** The maximum number of errors/warnings that are reported. |
|
174 |
*/ |
|
11052 | 175 |
protected int MaxErrors; |
176 |
protected int MaxWarnings; |
|
10 | 177 |
|
178 |
/** Switch: prompt user on each error. |
|
179 |
*/ |
|
180 |
public boolean promptOnError; |
|
181 |
||
182 |
/** Switch: emit warning messages. |
|
183 |
*/ |
|
184 |
public boolean emitWarnings; |
|
185 |
||
3660 | 186 |
/** Switch: suppress note messages. |
187 |
*/ |
|
188 |
public boolean suppressNotes; |
|
189 |
||
10 | 190 |
/** Print stack trace on errors? |
191 |
*/ |
|
192 |
public boolean dumpOnError; |
|
193 |
||
194 |
/** Print multiple errors for same source locations. |
|
195 |
*/ |
|
196 |
public boolean multipleErrors; |
|
197 |
||
198 |
/** |
|
199 |
* Diagnostic listener, if provided through programmatic |
|
200 |
* interface to javac (JSR 199). |
|
201 |
*/ |
|
202 |
protected DiagnosticListener<? super JavaFileObject> diagListener; |
|
867 | 203 |
|
10 | 204 |
/** |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
205 |
* Formatter for diagnostics. |
10 | 206 |
*/ |
942 | 207 |
private DiagnosticFormatter<JCDiagnostic> diagFormatter; |
10 | 208 |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
209 |
/** |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
210 |
* Keys for expected diagnostics. |
1866 | 211 |
*/ |
212 |
public Set<String> expectDiagKeys; |
|
213 |
||
214 |
/** |
|
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
215 |
* JavacMessages object used for localization. |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
216 |
*/ |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
217 |
private JavacMessages messages; |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
218 |
|
6582
c7a4fb5a2f86
6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents:
6143
diff
changeset
|
219 |
/** |
14538
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
220 |
+ * Handler for initial dispatch of diagnostics. |
6582
c7a4fb5a2f86
6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents:
6143
diff
changeset
|
221 |
*/ |
14538
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
222 |
private DiagnosticHandler diagnosticHandler; |
6582
c7a4fb5a2f86
6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents:
6143
diff
changeset
|
223 |
|
10 | 224 |
/** Construct a log with given I/O redirections. |
225 |
*/ |
|
226 |
protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { |
|
867 | 227 |
super(JCDiagnostic.Factory.instance(context)); |
10 | 228 |
context.put(logKey, this); |
229 |
this.errWriter = errWriter; |
|
230 |
this.warnWriter = warnWriter; |
|
231 |
this.noticeWriter = noticeWriter; |
|
232 |
||
233 |
@SuppressWarnings("unchecked") // FIXME |
|
1866 | 234 |
DiagnosticListener<? super JavaFileObject> dl = |
10 | 235 |
context.get(DiagnosticListener.class); |
1866 | 236 |
this.diagListener = dl; |
237 |
||
14538
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
238 |
diagnosticHandler = new DefaultDiagnosticHandler(); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
239 |
|
11052 | 240 |
messages = JavacMessages.instance(context); |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
241 |
messages.add(Main.javacBundleName); |
11052 | 242 |
|
243 |
final Options options = Options.instance(context); |
|
244 |
initOptions(options); |
|
245 |
options.addListener(new Runnable() { |
|
246 |
public void run() { |
|
247 |
initOptions(options); |
|
248 |
} |
|
249 |
}); |
|
10 | 250 |
} |
251 |
// where |
|
11052 | 252 |
private void initOptions(Options options) { |
253 |
this.dumpOnError = options.isSet(DOE); |
|
254 |
this.promptOnError = options.isSet(PROMPT); |
|
255 |
this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none"); |
|
256 |
this.suppressNotes = options.isSet("suppressNotes"); |
|
257 |
this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors()); |
|
258 |
this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings()); |
|
259 |
||
260 |
boolean rawDiagnostics = options.isSet("rawDiagnostics"); |
|
261 |
this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) : |
|
262 |
new BasicDiagnosticFormatter(options, messages); |
|
263 |
||
264 |
String ek = options.get("expectKeys"); |
|
265 |
if (ek != null) |
|
266 |
expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *"))); |
|
267 |
} |
|
268 |
||
11314 | 269 |
private int getIntOption(Options options, Option option, int defaultValue) { |
270 |
String s = options.get(option); |
|
10 | 271 |
try { |
4699
e58edf3542b3
6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents:
3995
diff
changeset
|
272 |
if (s != null) { |
e58edf3542b3
6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents:
3995
diff
changeset
|
273 |
int n = Integer.parseInt(s); |
e58edf3542b3
6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents:
3995
diff
changeset
|
274 |
return (n <= 0 ? Integer.MAX_VALUE : n); |
e58edf3542b3
6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents:
3995
diff
changeset
|
275 |
} |
10 | 276 |
} catch (NumberFormatException e) { |
277 |
// silently ignore ill-formed numbers |
|
278 |
} |
|
279 |
return defaultValue; |
|
280 |
} |
|
281 |
||
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
282 |
/** Default value for -Xmaxerrs. |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
283 |
*/ |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
284 |
protected int getDefaultMaxErrors() { |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
285 |
return 100; |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
286 |
} |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
287 |
|
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
288 |
/** Default value for -Xmaxwarns. |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
289 |
*/ |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
290 |
protected int getDefaultMaxWarnings() { |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
291 |
return 100; |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
292 |
} |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5847
diff
changeset
|
293 |
|
10 | 294 |
/** The default writer for diagnostics |
295 |
*/ |
|
11052 | 296 |
static PrintWriter defaultWriter(Context context) { |
10 | 297 |
PrintWriter result = context.get(outKey); |
298 |
if (result == null) |
|
299 |
context.put(outKey, result = new PrintWriter(System.err)); |
|
300 |
return result; |
|
301 |
} |
|
302 |
||
303 |
/** Construct a log with default settings. |
|
304 |
*/ |
|
305 |
protected Log(Context context) { |
|
306 |
this(context, defaultWriter(context)); |
|
307 |
} |
|
308 |
||
309 |
/** Construct a log with all output redirected. |
|
310 |
*/ |
|
311 |
protected Log(Context context, PrintWriter defaultWriter) { |
|
312 |
this(context, defaultWriter, defaultWriter, defaultWriter); |
|
313 |
} |
|
314 |
||
315 |
/** Get the Log instance for this context. */ |
|
316 |
public static Log instance(Context context) { |
|
317 |
Log instance = context.get(logKey); |
|
318 |
if (instance == null) |
|
319 |
instance = new Log(context); |
|
320 |
return instance; |
|
321 |
} |
|
322 |
||
323 |
/** The number of errors encountered so far. |
|
324 |
*/ |
|
325 |
public int nerrors = 0; |
|
326 |
||
327 |
/** The number of warnings encountered so far. |
|
328 |
*/ |
|
329 |
public int nwarnings = 0; |
|
330 |
||
331 |
/** A set of all errors generated so far. This is used to avoid printing an |
|
332 |
* error message more than once. For each error, a pair consisting of the |
|
333 |
* source file name and source code position of the error is added to the set. |
|
334 |
*/ |
|
335 |
private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>(); |
|
336 |
||
337 |
public boolean hasDiagnosticListener() { |
|
338 |
return diagListener != null; |
|
339 |
} |
|
340 |
||
11055 | 341 |
public void setEndPosTable(JavaFileObject name, EndPosTable endPosTable) { |
867 | 342 |
name.getClass(); // null check |
11055 | 343 |
getSource(name).setEndPosTable(endPosTable); |
10 | 344 |
} |
345 |
||
1591
e5a618442f5f
6768932: Add support for multiline diagnostics
mcimadamore
parents:
1472
diff
changeset
|
346 |
/** Return current sourcefile. |
10 | 347 |
*/ |
1591
e5a618442f5f
6768932: Add support for multiline diagnostics
mcimadamore
parents:
1472
diff
changeset
|
348 |
public JavaFileObject currentSourceFile() { |
10 | 349 |
return source == null ? null : source.getFile(); |
350 |
} |
|
351 |
||
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
352 |
/** Get the current diagnostic formatter. |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
353 |
*/ |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
354 |
public DiagnosticFormatter<JCDiagnostic> getDiagnosticFormatter() { |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
355 |
return diagFormatter; |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
356 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
357 |
|
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
358 |
/** Set the current diagnostic formatter. |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
359 |
*/ |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
360 |
public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) { |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
361 |
this.diagFormatter = diagFormatter; |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
362 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1866
diff
changeset
|
363 |
|
11052 | 364 |
public PrintWriter getWriter(WriterKind kind) { |
365 |
switch (kind) { |
|
366 |
case NOTICE: return noticeWriter; |
|
367 |
case WARNING: return warnWriter; |
|
368 |
case ERROR: return errWriter; |
|
369 |
default: throw new IllegalArgumentException(); |
|
370 |
} |
|
371 |
} |
|
372 |
||
373 |
public void setWriter(WriterKind kind, PrintWriter pw) { |
|
374 |
pw.getClass(); |
|
375 |
switch (kind) { |
|
376 |
case NOTICE: noticeWriter = pw; break; |
|
377 |
case WARNING: warnWriter = pw; break; |
|
378 |
case ERROR: errWriter = pw; break; |
|
379 |
default: throw new IllegalArgumentException(); |
|
380 |
} |
|
381 |
} |
|
382 |
||
383 |
public void setWriters(PrintWriter pw) { |
|
384 |
pw.getClass(); |
|
385 |
noticeWriter = warnWriter = errWriter = pw; |
|
386 |
} |
|
387 |
||
11316
4dcad625e72e
7111022: javac no long prints last round of processing
jjg
parents:
11314
diff
changeset
|
388 |
public void setWriters(Log other) { |
4dcad625e72e
7111022: javac no long prints last round of processing
jjg
parents:
11314
diff
changeset
|
389 |
this.noticeWriter = other.noticeWriter; |
4dcad625e72e
7111022: javac no long prints last round of processing
jjg
parents:
11314
diff
changeset
|
390 |
this.warnWriter = other.warnWriter; |
4dcad625e72e
7111022: javac no long prints last round of processing
jjg
parents:
11314
diff
changeset
|
391 |
this.errWriter = other.errWriter; |
4dcad625e72e
7111022: javac no long prints last round of processing
jjg
parents:
11314
diff
changeset
|
392 |
} |
4dcad625e72e
7111022: javac no long prints last round of processing
jjg
parents:
11314
diff
changeset
|
393 |
|
13841
a304aa57c550
7196760: tree end positions incorrect after anno processing
jjg
parents:
13077
diff
changeset
|
394 |
public void setSourceMap(Log other) { |
a304aa57c550
7196760: tree end positions incorrect after anno processing
jjg
parents:
13077
diff
changeset
|
395 |
this.sourceMap = other.sourceMap; |
a304aa57c550
7196760: tree end positions incorrect after anno processing
jjg
parents:
13077
diff
changeset
|
396 |
} |
a304aa57c550
7196760: tree end positions incorrect after anno processing
jjg
parents:
13077
diff
changeset
|
397 |
|
14538
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
398 |
/** |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
399 |
* Replace the specified diagnostic handler with the |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
400 |
* handler that was current at the time this handler was created. |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
401 |
* The given handler must be the currently installed handler; |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
402 |
* it must be specified explicitly for clarity and consistency checking. |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
403 |
*/ |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
404 |
public void popDiagnosticHandler(DiagnosticHandler h) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
405 |
Assert.check(diagnosticHandler == h); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
406 |
diagnosticHandler = h.prev; |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
407 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
408 |
|
10 | 409 |
/** Flush the logs |
410 |
*/ |
|
411 |
public void flush() { |
|
412 |
errWriter.flush(); |
|
413 |
warnWriter.flush(); |
|
414 |
noticeWriter.flush(); |
|
415 |
} |
|
416 |
||
11052 | 417 |
public void flush(WriterKind kind) { |
418 |
getWriter(kind).flush(); |
|
419 |
} |
|
420 |
||
10 | 421 |
/** Returns true if an error needs to be reported for a given |
422 |
* source name and pos. |
|
423 |
*/ |
|
424 |
protected boolean shouldReport(JavaFileObject file, int pos) { |
|
425 |
if (multipleErrors || file == null) |
|
426 |
return true; |
|
427 |
||
428 |
Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos); |
|
429 |
boolean shouldReport = !recorded.contains(coords); |
|
430 |
if (shouldReport) |
|
431 |
recorded.add(coords); |
|
432 |
return shouldReport; |
|
433 |
} |
|
434 |
||
435 |
/** Prompt user after an error. |
|
436 |
*/ |
|
437 |
public void prompt() { |
|
438 |
if (promptOnError) { |
|
6143
79b7dee406cc
6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents:
5850
diff
changeset
|
439 |
System.err.println(localize("resume.abort")); |
10 | 440 |
try { |
441 |
while (true) { |
|
442 |
switch (System.in.read()) { |
|
443 |
case 'a': case 'A': |
|
444 |
System.exit(-1); |
|
445 |
return; |
|
446 |
case 'r': case 'R': |
|
447 |
return; |
|
448 |
case 'x': case 'X': |
|
449 |
throw new AssertionError("user abort"); |
|
450 |
default: |
|
451 |
} |
|
452 |
} |
|
453 |
} catch (IOException e) {} |
|
454 |
} |
|
455 |
} |
|
456 |
||
457 |
/** Print the faulty source code line and point to the error. |
|
458 |
* @param pos Buffer index of the error position, must be on current line |
|
459 |
*/ |
|
460 |
private void printErrLine(int pos, PrintWriter writer) { |
|
867 | 461 |
String line = (source == null ? null : source.getLine(pos)); |
462 |
if (line == null) |
|
10 | 463 |
return; |
1109
853d8c191eac
6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents:
942
diff
changeset
|
464 |
int col = source.getColumnNumber(pos, false); |
10 | 465 |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
466 |
printRawLines(writer, line); |
867 | 467 |
for (int i = 0; i < col - 1; i++) { |
468 |
writer.print((line.charAt(i) == '\t') ? "\t" : " "); |
|
10 | 469 |
} |
470 |
writer.println("^"); |
|
471 |
writer.flush(); |
|
472 |
} |
|
473 |
||
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
474 |
public void printNewline() { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
475 |
noticeWriter.println(); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
476 |
} |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
477 |
|
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
478 |
public void printNewline(WriterKind wk) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
479 |
getWriter(wk).println(); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
480 |
} |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
481 |
|
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
482 |
public void printLines(String key, Object... args) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
483 |
printRawLines(noticeWriter, localize(key, args)); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
484 |
} |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
485 |
|
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
486 |
public void printLines(PrefixKind pk, String key, Object... args) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
487 |
printRawLines(noticeWriter, localize(pk, key, args)); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
488 |
} |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
489 |
|
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
490 |
public void printLines(WriterKind wk, String key, Object... args) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
491 |
printRawLines(getWriter(wk), localize(key, args)); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
492 |
} |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
493 |
|
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
494 |
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
|
495 |
printRawLines(getWriter(wk), localize(pk, key, args)); |
11052 | 496 |
} |
497 |
||
498 |
/** Print the text of a message, translating newlines appropriately |
|
499 |
* for the platform. |
|
500 |
*/ |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
501 |
public void printRawLines(String msg) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
502 |
printRawLines(noticeWriter, msg); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
503 |
} |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
504 |
|
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
505 |
/** Print the text of a message, translating newlines appropriately |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
506 |
* for the platform. |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
507 |
*/ |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
508 |
public void printRawLines(WriterKind kind, String msg) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
509 |
printRawLines(getWriter(kind), msg); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
510 |
} |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
511 |
|
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
512 |
/** Print the text of a message, translating newlines appropriately |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
513 |
* for the platform. |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
514 |
*/ |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
515 |
public static void printRawLines(PrintWriter writer, String msg) { |
10 | 516 |
int nl; |
517 |
while ((nl = msg.indexOf('\n')) != -1) { |
|
518 |
writer.println(msg.substring(0, nl)); |
|
519 |
msg = msg.substring(nl+1); |
|
520 |
} |
|
521 |
if (msg.length() != 0) writer.println(msg); |
|
522 |
} |
|
523 |
||
8630 | 524 |
/** |
525 |
* Print the localized text of a "verbose" message to the |
|
526 |
* noticeWriter stream. |
|
527 |
*/ |
|
528 |
public void printVerbose(String key, Object... args) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
529 |
printRawLines(noticeWriter, localize("verbose." + key, args)); |
8630 | 530 |
} |
531 |
||
867 | 532 |
protected void directError(String key, Object... args) { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
533 |
printRawLines(errWriter, localize(key, args)); |
867 | 534 |
errWriter.flush(); |
10 | 535 |
} |
536 |
||
537 |
/** Report a warning that cannot be suppressed. |
|
538 |
* @param pos The source position at which to report the warning. |
|
539 |
* @param key The key for the localized warning message. |
|
540 |
* @param args Fields of the warning message. |
|
541 |
*/ |
|
542 |
public void strictWarning(DiagnosticPosition pos, String key, Object ... args) { |
|
543 |
writeDiagnostic(diags.warning(source, pos, key, args)); |
|
544 |
nwarnings++; |
|
545 |
} |
|
546 |
||
14538
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
547 |
/** |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
548 |
* Primary method to report a diagnostic. |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
549 |
* @param diagnostic |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
550 |
*/ |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
551 |
public void report(JCDiagnostic diagnostic) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
552 |
diagnosticHandler.report(diagnostic); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
553 |
} |
6582
c7a4fb5a2f86
6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents:
6143
diff
changeset
|
554 |
|
10 | 555 |
/** |
556 |
* Common diagnostic handling. |
|
557 |
* The diagnostic is counted, and depending on the options and how many diagnostics have been |
|
558 |
* reported so far, the diagnostic may be handed off to writeDiagnostic. |
|
559 |
*/ |
|
14538
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
560 |
private class DefaultDiagnosticHandler extends DiagnosticHandler { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
561 |
public void report(JCDiagnostic diagnostic) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
562 |
if (expectDiagKeys != null) |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
563 |
expectDiagKeys.remove(diagnostic.getCode()); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
564 |
|
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
565 |
switch (diagnostic.getType()) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
566 |
case FRAGMENT: |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
567 |
throw new IllegalArgumentException(); |
6582
c7a4fb5a2f86
6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents:
6143
diff
changeset
|
568 |
|
14538
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
569 |
case NOTE: |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
570 |
// Print out notes only when we are permitted to report warnings |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
571 |
// Notes are only generated at the end of a compilation, so should be small |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
572 |
// in number. |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
573 |
if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
574 |
writeDiagnostic(diagnostic); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
575 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
576 |
break; |
10 | 577 |
|
14538
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
578 |
case WARNING: |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
579 |
if (emitWarnings || diagnostic.isMandatory()) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
580 |
if (nwarnings < MaxWarnings) { |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
581 |
writeDiagnostic(diagnostic); |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
582 |
nwarnings++; |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
583 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
584 |
} |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
585 |
break; |
10 | 586 |
|
14538
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
587 |
case ERROR: |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
588 |
if (nerrors < MaxErrors |
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
589 |
&& shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) { |
10 | 590 |
writeDiagnostic(diagnostic); |
14538
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
591 |
nerrors++; |
10 | 592 |
} |
14538
384681be798f
8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents:
14057
diff
changeset
|
593 |
break; |
10 | 594 |
} |
595 |
} |
|
596 |
} |
|
597 |
||
598 |
/** |
|
599 |
* Write out a diagnostic. |
|
600 |
*/ |
|
601 |
protected void writeDiagnostic(JCDiagnostic diag) { |
|
602 |
if (diagListener != null) { |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8630
diff
changeset
|
603 |
diagListener.report(diag); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8630
diff
changeset
|
604 |
return; |
10 | 605 |
} |
606 |
||
607 |
PrintWriter writer = getWriterForDiagnosticType(diag.getType()); |
|
608 |
||
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
609 |
printRawLines(writer, diagFormatter.format(diag, messages.getCurrentLocale())); |
10 | 610 |
|
611 |
if (promptOnError) { |
|
612 |
switch (diag.getType()) { |
|
613 |
case ERROR: |
|
614 |
case WARNING: |
|
615 |
prompt(); |
|
616 |
} |
|
617 |
} |
|
618 |
||
619 |
if (dumpOnError) |
|
620 |
new RuntimeException().printStackTrace(writer); |
|
621 |
||
622 |
writer.flush(); |
|
623 |
} |
|
624 |
||
625 |
@Deprecated |
|
626 |
protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) { |
|
627 |
switch (dt) { |
|
628 |
case FRAGMENT: |
|
629 |
throw new IllegalArgumentException(); |
|
630 |
||
631 |
case NOTE: |
|
632 |
return noticeWriter; |
|
633 |
||
634 |
case WARNING: |
|
635 |
return warnWriter; |
|
636 |
||
637 |
case ERROR: |
|
638 |
return errWriter; |
|
639 |
||
640 |
default: |
|
641 |
throw new Error(); |
|
642 |
} |
|
643 |
} |
|
644 |
||
645 |
/** 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
|
646 |
* 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
|
647 |
* Use localize(key, args) when possible. |
10 | 648 |
* @param key The key for the localized string. |
649 |
* @param args Fields to substitute into the string. |
|
650 |
*/ |
|
651 |
public static String getLocalizedString(String key, Object ... args) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
652 |
return JavacMessages.getDefaultLocalizedString(PrefixKind.COMPILER_MISC.key(key), args); |
10 | 653 |
} |
654 |
||
6143
79b7dee406cc
6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents:
5850
diff
changeset
|
655 |
/** 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
|
656 |
* @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
|
657 |
* @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
|
658 |
*/ |
79b7dee406cc
6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents:
5850
diff
changeset
|
659 |
public String localize(String key, Object... args) { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
660 |
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
|
661 |
} |
79b7dee406cc
6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents:
5850
diff
changeset
|
662 |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
663 |
/** Find a localized string in the resource bundle. |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
664 |
* @param key The key for the localized string. |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
665 |
* @param args Fields to substitute into the string. |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
666 |
*/ |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
667 |
public String localize(PrefixKind pk, String key, Object... args) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
668 |
if (useRawMessages) |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
669 |
return pk.key(key); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
670 |
else |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
671 |
return messages.getLocalizedString(pk.key(key), args); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
672 |
} |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
673 |
// where |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
674 |
// backdoor hook for testing, should transition to use -XDrawDiagnostics |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
675 |
private static boolean useRawMessages = false; |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
676 |
|
10 | 677 |
/*************************************************************************** |
678 |
* raw error messages without internationalization; used for experimentation |
|
679 |
* and quick prototyping |
|
680 |
***************************************************************************/ |
|
681 |
||
867 | 682 |
/** print an error or warning message: |
683 |
*/ |
|
10 | 684 |
private void printRawError(int pos, String msg) { |
867 | 685 |
if (source == null || pos == Position.NOPOS) { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
686 |
printRawLines(errWriter, "error: " + msg); |
10 | 687 |
} else { |
867 | 688 |
int line = source.getLineNumber(pos); |
1591
e5a618442f5f
6768932: Add support for multiline diagnostics
mcimadamore
parents:
1472
diff
changeset
|
689 |
JavaFileObject file = source.getFile(); |
10 | 690 |
if (file != null) |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
691 |
printRawLines(errWriter, |
3995
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3660
diff
changeset
|
692 |
file.getName() + ":" + |
10 | 693 |
line + ": " + msg); |
694 |
printErrLine(pos, errWriter); |
|
695 |
} |
|
696 |
errWriter.flush(); |
|
697 |
} |
|
698 |
||
867 | 699 |
/** report an error: |
700 |
*/ |
|
10 | 701 |
public void rawError(int pos, String msg) { |
1591
e5a618442f5f
6768932: Add support for multiline diagnostics
mcimadamore
parents:
1472
diff
changeset
|
702 |
if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) { |
10 | 703 |
printRawError(pos, msg); |
704 |
prompt(); |
|
705 |
nerrors++; |
|
706 |
} |
|
707 |
errWriter.flush(); |
|
708 |
} |
|
709 |
||
867 | 710 |
/** report a warning: |
711 |
*/ |
|
10 | 712 |
public void rawWarning(int pos, String msg) { |
713 |
if (nwarnings < MaxWarnings && emitWarnings) { |
|
714 |
printRawError(pos, "warning: " + msg); |
|
715 |
} |
|
716 |
prompt(); |
|
717 |
nwarnings++; |
|
718 |
errWriter.flush(); |
|
719 |
} |
|
720 |
||
721 |
public static String format(String fmt, Object... args) { |
|
722 |
return String.format((java.util.Locale)null, fmt, args); |
|
723 |
} |
|
724 |
||
725 |
} |