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