author | mcimadamore |
Mon, 28 Jul 2008 10:22:10 +0100 | |
changeset 942 | 98e1d4f8aacd |
parent 939 | 38e24969c7e9 |
child 1040 | c0f5acfd9d15 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
735 | 2 |
* Copyright 2003-2008 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.javac.util; |
|
27 |
||
28 |
import java.util.Locale; |
|
29 |
import java.util.Map; |
|
30 |
||
31 |
import javax.tools.Diagnostic; |
|
32 |
import javax.tools.JavaFileObject; |
|
33 |
||
942 | 34 |
import com.sun.tools.javac.api.DiagnosticFormatter; |
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
10
diff
changeset
|
35 |
import com.sun.tools.javac.file.JavacFileManager; |
10 | 36 |
import com.sun.tools.javac.tree.JCTree; |
37 |
||
38 |
import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*; |
|
39 |
||
40 |
/** An abstraction of a diagnostic message generated by the compiler. |
|
41 |
* |
|
42 |
* <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
43 |
* you write code that depends on this, you do so at your own risk. |
|
44 |
* This code and its internal interfaces are subject to change or |
|
45 |
* deletion without notice.</b> |
|
46 |
*/ |
|
47 |
public class JCDiagnostic implements Diagnostic<JavaFileObject> { |
|
48 |
/** A factory for creating diagnostic objects. */ |
|
49 |
public static class Factory { |
|
50 |
/** The context key for the diagnostic factory. */ |
|
51 |
protected static final Context.Key<JCDiagnostic.Factory> diagnosticFactoryKey = |
|
52 |
new Context.Key<JCDiagnostic.Factory>(); |
|
53 |
||
54 |
/** Get the Factory instance for this context. */ |
|
55 |
public static Factory instance(Context context) { |
|
56 |
Factory instance = context.get(diagnosticFactoryKey); |
|
57 |
if (instance == null) |
|
58 |
instance = new Factory(context); |
|
59 |
return instance; |
|
60 |
} |
|
61 |
||
62 |
final Messages messages; |
|
63 |
final String prefix; |
|
64 |
||
65 |
/** Create a new diagnostic factory. */ |
|
66 |
protected Factory(Context context) { |
|
67 |
context.put(diagnosticFactoryKey, this); |
|
68 |
messages = Messages.instance(context); |
|
69 |
prefix = "compiler"; |
|
70 |
} |
|
71 |
||
72 |
/** Create a new diagnostic factory. */ |
|
73 |
public Factory(Messages messages, String prefix) { |
|
74 |
this.messages = messages; |
|
75 |
this.prefix = prefix; |
|
76 |
} |
|
77 |
||
78 |
/** |
|
79 |
* Create an error diagnostic. |
|
80 |
* @param source The source of the compilation unit, if any, in which to report the error. |
|
81 |
* @param pos The source position at which to report the error. |
|
82 |
* @param key The key for the localized error message. |
|
83 |
* @param args Fields of the error message. |
|
84 |
*/ |
|
85 |
public JCDiagnostic error( |
|
86 |
DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { |
|
87 |
return new JCDiagnostic(messages, ERROR, true, source, pos, qualify(ERROR, key), args); |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options. |
|
92 |
* @param source The source of the compilation unit, if any, in which to report the warning. |
|
93 |
* @param pos The source position at which to report the warning. |
|
94 |
* @param key The key for the localized error message. |
|
95 |
* @param args Fields of the error message. |
|
96 |
* @see MandatoryWarningHandler |
|
97 |
*/ |
|
98 |
public JCDiagnostic mandatoryWarning( |
|
99 |
DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { |
|
100 |
return new JCDiagnostic(messages, WARNING, true, source, pos, qualify(WARNING, key), args); |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Create a warning diagnostic. |
|
105 |
* @param source The source of the compilation unit, if any, in which to report the warning. |
|
106 |
* @param pos The source position at which to report the warning. |
|
107 |
* @param key The key for the localized error message. |
|
108 |
* @param args Fields of the error message. |
|
109 |
*/ |
|
110 |
public JCDiagnostic warning( |
|
111 |
DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { |
|
112 |
return new JCDiagnostic(messages, WARNING, false, source, pos, qualify(WARNING, key), args); |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Create a note diagnostic that will not be hidden by the -nowarn or -Xlint:none options. |
|
117 |
* @param key The key for the localized error message. |
|
118 |
* @param args Fields of the error message. |
|
119 |
* @see MandatoryWarningHandler |
|
120 |
*/ |
|
121 |
public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) { |
|
122 |
return new JCDiagnostic(messages, NOTE, true, source, null, qualify(NOTE, key), args); |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* Create a note diagnostic. |
|
127 |
* @param key The key for the localized error message. |
|
128 |
* @param args Fields of the error message. |
|
129 |
*/ |
|
130 |
public JCDiagnostic note(String key, Object... args) { |
|
131 |
return note(null, null, key, args); |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* Create a note diagnostic. |
|
136 |
* @param source The source of the compilation unit, if any, in which to report the note. |
|
137 |
* @param pos The source position at which to report the note. |
|
138 |
* @param key The key for the localized error message. |
|
139 |
* @param args Fields of the error message. |
|
140 |
*/ |
|
141 |
public JCDiagnostic note( |
|
142 |
DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { |
|
143 |
return new JCDiagnostic(messages, NOTE, false, source, pos, qualify(NOTE, key), args); |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Create a fragment diagnostic, for use as an argument in other diagnostics |
|
148 |
* @param key The key for the localized error message. |
|
149 |
* @param args Fields of the error message. |
|
150 |
*/ |
|
151 |
public JCDiagnostic fragment(String key, Object... args) { |
|
152 |
return new JCDiagnostic(messages, FRAGMENT, false, null, null, qualify(FRAGMENT, key), args); |
|
153 |
} |
|
154 |
||
155 |
protected String qualify(DiagnosticType t, String key) { |
|
156 |
return prefix + "." + t.key + "." + key; |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
||
161 |
||
162 |
/** |
|
163 |
* Create a fragment diagnostic, for use as an argument in other diagnostics |
|
164 |
* @param key The key for the localized error message. |
|
165 |
* @param args Fields of the error message. |
|
166 |
*/ |
|
167 |
// should be deprecated |
|
168 |
public static JCDiagnostic fragment(String key, Object... args) { |
|
169 |
return new JCDiagnostic(Messages.getDefaultMessages(), |
|
170 |
FRAGMENT, |
|
171 |
false, |
|
172 |
null, |
|
173 |
null, |
|
174 |
"compiler." + FRAGMENT.key + "." + key, |
|
175 |
args); |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* A DiagnosticType defines the type of the diagnostic. |
|
180 |
**/ |
|
181 |
public enum DiagnosticType { |
|
182 |
/** A fragment of an enclosing diagnostic. */ |
|
183 |
FRAGMENT("misc"), |
|
184 |
/** A note: similar to, but less serious than, a warning. */ |
|
185 |
NOTE("note"), |
|
186 |
/** A warning. */ |
|
187 |
WARNING("warn"), |
|
188 |
/** An error. */ |
|
189 |
ERROR("err"); |
|
190 |
||
191 |
final String key; |
|
192 |
||
193 |
/** Create a DiagnosticType. |
|
194 |
* @param key A string used to create the resource key for the diagnostic. |
|
195 |
*/ |
|
196 |
DiagnosticType(String key) { |
|
197 |
this.key = key; |
|
198 |
} |
|
199 |
}; |
|
200 |
||
201 |
/** |
|
202 |
* A DiagnosticPosition provides information about the positions in a file |
|
203 |
* that gave rise to a diagnostic. It always defines a "preferred" position |
|
204 |
* that most accurately defines the location of the diagnostic, it may also |
|
205 |
* provide a related tree node that spans that location. |
|
206 |
*/ |
|
207 |
public static interface DiagnosticPosition { |
|
208 |
/** Gets the tree node, if any, to which the diagnostic applies. */ |
|
209 |
JCTree getTree(); |
|
210 |
/** If there is a tree node, get the start position of the tree node. |
|
211 |
* Otherwise, just returns the same as getPreferredPosition(). */ |
|
212 |
int getStartPosition(); |
|
213 |
/** Get the position within the file that most accurately defines the |
|
214 |
* location for the diagnostic. */ |
|
215 |
int getPreferredPosition(); |
|
216 |
/** If there is a tree node, and if endPositions are available, get |
|
217 |
* the end position of the tree node. Otherwise, just returns the |
|
218 |
* same as getPreferredPosition(). */ |
|
219 |
int getEndPosition(Map<JCTree, Integer> endPosTable); |
|
220 |
} |
|
221 |
||
222 |
/** |
|
223 |
* A DiagnosticPosition that simply identifies a position, but no related |
|
224 |
* tree node, as the location for a diagnostic. Used for scanner and parser |
|
225 |
* diagnostics. */ |
|
226 |
public static class SimpleDiagnosticPosition implements DiagnosticPosition { |
|
227 |
public SimpleDiagnosticPosition(int pos) { |
|
228 |
this.pos = pos; |
|
229 |
} |
|
230 |
||
231 |
public JCTree getTree() { |
|
232 |
return null; |
|
233 |
} |
|
234 |
||
235 |
public int getStartPosition() { |
|
236 |
return pos; |
|
237 |
} |
|
238 |
||
239 |
public int getPreferredPosition() { |
|
240 |
return pos; |
|
241 |
} |
|
242 |
||
243 |
public int getEndPosition(Map<JCTree, Integer> endPosTable) { |
|
244 |
return pos; |
|
245 |
} |
|
246 |
||
247 |
private final int pos; |
|
248 |
} |
|
249 |
||
250 |
private final Messages messages; |
|
251 |
private final DiagnosticType type; |
|
252 |
private final DiagnosticSource source; |
|
253 |
private final DiagnosticPosition position; |
|
254 |
private final int line; |
|
255 |
private final int column; |
|
256 |
private final String key; |
|
942 | 257 |
protected Object[] args; |
10 | 258 |
private boolean mandatory; |
259 |
||
260 |
/** |
|
261 |
* Create a diagnostic object. |
|
262 |
* @param messages the resource for localized messages |
|
263 |
* @param dt the type of diagnostic |
|
264 |
* @param name the name of the source file, or null if none. |
|
265 |
* @param pos the character offset within the source file, if given. |
|
266 |
* @param key a resource key to identify the text of the diagnostic |
|
267 |
* @param args arguments to be included in the text of the diagnostic |
|
268 |
*/ |
|
269 |
protected JCDiagnostic(Messages messages, |
|
270 |
DiagnosticType dt, |
|
271 |
boolean mandatory, |
|
272 |
DiagnosticSource source, |
|
273 |
DiagnosticPosition pos, |
|
274 |
String key, |
|
275 |
Object ... args) { |
|
276 |
if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS) |
|
277 |
throw new IllegalArgumentException(); |
|
278 |
||
279 |
this.messages = messages; |
|
280 |
this.type = dt; |
|
281 |
this.mandatory = mandatory; |
|
282 |
this.source = source; |
|
283 |
this.position = pos; |
|
284 |
this.key = key; |
|
285 |
this.args = args; |
|
286 |
||
287 |
int n = (pos == null ? Position.NOPOS : pos.getPreferredPosition()); |
|
288 |
if (n == Position.NOPOS || source == null) |
|
289 |
line = column = -1; |
|
290 |
else { |
|
291 |
line = source.getLineNumber(n); |
|
292 |
column = source.getColumnNumber(n); |
|
293 |
} |
|
294 |
} |
|
295 |
||
296 |
/** |
|
297 |
* Get the type of this diagnostic. |
|
298 |
* @return the type of this diagnostic |
|
299 |
*/ |
|
300 |
public DiagnosticType getType() { |
|
301 |
return type; |
|
302 |
} |
|
303 |
||
304 |
/** |
|
305 |
* Check whether or not this diagnostic is required to be shown. |
|
306 |
* @return true if this diagnostic is required to be shown. |
|
307 |
*/ |
|
308 |
public boolean isMandatory() { |
|
309 |
return mandatory; |
|
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* Get the name of the source file referred to by this diagnostic. |
|
314 |
* @return the name of the source referred to with this diagnostic, or null if none |
|
315 |
*/ |
|
316 |
public JavaFileObject getSource() { |
|
317 |
if (source == null) |
|
318 |
return null; |
|
319 |
else |
|
320 |
return source.getFile(); |
|
321 |
} |
|
322 |
||
323 |
/** |
|
324 |
* Get the name of the source file referred to by this diagnostic. |
|
325 |
* @return the name of the source referred to with this diagnostic, or null if none |
|
326 |
*/ |
|
327 |
public String getSourceName() { |
|
328 |
JavaFileObject s = getSource(); |
|
329 |
return s == null ? null : JavacFileManager.getJavacFileName(s); |
|
330 |
} |
|
331 |
||
332 |
/** |
|
333 |
* Get the source referred to by this diagnostic. |
|
334 |
* @return the source referred to with this diagnostic, or null if none |
|
335 |
*/ |
|
336 |
public DiagnosticSource getDiagnosticSource() { |
|
337 |
return source; |
|
338 |
} |
|
339 |
||
340 |
protected int getIntStartPosition() { |
|
341 |
return (position == null ? Position.NOPOS : position.getStartPosition()); |
|
342 |
} |
|
343 |
||
344 |
protected int getIntPosition() { |
|
345 |
return (position == null ? Position.NOPOS : position.getPreferredPosition()); |
|
346 |
} |
|
347 |
||
348 |
protected int getIntEndPosition() { |
|
349 |
return (position == null ? Position.NOPOS : position.getEndPosition(source.getEndPosTable())); |
|
350 |
} |
|
351 |
||
352 |
public long getStartPosition() { |
|
353 |
return getIntStartPosition(); |
|
354 |
} |
|
355 |
||
356 |
public long getPosition() { |
|
357 |
return getIntPosition(); |
|
358 |
} |
|
359 |
||
360 |
public long getEndPosition() { |
|
361 |
return getIntEndPosition(); |
|
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
* Get the line number within the source referred to by this diagnostic. |
|
366 |
* @return the line number within the source referred to by this diagnostic |
|
367 |
*/ |
|
368 |
public long getLineNumber() { |
|
369 |
return line; |
|
370 |
} |
|
371 |
||
372 |
/** |
|
373 |
* Get the column number within the line of source referred to by this diagnostic. |
|
374 |
* @return the column number within the line of source referred to by this diagnostic |
|
375 |
*/ |
|
376 |
public long getColumnNumber() { |
|
377 |
return column; |
|
378 |
} |
|
379 |
||
380 |
/** |
|
381 |
* Get the arguments to be included in the text of the diagnostic. |
|
382 |
* @return the arguments to be included in the text of the diagnostic |
|
383 |
*/ |
|
384 |
public Object[] getArgs() { |
|
385 |
return args; |
|
386 |
} |
|
387 |
||
388 |
/** |
|
389 |
* Get the prefix string associated with this type of diagnostic. |
|
390 |
* @return the prefix string associated with this type of diagnostic |
|
391 |
*/ |
|
392 |
public String getPrefix() { |
|
393 |
return getPrefix(type); |
|
394 |
} |
|
395 |
||
396 |
/** |
|
397 |
* Get the prefix string associated with a particular type of diagnostic. |
|
398 |
* @return the prefix string associated with a particular type of diagnostic |
|
399 |
*/ |
|
400 |
public String getPrefix(DiagnosticType dt) { |
|
942 | 401 |
return getFormatter().formatKind(this, Locale.getDefault()); |
402 |
} |
|
403 |
||
404 |
private DiagnosticFormatter<JCDiagnostic> getFormatter() { |
|
405 |
if (defaultFormatter == null) { |
|
406 |
defaultFormatter = new BasicDiagnosticFormatter(messages); |
|
10 | 407 |
} |
942 | 408 |
return defaultFormatter; |
10 | 409 |
} |
410 |
||
942 | 411 |
|
10 | 412 |
/** |
413 |
* Return the standard presentation of this diagnostic. |
|
414 |
*/ |
|
415 |
public String toString() { |
|
942 | 416 |
return getFormatter().format(this,Locale.getDefault()); |
10 | 417 |
} |
418 |
||
942 | 419 |
private static DiagnosticFormatter<JCDiagnostic> defaultFormatter; |
10 | 420 |
|
421 |
// Methods for javax.tools.Diagnostic |
|
422 |
||
423 |
public Diagnostic.Kind getKind() { |
|
424 |
switch (type) { |
|
425 |
case NOTE: |
|
426 |
return Diagnostic.Kind.NOTE; |
|
427 |
case WARNING: |
|
428 |
return mandatory ? Diagnostic.Kind.MANDATORY_WARNING |
|
429 |
: Diagnostic.Kind.WARNING; |
|
430 |
case ERROR: |
|
431 |
return Diagnostic.Kind.ERROR; |
|
432 |
default: |
|
433 |
return Diagnostic.Kind.OTHER; |
|
434 |
} |
|
435 |
} |
|
436 |
||
437 |
public String getCode() { |
|
438 |
return key; |
|
439 |
} |
|
440 |
||
441 |
public String getMessage(Locale locale) { |
|
442 |
// RFE 6406133: JCDiagnostic.getMessage ignores locale argument |
|
942 | 443 |
return getFormatter().formatMessage(this, locale); |
10 | 444 |
} |
445 |
} |