src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java

changeset 89
b6d5f53b3b29
parent 83
37470f5ea179
child 100
37551dc0f591
equal deleted inserted replaced
88:05684554f040 89:b6d5f53b3b29
57 if (instance == null) 57 if (instance == null)
58 instance = new Factory(context); 58 instance = new Factory(context);
59 return instance; 59 return instance;
60 } 60 }
61 61
62 final Messages messages; 62 DiagnosticFormatter<JCDiagnostic> formatter;
63 final String prefix; 63 final String prefix;
64 64
65 /** Create a new diagnostic factory. */ 65 /** Create a new diagnostic factory. */
66 protected Factory(Context context) { 66 protected Factory(Context context) {
67 this(Messages.instance(context), "compiler");
67 context.put(diagnosticFactoryKey, this); 68 context.put(diagnosticFactoryKey, this);
68 messages = Messages.instance(context);
69 prefix = "compiler";
70 } 69 }
71 70
72 /** Create a new diagnostic factory. */ 71 /** Create a new diagnostic factory. */
73 public Factory(Messages messages, String prefix) { 72 public Factory(Messages messages, String prefix) {
74 this.messages = messages;
75 this.prefix = prefix; 73 this.prefix = prefix;
74 this.formatter = new BasicDiagnosticFormatter(messages);
76 } 75 }
77 76
78 /** 77 /**
79 * Create an error diagnostic. 78 * Create an error diagnostic.
80 * @param source The source of the compilation unit, if any, in which to report the error. 79 * @param source The source of the compilation unit, if any, in which to report the error.
82 * @param key The key for the localized error message. 81 * @param key The key for the localized error message.
83 * @param args Fields of the error message. 82 * @param args Fields of the error message.
84 */ 83 */
85 public JCDiagnostic error( 84 public JCDiagnostic error(
86 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 85 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
87 return new JCDiagnostic(messages, ERROR, true, source, pos, qualify(ERROR, key), args); 86 return new JCDiagnostic(formatter, ERROR, true, source, pos, qualify(ERROR, key), args);
88 } 87 }
89 88
90 /** 89 /**
91 * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options. 90 * 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. 91 * @param source The source of the compilation unit, if any, in which to report the warning.
95 * @param args Fields of the error message. 94 * @param args Fields of the error message.
96 * @see MandatoryWarningHandler 95 * @see MandatoryWarningHandler
97 */ 96 */
98 public JCDiagnostic mandatoryWarning( 97 public JCDiagnostic mandatoryWarning(
99 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 98 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
100 return new JCDiagnostic(messages, WARNING, true, source, pos, qualify(WARNING, key), args); 99 return new JCDiagnostic(formatter, WARNING, true, source, pos, qualify(WARNING, key), args);
101 } 100 }
102 101
103 /** 102 /**
104 * Create a warning diagnostic. 103 * Create a warning diagnostic.
105 * @param source The source of the compilation unit, if any, in which to report the warning. 104 * @param source The source of the compilation unit, if any, in which to report the warning.
107 * @param key The key for the localized error message. 106 * @param key The key for the localized error message.
108 * @param args Fields of the error message. 107 * @param args Fields of the error message.
109 */ 108 */
110 public JCDiagnostic warning( 109 public JCDiagnostic warning(
111 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 110 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
112 return new JCDiagnostic(messages, WARNING, false, source, pos, qualify(WARNING, key), args); 111 return new JCDiagnostic(formatter, WARNING, false, source, pos, qualify(WARNING, key), args);
113 } 112 }
114 113
115 /** 114 /**
116 * Create a note diagnostic that will not be hidden by the -nowarn or -Xlint:none options. 115 * 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. 116 * @param key The key for the localized error message.
118 * @param args Fields of the error message. 117 * @param args Fields of the error message.
119 * @see MandatoryWarningHandler 118 * @see MandatoryWarningHandler
120 */ 119 */
121 public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) { 120 public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) {
122 return new JCDiagnostic(messages, NOTE, true, source, null, qualify(NOTE, key), args); 121 return new JCDiagnostic(formatter, NOTE, true, source, null, qualify(NOTE, key), args);
123 } 122 }
124 123
125 /** 124 /**
126 * Create a note diagnostic. 125 * Create a note diagnostic.
127 * @param key The key for the localized error message. 126 * @param key The key for the localized error message.
138 * @param key The key for the localized error message. 137 * @param key The key for the localized error message.
139 * @param args Fields of the error message. 138 * @param args Fields of the error message.
140 */ 139 */
141 public JCDiagnostic note( 140 public JCDiagnostic note(
142 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 141 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
143 return new JCDiagnostic(messages, NOTE, false, source, pos, qualify(NOTE, key), args); 142 return new JCDiagnostic(formatter, NOTE, false, source, pos, qualify(NOTE, key), args);
144 } 143 }
145 144
146 /** 145 /**
147 * Create a fragment diagnostic, for use as an argument in other diagnostics 146 * Create a fragment diagnostic, for use as an argument in other diagnostics
148 * @param key The key for the localized error message. 147 * @param key The key for the localized error message.
149 * @param args Fields of the error message. 148 * @param args Fields of the error message.
150 */ 149 */
151 public JCDiagnostic fragment(String key, Object... args) { 150 public JCDiagnostic fragment(String key, Object... args) {
152 return new JCDiagnostic(messages, FRAGMENT, false, null, null, qualify(FRAGMENT, key), args); 151 return new JCDiagnostic(formatter, FRAGMENT, false, null, null, qualify(FRAGMENT, key), args);
153 } 152 }
154 153
155 protected String qualify(DiagnosticType t, String key) { 154 protected String qualify(DiagnosticType t, String key) {
156 return prefix + "." + t.key + "." + key; 155 return prefix + "." + t.key + "." + key;
157 } 156 }
161 160
162 /** 161 /**
163 * Create a fragment diagnostic, for use as an argument in other diagnostics 162 * Create a fragment diagnostic, for use as an argument in other diagnostics
164 * @param key The key for the localized error message. 163 * @param key The key for the localized error message.
165 * @param args Fields of the error message. 164 * @param args Fields of the error message.
166 */ 165 *
167 // should be deprecated 166 */
167 @Deprecated
168 public static JCDiagnostic fragment(String key, Object... args) { 168 public static JCDiagnostic fragment(String key, Object... args) {
169 return new JCDiagnostic(Messages.getDefaultMessages(), 169 return new JCDiagnostic(getFragmentFormatter(),
170 FRAGMENT, 170 FRAGMENT,
171 false, 171 false,
172 null, 172 null,
173 null, 173 null,
174 "compiler." + FRAGMENT.key + "." + key, 174 "compiler." + FRAGMENT.key + "." + key,
175 args); 175 args);
176 }
177 //where
178 @Deprecated
179 public static DiagnosticFormatter<JCDiagnostic> getFragmentFormatter() {
180 if (fragmentFormatter == null) {
181 fragmentFormatter = new BasicDiagnosticFormatter(Messages.getDefaultMessages());
182 }
183 return fragmentFormatter;
176 } 184 }
177 185
178 /** 186 /**
179 * A DiagnosticType defines the type of the diagnostic. 187 * A DiagnosticType defines the type of the diagnostic.
180 **/ 188 **/
245 } 253 }
246 254
247 private final int pos; 255 private final int pos;
248 } 256 }
249 257
250 private final Messages messages;
251 private final DiagnosticType type; 258 private final DiagnosticType type;
252 private final DiagnosticSource source; 259 private final DiagnosticSource source;
253 private final DiagnosticPosition position; 260 private final DiagnosticPosition position;
254 private final int line; 261 private final int line;
255 private final int column; 262 private final int column;
264 * @param name the name of the source file, or null if none. 271 * @param name the name of the source file, or null if none.
265 * @param pos the character offset within the source file, if given. 272 * @param pos the character offset within the source file, if given.
266 * @param key a resource key to identify the text of the diagnostic 273 * @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 274 * @param args arguments to be included in the text of the diagnostic
268 */ 275 */
269 protected JCDiagnostic(Messages messages, 276 protected JCDiagnostic(DiagnosticFormatter<JCDiagnostic> formatter,
270 DiagnosticType dt, 277 DiagnosticType dt,
271 boolean mandatory, 278 boolean mandatory,
272 DiagnosticSource source, 279 DiagnosticSource source,
273 DiagnosticPosition pos, 280 DiagnosticPosition pos,
274 String key, 281 String key,
275 Object ... args) { 282 Object ... args) {
276 if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS) 283 if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS)
277 throw new IllegalArgumentException(); 284 throw new IllegalArgumentException();
278 285
279 this.messages = messages; 286 this.defaultFormatter = formatter;
280 this.type = dt; 287 this.type = dt;
281 this.mandatory = mandatory; 288 this.mandatory = mandatory;
282 this.source = source; 289 this.source = source;
283 this.position = pos; 290 this.position = pos;
284 this.key = key; 291 this.key = key;
396 /** 403 /**
397 * Get the prefix string associated with a particular type of diagnostic. 404 * Get the prefix string associated with a particular type of diagnostic.
398 * @return the prefix string associated with a particular type of diagnostic 405 * @return the prefix string associated with a particular type of diagnostic
399 */ 406 */
400 public String getPrefix(DiagnosticType dt) { 407 public String getPrefix(DiagnosticType dt) {
401 return getFormatter().formatKind(this, Locale.getDefault()); 408 return defaultFormatter.formatKind(this, Locale.getDefault());
402 } 409 }
403
404 private DiagnosticFormatter<JCDiagnostic> getFormatter() {
405 if (defaultFormatter == null) {
406 defaultFormatter = new BasicDiagnosticFormatter(messages);
407 }
408 return defaultFormatter;
409 }
410
411 410
412 /** 411 /**
413 * Return the standard presentation of this diagnostic. 412 * Return the standard presentation of this diagnostic.
414 */ 413 */
415 public String toString() { 414 public String toString() {
416 return getFormatter().format(this,Locale.getDefault()); 415 return defaultFormatter.format(this,Locale.getDefault());
417 } 416 }
418 417
419 private static DiagnosticFormatter<JCDiagnostic> defaultFormatter; 418 private DiagnosticFormatter<JCDiagnostic> defaultFormatter;
419 @Deprecated
420 private static DiagnosticFormatter<JCDiagnostic> fragmentFormatter;
420 421
421 // Methods for javax.tools.Diagnostic 422 // Methods for javax.tools.Diagnostic
422 423
423 public Diagnostic.Kind getKind() { 424 public Diagnostic.Kind getKind() {
424 switch (type) { 425 switch (type) {
438 return key; 439 return key;
439 } 440 }
440 441
441 public String getMessage(Locale locale) { 442 public String getMessage(Locale locale) {
442 // RFE 6406133: JCDiagnostic.getMessage ignores locale argument 443 // RFE 6406133: JCDiagnostic.getMessage ignores locale argument
443 return getFormatter().formatMessage(this, locale); 444 return defaultFormatter.formatMessage(this, locale);
444 } 445 }
445 } 446 }

mercurial