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

changeset 612
d1bd93028447
parent 581
f2fdd52e4e87
child 643
a626d8c1de6e
equal deleted inserted replaced
611:4172cfff05f0 612:d1bd93028447
30 30
31 import javax.tools.Diagnostic; 31 import javax.tools.Diagnostic;
32 import javax.tools.JavaFileObject; 32 import javax.tools.JavaFileObject;
33 33
34 import com.sun.tools.javac.api.DiagnosticFormatter; 34 import com.sun.tools.javac.api.DiagnosticFormatter;
35 import com.sun.tools.javac.code.Lint.LintCategory;
35 import com.sun.tools.javac.tree.JCTree; 36 import com.sun.tools.javac.tree.JCTree;
36 37
37 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*; 38 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*;
38 39
39 /** An abstraction of a diagnostic message generated by the compiler. 40 /** An abstraction of a diagnostic message generated by the compiler.
80 * @param key The key for the localized error message. 81 * @param key The key for the localized error message.
81 * @param args Fields of the error message. 82 * @param args Fields of the error message.
82 */ 83 */
83 public JCDiagnostic error( 84 public JCDiagnostic error(
84 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 85 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
85 return create(ERROR, true, source, pos, key, args); 86 return create(ERROR, null, true, source, pos, key, args);
86 } 87 }
87 88
88 /** 89 /**
89 * 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.
90 * @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.
91 * @param pos The source position at which to report the warning. 92 * @param pos The source position at which to report the warning.
93 * @param key The key for the localized warning message.
94 * @param args Fields of the warning message.
95 * @see MandatoryWarningHandler
96 */
97 public JCDiagnostic mandatoryWarning(
98 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
99 return create(WARNING, null, true, source, pos, key, args);
100 }
101
102 /**
103 * 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
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 warning message.
108 * @param args Fields of the warning message.
109 * @see MandatoryWarningHandler
110 */
111 public JCDiagnostic mandatoryWarning(
112 LintCategory lc,
113 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
114 return create(WARNING, lc, true, source, pos, key, args);
115 }
116
117 /**
118 * Create a warning diagnostic.
119 * @param lc The lint category for the diagnostic
92 * @param key The key for the localized error message. 120 * @param key The key for the localized error message.
93 * @param args Fields of the error message. 121 * @param args Fields of the warning message.
94 * @see MandatoryWarningHandler 122 * @see MandatoryWarningHandler
95 */ 123 */
96 public JCDiagnostic mandatoryWarning( 124 public JCDiagnostic warning(
97 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 125 LintCategory lc, String key, Object... args) {
98 return create(WARNING, true, source, pos, key, args); 126 return create(WARNING, lc, false, null, null, key, args);
99 } 127 }
100 128
101 /** 129 /**
102 * Create a warning diagnostic. 130 * Create a warning diagnostic.
103 * @param source The source of the compilation unit, if any, in which to report the warning. 131 * @param source The source of the compilation unit, if any, in which to report the warning.
104 * @param pos The source position at which to report the warning. 132 * @param pos The source position at which to report the warning.
105 * @param key The key for the localized error message. 133 * @param key The key for the localized warning message.
106 * @param args Fields of the error message. 134 * @param args Fields of the warning message.
107 */ 135 */
108 public JCDiagnostic warning( 136 public JCDiagnostic warning(
109 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 137 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
110 return create(WARNING, false, source, pos, key, args); 138 return create(WARNING, null, false, source, pos, key, args);
139 }
140
141 /**
142 * Create a warning diagnostic.
143 * @param lc The lint category for the diagnostic
144 * @param source The source of the compilation unit, if any, in which to report the warning.
145 * @param pos The source position at which to report the warning.
146 * @param key The key for the localized warning message.
147 * @param args Fields of the warning message.
148 * @see MandatoryWarningHandler
149 */
150 public JCDiagnostic warning(
151 LintCategory lc, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
152 return create(WARNING, lc, false, source, pos, key, args);
111 } 153 }
112 154
113 /** 155 /**
114 * Create a note diagnostic that will not be hidden by the -nowarn or -Xlint:none options. 156 * Create a note diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
115 * @param key The key for the localized error message. 157 * @param key The key for the localized message.
116 * @param args Fields of the error message. 158 * @param args Fields of the message.
117 * @see MandatoryWarningHandler 159 * @see MandatoryWarningHandler
118 */ 160 */
119 public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) { 161 public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) {
120 return create(NOTE, true, source, null, key, args); 162 return create(NOTE, null, true, source, null, key, args);
121 } 163 }
122 164
123 /** 165 /**
124 * Create a note diagnostic. 166 * Create a note diagnostic.
125 * @param key The key for the localized error message. 167 * @param key The key for the localized error message.
126 * @param args Fields of the error message. 168 * @param args Fields of the message.
127 */ 169 */
128 public JCDiagnostic note(String key, Object... args) { 170 public JCDiagnostic note(String key, Object... args) {
129 return create(NOTE, false, null, null, key, args); 171 return create(NOTE, null, false, null, null, key, args);
130 } 172 }
131 173
132 /** 174 /**
133 * Create a note diagnostic. 175 * Create a note diagnostic.
134 * @param source The source of the compilation unit, if any, in which to report the note. 176 * @param source The source of the compilation unit, if any, in which to report the note.
135 * @param pos The source position at which to report the note. 177 * @param pos The source position at which to report the note.
136 * @param key The key for the localized error message. 178 * @param key The key for the localized message.
137 * @param args Fields of the error message. 179 * @param args Fields of the message.
138 */ 180 */
139 public JCDiagnostic note( 181 public JCDiagnostic note(
140 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 182 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
141 return create(NOTE, false, source, pos, key, args); 183 return create(NOTE, null, false, source, pos, key, args);
142 } 184 }
143 185
144 /** 186 /**
145 * Create a fragment diagnostic, for use as an argument in other diagnostics 187 * Create a fragment diagnostic, for use as an argument in other diagnostics
146 * @param key The key for the localized error message. 188 * @param key The key for the localized message.
147 * @param args Fields of the error message. 189 * @param args Fields of the message.
148 */ 190 */
149 public JCDiagnostic fragment(String key, Object... args) { 191 public JCDiagnostic fragment(String key, Object... args) {
150 return create(FRAGMENT, false, null, null, key, args); 192 return create(FRAGMENT, null, false, null, null, key, args);
193 }
194
195 /**
196 * Create a new diagnostic of the given kind, which is not mandatory and which has
197 * no lint category.
198 * @param kind The diagnostic kind
199 * @param ls The lint category, if applicable, or null
200 * @param source The source of the compilation unit, if any, in which to report the message.
201 * @param pos The source position at which to report the message.
202 * @param key The key for the localized message.
203 * @param args Fields of the message.
204 */
205 public JCDiagnostic create(
206 DiagnosticType kind, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
207 return create(kind, null, false, source, pos, key, args);
151 } 208 }
152 209
153 /** 210 /**
154 * Create a new diagnostic of the given kind. 211 * Create a new diagnostic of the given kind.
155 * @param kind The diagnostic kind 212 * @param kind The diagnostic kind
213 * @param lc The lint category, if applicable, or null
156 * @param isMandatory is diagnostic mandatory? 214 * @param isMandatory is diagnostic mandatory?
157 * @param source The source of the compilation unit, if any, in which to report the note. 215 * @param source The source of the compilation unit, if any, in which to report the message.
158 * @param pos The source position at which to report the note. 216 * @param pos The source position at which to report the message.
159 * @param key The key for the localized error message. 217 * @param key The key for the localized message.
160 * @param args Fields of the error message. 218 * @param args Fields of the message.
161 */ 219 */
162 public JCDiagnostic create( 220 public JCDiagnostic create(
163 DiagnosticType kind, boolean isMandatory, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { 221 DiagnosticType kind, LintCategory lc, boolean isMandatory, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
164 return new JCDiagnostic(formatter, kind, isMandatory, source, pos, qualify(kind, key), args); 222 return new JCDiagnostic(formatter, kind, lc, isMandatory, source, pos, qualify(kind, key), args);
165 } 223 }
166 224
167 protected String qualify(DiagnosticType t, String key) { 225 protected String qualify(DiagnosticType t, String key) {
168 return prefix + "." + t.key + "." + key; 226 return prefix + "." + t.key + "." + key;
169 } 227 }
179 */ 237 */
180 @Deprecated 238 @Deprecated
181 public static JCDiagnostic fragment(String key, Object... args) { 239 public static JCDiagnostic fragment(String key, Object... args) {
182 return new JCDiagnostic(getFragmentFormatter(), 240 return new JCDiagnostic(getFragmentFormatter(),
183 FRAGMENT, 241 FRAGMENT,
242 null,
184 false, 243 false,
185 null, 244 null,
186 null, 245 null,
187 "compiler." + FRAGMENT.key + "." + key, 246 "compiler." + FRAGMENT.key + "." + key,
188 args); 247 args);
272 private final DiagnosticSource source; 331 private final DiagnosticSource source;
273 private final DiagnosticPosition position; 332 private final DiagnosticPosition position;
274 private final int line; 333 private final int line;
275 private final int column; 334 private final int column;
276 private final String key; 335 private final String key;
277 protected Object[] args; 336 protected final Object[] args;
278 private boolean mandatory; 337 private final boolean mandatory;
338 private final LintCategory lintCategory;
279 339
280 /** 340 /**
281 * Create a diagnostic object. 341 * Create a diagnostic object.
282 * @param messages the resource for localized messages 342 * @param fomatter the formatter to use for the diagnostic
283 * @param dt the type of diagnostic 343 * @param dt the type of diagnostic
284 * @param name the name of the source file, or null if none. 344 * @param lc the lint category for the diagnostic
345 * @param source the name of the source file, or null if none.
285 * @param pos the character offset within the source file, if given. 346 * @param pos the character offset within the source file, if given.
286 * @param key a resource key to identify the text of the diagnostic 347 * @param key a resource key to identify the text of the diagnostic
287 * @param args arguments to be included in the text of the diagnostic 348 * @param args arguments to be included in the text of the diagnostic
288 */ 349 */
289 protected JCDiagnostic(DiagnosticFormatter<JCDiagnostic> formatter, 350 protected JCDiagnostic(DiagnosticFormatter<JCDiagnostic> formatter,
290 DiagnosticType dt, 351 DiagnosticType dt,
352 LintCategory lc,
291 boolean mandatory, 353 boolean mandatory,
292 DiagnosticSource source, 354 DiagnosticSource source,
293 DiagnosticPosition pos, 355 DiagnosticPosition pos,
294 String key, 356 String key,
295 Object ... args) { 357 Object... args) {
296 if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS) 358 if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS)
297 throw new IllegalArgumentException(); 359 throw new IllegalArgumentException();
298 360
299 this.defaultFormatter = formatter; 361 this.defaultFormatter = formatter;
300 this.type = dt; 362 this.type = dt;
363 this.lintCategory = lc;
301 this.mandatory = mandatory; 364 this.mandatory = mandatory;
302 this.source = source; 365 this.source = source;
303 this.position = pos; 366 this.position = pos;
304 this.key = key; 367 this.key = key;
305 this.args = args; 368 this.args = args;
337 * Check whether or not this diagnostic is required to be shown. 400 * Check whether or not this diagnostic is required to be shown.
338 * @return true if this diagnostic is required to be shown. 401 * @return true if this diagnostic is required to be shown.
339 */ 402 */
340 public boolean isMandatory() { 403 public boolean isMandatory() {
341 return mandatory; 404 return mandatory;
405 }
406
407 /**
408 * Check whether this diagnostic has an associated lint category.
409 */
410 public boolean hasLintCategory() {
411 return (lintCategory != null);
412 }
413
414 /**
415 * Get the associated lint category, or null if none.
416 */
417 public LintCategory getLintCategory() {
418 return lintCategory;
342 } 419 }
343 420
344 /** 421 /**
345 * Get the name of the source file referred to by this diagnostic. 422 * Get the name of the source file referred to by this diagnostic.
346 * @return the name of the source referred to with this diagnostic, or null if none 423 * @return the name of the source referred to with this diagnostic, or null if none
465 private final List<JCDiagnostic> subdiagnostics; 542 private final List<JCDiagnostic> subdiagnostics;
466 543
467 public MultilineDiagnostic(JCDiagnostic other, List<JCDiagnostic> subdiagnostics) { 544 public MultilineDiagnostic(JCDiagnostic other, List<JCDiagnostic> subdiagnostics) {
468 super(other.defaultFormatter, 545 super(other.defaultFormatter,
469 other.getType(), 546 other.getType(),
547 other.getLintCategory(),
470 other.isMandatory(), 548 other.isMandatory(),
471 other.getDiagnosticSource(), 549 other.getDiagnosticSource(),
472 other.position, 550 other.position,
473 other.getCode(), 551 other.getCode(),
474 other.getArgs()); 552 other.getArgs());

mercurial