23 * have any questions. |
23 * have any questions. |
24 */ |
24 */ |
25 |
25 |
26 package com.sun.tools.javac.util; |
26 package com.sun.tools.javac.util; |
27 |
27 |
28 import java.util.ResourceBundle; |
|
29 import java.util.Collection; |
|
30 import java.util.Locale; |
28 import java.util.Locale; |
31 import java.util.Map; |
29 import java.util.Map; |
32 |
30 |
33 import javax.tools.Diagnostic; |
31 import javax.tools.Diagnostic; |
34 import javax.tools.JavaFileObject; |
32 import javax.tools.JavaFileObject; |
35 |
33 |
36 import com.sun.tools.javac.api.Formattable; |
34 import com.sun.tools.javac.api.DiagnosticFormatter; |
37 import com.sun.tools.javac.file.JavacFileManager; |
35 import com.sun.tools.javac.file.JavacFileManager; |
38 import com.sun.tools.javac.tree.JCTree; |
36 import com.sun.tools.javac.tree.JCTree; |
39 |
37 |
40 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*; |
38 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*; |
41 |
39 |
254 private final DiagnosticSource source; |
252 private final DiagnosticSource source; |
255 private final DiagnosticPosition position; |
253 private final DiagnosticPosition position; |
256 private final int line; |
254 private final int line; |
257 private final int column; |
255 private final int column; |
258 private final String key; |
256 private final String key; |
259 private final Object[] args; |
257 protected Object[] args; |
260 private boolean mandatory; |
258 private boolean mandatory; |
261 |
259 |
262 /** |
260 /** |
263 * Create a diagnostic object. |
261 * Create a diagnostic object. |
264 * @param messages the resource for localized messages |
262 * @param messages the resource for localized messages |
398 /** |
396 /** |
399 * Get the prefix string associated with a particular type of diagnostic. |
397 * Get the prefix string associated with a particular type of diagnostic. |
400 * @return the prefix string associated with a particular type of diagnostic |
398 * @return the prefix string associated with a particular type of diagnostic |
401 */ |
399 */ |
402 public String getPrefix(DiagnosticType dt) { |
400 public String getPrefix(DiagnosticType dt) { |
403 switch (dt) { |
401 return getFormatter().formatKind(this, Locale.getDefault()); |
404 case FRAGMENT: return ""; |
402 } |
405 case NOTE: return getLocalizedString("compiler.note.note"); |
403 |
406 case WARNING: return getLocalizedString("compiler.warn.warning"); |
404 private DiagnosticFormatter<JCDiagnostic> getFormatter() { |
407 case ERROR: return getLocalizedString("compiler.err.error"); |
405 if (defaultFormatter == null) { |
408 default: |
406 defaultFormatter = new BasicDiagnosticFormatter(messages); |
409 throw new AssertionError("Unknown diagnostic type: " + dt); |
407 } |
410 } |
408 return defaultFormatter; |
411 } |
409 } |
|
410 |
412 |
411 |
413 /** |
412 /** |
414 * Return the standard presentation of this diagnostic. |
413 * Return the standard presentation of this diagnostic. |
415 */ |
414 */ |
416 public String toString() { |
415 public String toString() { |
417 if (defaultFormatter == null) { |
416 return getFormatter().format(this,Locale.getDefault()); |
418 defaultFormatter = |
417 } |
419 new DiagnosticFormatter(); |
418 |
420 } |
419 private static DiagnosticFormatter<JCDiagnostic> defaultFormatter; |
421 return defaultFormatter.format(this); |
|
422 } |
|
423 |
|
424 private static DiagnosticFormatter defaultFormatter; |
|
425 |
|
426 private static final String messageBundleName = |
|
427 "com.sun.tools.javac.resources.compiler"; |
|
428 |
|
429 private String getLocalizedString(String key, Object... args) { |
|
430 String[] strings = new String[args.length]; |
|
431 for (int i = 0; i < strings.length; i++) { |
|
432 Object arg = args[i]; |
|
433 if (arg == null) |
|
434 strings[i] = null; |
|
435 else if (arg instanceof JCDiagnostic) |
|
436 strings[i] = ((JCDiagnostic) arg).getMessage(null); |
|
437 else if (arg instanceof Collection<?>) |
|
438 strings[i] = DiagnosticFormatter.convert((Collection<?>)arg).toString(); |
|
439 else if (arg instanceof Formattable) { |
|
440 ResourceBundle rb = ResourceBundle.getBundle(messageBundleName); |
|
441 strings[i] = ((Formattable)arg).toString(rb); |
|
442 } |
|
443 else |
|
444 strings[i] = arg.toString(); |
|
445 } |
|
446 |
|
447 return messages.getLocalizedString(key, (Object[]) strings); |
|
448 } |
|
449 |
420 |
450 // Methods for javax.tools.Diagnostic |
421 // Methods for javax.tools.Diagnostic |
451 |
422 |
452 public Diagnostic.Kind getKind() { |
423 public Diagnostic.Kind getKind() { |
453 switch (type) { |
424 switch (type) { |