author | briangoetz |
Wed, 18 Dec 2013 10:29:25 -0500 | |
changeset 22159 | 682da512ec17 |
parent 14259 | fb94a1df0d53 |
child 22163 | 3651128c74eb |
permissions | -rw-r--r-- |
867 | 1 |
/* |
14259 | 2 |
* Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. |
867 | 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 |
867 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
867 | 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. |
|
867 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.util; |
|
27 |
||
28 |
import java.util.HashMap; |
|
29 |
import java.util.Map; |
|
30 |
import javax.tools.JavaFileObject; |
|
31 |
||
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
32 |
import com.sun.tools.javac.code.Lint.LintCategory; |
7076
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
33 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag; |
867 | 34 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
35 |
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition; |
|
36 |
||
37 |
||
38 |
/** |
|
39 |
* A base class for error logs. Reports errors and warnings, and |
|
40 |
* keeps track of error numbers and positions. |
|
41 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
42 |
* <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
|
43 |
* If you write code that depends on this, you do so at your own risk. |
867 | 44 |
* This code and its internal interfaces are subject to change or |
45 |
* deletion without notice.</b> |
|
46 |
*/ |
|
47 |
public abstract class AbstractLog { |
|
48 |
AbstractLog(JCDiagnostic.Factory diags) { |
|
49 |
this.diags = diags; |
|
50 |
sourceMap = new HashMap<JavaFileObject, DiagnosticSource>(); |
|
51 |
} |
|
52 |
||
53 |
/** Re-assign source, returning previous setting. |
|
54 |
*/ |
|
55 |
public JavaFileObject useSource(JavaFileObject file) { |
|
56 |
JavaFileObject prev = (source == null ? null : source.getFile()); |
|
57 |
source = getSource(file); |
|
58 |
return prev; |
|
59 |
} |
|
60 |
||
61 |
protected DiagnosticSource getSource(JavaFileObject file) { |
|
62 |
if (file == null) |
|
3144
202fa249dc34
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents:
1591
diff
changeset
|
63 |
return DiagnosticSource.NO_SOURCE; |
867 | 64 |
DiagnosticSource s = sourceMap.get(file); |
65 |
if (s == null) { |
|
66 |
s = new DiagnosticSource(file, this); |
|
67 |
sourceMap.put(file, s); |
|
68 |
} |
|
69 |
return s; |
|
70 |
} |
|
71 |
||
1591 | 72 |
/** Return the underlying diagnostic source |
73 |
*/ |
|
74 |
public DiagnosticSource currentSource() { |
|
75 |
return source; |
|
76 |
} |
|
77 |
||
867 | 78 |
/** Report an error, unless another error was already reported at same |
79 |
* source position. |
|
80 |
* @param key The key for the localized error message. |
|
81 |
* @param args Fields of the error message. |
|
82 |
*/ |
|
83 |
public void error(String key, Object ... args) { |
|
84 |
report(diags.error(source, null, key, args)); |
|
85 |
} |
|
86 |
||
87 |
/** Report an error, unless another error was already reported at same |
|
88 |
* source position. |
|
89 |
* @param pos The source position at which to report the error. |
|
90 |
* @param key The key for the localized error message. |
|
91 |
* @param args Fields of the error message. |
|
92 |
*/ |
|
93 |
public void error(DiagnosticPosition pos, String key, Object ... args) { |
|
94 |
report(diags.error(source, pos, key, args)); |
|
95 |
} |
|
96 |
||
97 |
/** Report an error, unless another error was already reported at same |
|
98 |
* source position. |
|
10455
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
7681
diff
changeset
|
99 |
* @param flag A flag to set on the diagnostic |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
7681
diff
changeset
|
100 |
* @param pos The source position at which to report the error. |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
7681
diff
changeset
|
101 |
* @param key The key for the localized error message. |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
7681
diff
changeset
|
102 |
* @param args Fields of the error message. |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
7681
diff
changeset
|
103 |
*/ |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
7681
diff
changeset
|
104 |
public void error(DiagnosticFlag flag, DiagnosticPosition pos, String key, Object ... args) { |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
7681
diff
changeset
|
105 |
JCDiagnostic d = diags.error(source, pos, key, args); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
7681
diff
changeset
|
106 |
d.setFlag(flag); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
7681
diff
changeset
|
107 |
report(d); |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
7681
diff
changeset
|
108 |
} |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
7681
diff
changeset
|
109 |
|
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
7681
diff
changeset
|
110 |
/** Report an error, unless another error was already reported at same |
3d070be0fff8
7073631: (javac) javac parser improvements for error position reporting
ksrini
parents:
7681
diff
changeset
|
111 |
* source position. |
867 | 112 |
* @param pos The source position at which to report the error. |
113 |
* @param key The key for the localized error message. |
|
114 |
* @param args Fields of the error message. |
|
115 |
*/ |
|
116 |
public void error(int pos, String key, Object ... args) { |
|
117 |
report(diags.error(source, wrap(pos), key, args)); |
|
118 |
} |
|
119 |
||
7076
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
120 |
/** Report an error, unless another error was already reported at same |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
121 |
* source position. |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
122 |
* @param flag A flag to set on the diagnostic |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
123 |
* @param pos The source position at which to report the error. |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
124 |
* @param key The key for the localized error message. |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
125 |
* @param args Fields of the error message. |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
126 |
*/ |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
127 |
public void error(DiagnosticFlag flag, int pos, String key, Object ... args) { |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
128 |
JCDiagnostic d = diags.error(source, wrap(pos), key, args); |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
129 |
d.setFlag(flag); |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
130 |
report(d); |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
131 |
} |
c96fa26247c8
6994946: option to specify only syntax errors as unrecoverable
jjg
parents:
6151
diff
changeset
|
132 |
|
867 | 133 |
/** Report a warning, unless suppressed by the -nowarn option or the |
134 |
* maximum number of warnings has been reached. |
|
135 |
* @param key The key for the localized warning message. |
|
136 |
* @param args Fields of the warning message. |
|
137 |
*/ |
|
138 |
public void warning(String key, Object ... args) { |
|
139 |
report(diags.warning(source, null, key, args)); |
|
140 |
} |
|
141 |
||
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
142 |
/** Report a lint warning, unless suppressed by the -nowarn option or the |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
143 |
* maximum number of warnings has been reached. |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
144 |
* @param lc The lint category for the diagnostic |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
145 |
* @param key The key for the localized warning message. |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
146 |
* @param args Fields of the warning message. |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
147 |
*/ |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
148 |
public void warning(LintCategory lc, String key, Object ... args) { |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
149 |
report(diags.warning(lc, key, args)); |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
150 |
} |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
151 |
|
867 | 152 |
/** Report a warning, unless suppressed by the -nowarn option or the |
153 |
* maximum number of warnings has been reached. |
|
154 |
* @param pos The source position at which to report the warning. |
|
155 |
* @param key The key for the localized warning message. |
|
156 |
* @param args Fields of the warning message. |
|
157 |
*/ |
|
158 |
public void warning(DiagnosticPosition pos, String key, Object ... args) { |
|
159 |
report(diags.warning(source, pos, key, args)); |
|
160 |
} |
|
161 |
||
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
162 |
/** Report a lint warning, unless suppressed by the -nowarn option or the |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
163 |
* maximum number of warnings has been reached. |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
164 |
* @param lc The lint category for the diagnostic |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
165 |
* @param pos The source position at which to report the warning. |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
166 |
* @param key The key for the localized warning message. |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
167 |
* @param args Fields of the warning message. |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
168 |
*/ |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
169 |
public void warning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) { |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
170 |
report(diags.warning(lc, source, pos, key, args)); |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
171 |
} |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
172 |
|
867 | 173 |
/** Report a warning, unless suppressed by the -nowarn option or the |
174 |
* maximum number of warnings has been reached. |
|
175 |
* @param pos The source position at which to report the warning. |
|
176 |
* @param key The key for the localized warning message. |
|
177 |
* @param args Fields of the warning message. |
|
178 |
*/ |
|
179 |
public void warning(int pos, String key, Object ... args) { |
|
180 |
report(diags.warning(source, wrap(pos), key, args)); |
|
181 |
} |
|
182 |
||
183 |
/** Report a warning. |
|
184 |
* @param pos The source position at which to report the warning. |
|
185 |
* @param key The key for the localized warning message. |
|
186 |
* @param args Fields of the warning message. |
|
187 |
*/ |
|
188 |
public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) { |
|
189 |
report(diags.mandatoryWarning(source, pos, key, args)); |
|
190 |
} |
|
191 |
||
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
192 |
/** Report a warning. |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
193 |
* @param lc The lint category for the diagnostic |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
194 |
* @param pos The source position at which to report the warning. |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
195 |
* @param key The key for the localized warning message. |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
196 |
* @param args Fields of the warning message. |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
197 |
*/ |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
198 |
public void mandatoryWarning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) { |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
199 |
report(diags.mandatoryWarning(lc, source, pos, key, args)); |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
200 |
} |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
201 |
|
867 | 202 |
/** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
203 |
* @param key The key for the localized notification message. |
|
204 |
* @param args Fields of the notint an error or warning message: |
|
205 |
*/ |
|
206 |
public void note(String key, Object ... args) { |
|
207 |
report(diags.note(source, null, key, args)); |
|
208 |
} |
|
209 |
||
210 |
/** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
|
211 |
* @param key The key for the localized notification message. |
|
212 |
* @param args Fields of the notification message. |
|
213 |
*/ |
|
214 |
public void note(DiagnosticPosition pos, String key, Object ... args) { |
|
215 |
report(diags.note(source, pos, key, args)); |
|
216 |
} |
|
217 |
||
218 |
/** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
|
219 |
* @param key The key for the localized notification message. |
|
220 |
* @param args Fields of the notification message. |
|
221 |
*/ |
|
222 |
public void note(int pos, String key, Object ... args) { |
|
223 |
report(diags.note(source, wrap(pos), key, args)); |
|
224 |
} |
|
225 |
||
226 |
/** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
|
227 |
* @param key The key for the localized notification message. |
|
228 |
* @param args Fields of the notification message. |
|
229 |
*/ |
|
230 |
public void note(JavaFileObject file, String key, Object ... args) { |
|
231 |
report(diags.note(getSource(file), null, key, args)); |
|
232 |
} |
|
233 |
||
234 |
/** Provide a non-fatal notification, unless suppressed by the -nowarn option. |
|
235 |
* @param key The key for the localized notification message. |
|
236 |
* @param args Fields of the notification message. |
|
237 |
*/ |
|
238 |
public void mandatoryNote(final JavaFileObject file, String key, Object ... args) { |
|
239 |
report(diags.mandatoryNote(getSource(file), key, args)); |
|
240 |
} |
|
241 |
||
242 |
protected abstract void report(JCDiagnostic diagnostic); |
|
243 |
||
244 |
protected abstract void directError(String key, Object... args); |
|
245 |
||
246 |
private DiagnosticPosition wrap(int pos) { |
|
247 |
return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos)); |
|
248 |
} |
|
249 |
||
250 |
/** Factory for diagnostics |
|
251 |
*/ |
|
252 |
protected JCDiagnostic.Factory diags; |
|
253 |
||
254 |
/** The file that's currently being translated. |
|
255 |
*/ |
|
256 |
protected DiagnosticSource source; |
|
257 |
||
258 |
/** A cache of lightweight DiagnosticSource objects. |
|
259 |
*/ |
|
260 |
protected Map<JavaFileObject, DiagnosticSource> sourceMap; |
|
261 |
} |