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

changeset 643
a626d8c1de6e
parent 612
d1bd93028447
child 726
2974d3800eb1
equal deleted inserted replaced
642:6b95dd682538 643:a626d8c1de6e
23 * questions. 23 * 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.EnumSet;
28 import java.util.Locale; 29 import java.util.Locale;
29 import java.util.Map; 30 import java.util.Map;
31 import java.util.Set;
30 32
31 import javax.tools.Diagnostic; 33 import javax.tools.Diagnostic;
32 import javax.tools.JavaFileObject; 34 import javax.tools.JavaFileObject;
33 35
34 import com.sun.tools.javac.api.DiagnosticFormatter; 36 import com.sun.tools.javac.api.DiagnosticFormatter;
81 * @param key The key for the localized error message. 83 * @param key The key for the localized error message.
82 * @param args Fields of the error message. 84 * @param args Fields of the error message.
83 */ 85 */
84 public JCDiagnostic error( 86 public JCDiagnostic error(
85 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 87 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
86 return create(ERROR, null, true, source, pos, key, args); 88 return create(ERROR, null, EnumSet.of(DiagnosticFlag.MANDATORY), source, pos, key, args);
87 } 89 }
88 90
89 /** 91 /**
90 * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options. 92 * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
91 * @param source The source of the compilation unit, if any, in which to report the warning. 93 * @param source The source of the compilation unit, if any, in which to report the warning.
94 * @param args Fields of the warning message. 96 * @param args Fields of the warning message.
95 * @see MandatoryWarningHandler 97 * @see MandatoryWarningHandler
96 */ 98 */
97 public JCDiagnostic mandatoryWarning( 99 public JCDiagnostic mandatoryWarning(
98 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 100 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
99 return create(WARNING, null, true, source, pos, key, args); 101 return create(WARNING, null, EnumSet.of(DiagnosticFlag.MANDATORY), source, pos, key, args);
100 } 102 }
101 103
102 /** 104 /**
103 * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options. 105 * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
104 * @param lc The lint category for the diagnostic 106 * @param lc The lint category for the diagnostic
109 * @see MandatoryWarningHandler 111 * @see MandatoryWarningHandler
110 */ 112 */
111 public JCDiagnostic mandatoryWarning( 113 public JCDiagnostic mandatoryWarning(
112 LintCategory lc, 114 LintCategory lc,
113 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 115 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
114 return create(WARNING, lc, true, source, pos, key, args); 116 return create(WARNING, lc, EnumSet.of(DiagnosticFlag.MANDATORY), source, pos, key, args);
115 } 117 }
116 118
117 /** 119 /**
118 * Create a warning diagnostic. 120 * Create a warning diagnostic.
119 * @param lc The lint category for the diagnostic 121 * @param lc The lint category for the diagnostic
121 * @param args Fields of the warning message. 123 * @param args Fields of the warning message.
122 * @see MandatoryWarningHandler 124 * @see MandatoryWarningHandler
123 */ 125 */
124 public JCDiagnostic warning( 126 public JCDiagnostic warning(
125 LintCategory lc, String key, Object... args) { 127 LintCategory lc, String key, Object... args) {
126 return create(WARNING, lc, false, null, null, key, args); 128 return create(WARNING, lc, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args);
127 } 129 }
128 130
129 /** 131 /**
130 * Create a warning diagnostic. 132 * Create a warning diagnostic.
131 * @param source The source of the compilation unit, if any, in which to report the warning. 133 * @param source The source of the compilation unit, if any, in which to report the warning.
133 * @param key The key for the localized warning message. 135 * @param key The key for the localized warning message.
134 * @param args Fields of the warning message. 136 * @param args Fields of the warning message.
135 */ 137 */
136 public JCDiagnostic warning( 138 public JCDiagnostic warning(
137 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 139 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
138 return create(WARNING, null, false, source, pos, key, args); 140 return create(WARNING, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args);
139 } 141 }
140 142
141 /** 143 /**
142 * Create a warning diagnostic. 144 * Create a warning diagnostic.
143 * @param lc The lint category for the diagnostic 145 * @param lc The lint category for the diagnostic
147 * @param args Fields of the warning message. 149 * @param args Fields of the warning message.
148 * @see MandatoryWarningHandler 150 * @see MandatoryWarningHandler
149 */ 151 */
150 public JCDiagnostic warning( 152 public JCDiagnostic warning(
151 LintCategory lc, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 153 LintCategory lc, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
152 return create(WARNING, lc, false, source, pos, key, args); 154 return create(WARNING, lc, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args);
153 } 155 }
154 156
155 /** 157 /**
156 * Create a note diagnostic that will not be hidden by the -nowarn or -Xlint:none options. 158 * Create a note diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
157 * @param key The key for the localized message. 159 * @param key The key for the localized message.
158 * @param args Fields of the message. 160 * @param args Fields of the message.
159 * @see MandatoryWarningHandler 161 * @see MandatoryWarningHandler
160 */ 162 */
161 public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) { 163 public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) {
162 return create(NOTE, null, true, source, null, key, args); 164 return create(NOTE, null, EnumSet.of(DiagnosticFlag.MANDATORY), source, null, key, args);
163 } 165 }
164 166
165 /** 167 /**
166 * Create a note diagnostic. 168 * Create a note diagnostic.
167 * @param key The key for the localized error message. 169 * @param key The key for the localized error message.
168 * @param args Fields of the message. 170 * @param args Fields of the message.
169 */ 171 */
170 public JCDiagnostic note(String key, Object... args) { 172 public JCDiagnostic note(String key, Object... args) {
171 return create(NOTE, null, false, null, null, key, args); 173 return create(NOTE, null, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args);
172 } 174 }
173 175
174 /** 176 /**
175 * Create a note diagnostic. 177 * Create a note diagnostic.
176 * @param source The source of the compilation unit, if any, in which to report the note. 178 * @param source The source of the compilation unit, if any, in which to report the note.
178 * @param key The key for the localized message. 180 * @param key The key for the localized message.
179 * @param args Fields of the message. 181 * @param args Fields of the message.
180 */ 182 */
181 public JCDiagnostic note( 183 public JCDiagnostic note(
182 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 184 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
183 return create(NOTE, null, false, source, pos, key, args); 185 return create(NOTE, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args);
184 } 186 }
185 187
186 /** 188 /**
187 * Create a fragment diagnostic, for use as an argument in other diagnostics 189 * Create a fragment diagnostic, for use as an argument in other diagnostics
188 * @param key The key for the localized message. 190 * @param key The key for the localized message.
189 * @param args Fields of the message. 191 * @param args Fields of the message.
190 */ 192 */
191 public JCDiagnostic fragment(String key, Object... args) { 193 public JCDiagnostic fragment(String key, Object... args) {
192 return create(FRAGMENT, null, false, null, null, key, args); 194 return create(FRAGMENT, null, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args);
193 } 195 }
194 196
195 /** 197 /**
196 * Create a new diagnostic of the given kind, which is not mandatory and which has 198 * Create a new diagnostic of the given kind, which is not mandatory and which has
197 * no lint category. 199 * no lint category.
202 * @param key The key for the localized message. 204 * @param key The key for the localized message.
203 * @param args Fields of the message. 205 * @param args Fields of the message.
204 */ 206 */
205 public JCDiagnostic create( 207 public JCDiagnostic create(
206 DiagnosticType kind, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 208 DiagnosticType kind, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
207 return create(kind, null, false, source, pos, key, args); 209 return create(kind, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args);
208 } 210 }
209 211
210 /** 212 /**
211 * Create a new diagnostic of the given kind. 213 * Create a new diagnostic of the given kind.
212 * @param kind The diagnostic kind 214 * @param kind The diagnostic kind
216 * @param pos The source position at which to report the message. 218 * @param pos The source position at which to report the message.
217 * @param key The key for the localized message. 219 * @param key The key for the localized message.
218 * @param args Fields of the message. 220 * @param args Fields of the message.
219 */ 221 */
220 public JCDiagnostic create( 222 public JCDiagnostic create(
221 DiagnosticType kind, LintCategory lc, boolean isMandatory, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 223 DiagnosticType kind, LintCategory lc, Set<DiagnosticFlag> flags, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
222 return new JCDiagnostic(formatter, kind, lc, isMandatory, source, pos, qualify(kind, key), args); 224 return new JCDiagnostic(formatter, kind, lc, flags, source, pos, qualify(kind, key), args);
223 } 225 }
224 226
225 protected String qualify(DiagnosticType t, String key) { 227 protected String qualify(DiagnosticType t, String key) {
226 return prefix + "." + t.key + "." + key; 228 return prefix + "." + t.key + "." + key;
227 } 229 }
238 @Deprecated 240 @Deprecated
239 public static JCDiagnostic fragment(String key, Object... args) { 241 public static JCDiagnostic fragment(String key, Object... args) {
240 return new JCDiagnostic(getFragmentFormatter(), 242 return new JCDiagnostic(getFragmentFormatter(),
241 FRAGMENT, 243 FRAGMENT,
242 null, 244 null,
243 false, 245 EnumSet.noneOf(DiagnosticFlag.class),
244 null, 246 null,
245 null, 247 null,
246 "compiler." + FRAGMENT.key + "." + key, 248 "compiler." + FRAGMENT.key + "." + key,
247 args); 249 args);
248 } 250 }
325 } 327 }
326 328
327 private final int pos; 329 private final int pos;
328 } 330 }
329 331
332 public enum DiagnosticFlag {
333 MANDATORY,
334 RESOLVE_ERROR
335 }
336
330 private final DiagnosticType type; 337 private final DiagnosticType type;
331 private final DiagnosticSource source; 338 private final DiagnosticSource source;
332 private final DiagnosticPosition position; 339 private final DiagnosticPosition position;
333 private final int line; 340 private final int line;
334 private final int column; 341 private final int column;
335 private final String key; 342 private final String key;
336 protected final Object[] args; 343 protected final Object[] args;
337 private final boolean mandatory; 344 private final Set<DiagnosticFlag> flags;
338 private final LintCategory lintCategory; 345 private final LintCategory lintCategory;
339 346
340 /** 347 /**
341 * Create a diagnostic object. 348 * Create a diagnostic object.
342 * @param fomatter the formatter to use for the diagnostic 349 * @param fomatter the formatter to use for the diagnostic
348 * @param args arguments to be included in the text of the diagnostic 355 * @param args arguments to be included in the text of the diagnostic
349 */ 356 */
350 protected JCDiagnostic(DiagnosticFormatter<JCDiagnostic> formatter, 357 protected JCDiagnostic(DiagnosticFormatter<JCDiagnostic> formatter,
351 DiagnosticType dt, 358 DiagnosticType dt,
352 LintCategory lc, 359 LintCategory lc,
353 boolean mandatory, 360 Set<DiagnosticFlag> flags,
354 DiagnosticSource source, 361 DiagnosticSource source,
355 DiagnosticPosition pos, 362 DiagnosticPosition pos,
356 String key, 363 String key,
357 Object... args) { 364 Object... args) {
358 if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS) 365 if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS)
359 throw new IllegalArgumentException(); 366 throw new IllegalArgumentException();
360 367
361 this.defaultFormatter = formatter; 368 this.defaultFormatter = formatter;
362 this.type = dt; 369 this.type = dt;
363 this.lintCategory = lc; 370 this.lintCategory = lc;
364 this.mandatory = mandatory; 371 this.flags = flags;
365 this.source = source; 372 this.source = source;
366 this.position = pos; 373 this.position = pos;
367 this.key = key; 374 this.key = key;
368 this.args = args; 375 this.args = args;
369 376
399 /** 406 /**
400 * Check whether or not this diagnostic is required to be shown. 407 * Check whether or not this diagnostic is required to be shown.
401 * @return true if this diagnostic is required to be shown. 408 * @return true if this diagnostic is required to be shown.
402 */ 409 */
403 public boolean isMandatory() { 410 public boolean isMandatory() {
404 return mandatory; 411 return flags.contains(DiagnosticFlag.MANDATORY);
405 } 412 }
406 413
407 /** 414 /**
408 * Check whether this diagnostic has an associated lint category. 415 * Check whether this diagnostic has an associated lint category.
409 */ 416 */
518 public Diagnostic.Kind getKind() { 525 public Diagnostic.Kind getKind() {
519 switch (type) { 526 switch (type) {
520 case NOTE: 527 case NOTE:
521 return Diagnostic.Kind.NOTE; 528 return Diagnostic.Kind.NOTE;
522 case WARNING: 529 case WARNING:
523 return mandatory ? Diagnostic.Kind.MANDATORY_WARNING 530 return flags.contains(DiagnosticFlag.MANDATORY)
524 : Diagnostic.Kind.WARNING; 531 ? Diagnostic.Kind.MANDATORY_WARNING
532 : Diagnostic.Kind.WARNING;
525 case ERROR: 533 case ERROR:
526 return Diagnostic.Kind.ERROR; 534 return Diagnostic.Kind.ERROR;
527 default: 535 default:
528 return Diagnostic.Kind.OTHER; 536 return Diagnostic.Kind.OTHER;
529 } 537 }
535 543
536 public String getMessage(Locale locale) { 544 public String getMessage(Locale locale) {
537 return defaultFormatter.formatMessage(this, locale); 545 return defaultFormatter.formatMessage(this, locale);
538 } 546 }
539 547
548 public void setFlag(DiagnosticFlag flag) {
549 flags.add(flag);
550 }
551
552 public boolean isFlagSet(DiagnosticFlag flag) {
553 return flags.contains(flag);
554 }
555
540 public static class MultilineDiagnostic extends JCDiagnostic { 556 public static class MultilineDiagnostic extends JCDiagnostic {
541 557
542 private final List<JCDiagnostic> subdiagnostics; 558 private final List<JCDiagnostic> subdiagnostics;
543 559
544 public MultilineDiagnostic(JCDiagnostic other, List<JCDiagnostic> subdiagnostics) { 560 public MultilineDiagnostic(JCDiagnostic other, List<JCDiagnostic> subdiagnostics) {
545 super(other.defaultFormatter, 561 super(other.defaultFormatter,
546 other.getType(), 562 other.getType(),
547 other.getLintCategory(), 563 other.getLintCategory(),
548 other.isMandatory(), 564 other.flags,
549 other.getDiagnosticSource(), 565 other.getDiagnosticSource(),
550 other.position, 566 other.position,
551 other.getCode(), 567 other.getCode(),
552 other.getArgs()); 568 other.getArgs());
553 this.subdiagnostics = subdiagnostics; 569 this.subdiagnostics = subdiagnostics;

mercurial