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

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 1280
5c0b3faeb0b0
child 1358
fc123bdeddb8
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

duke@1 1 /*
jjg@1280 2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.util;
duke@1 27
jjg@643 28 import java.util.EnumSet;
duke@1 29 import java.util.Locale;
jjg@643 30 import java.util.Set;
duke@1 31
duke@1 32 import javax.tools.Diagnostic;
duke@1 33 import javax.tools.JavaFileObject;
duke@1 34
mcimadamore@83 35 import com.sun.tools.javac.api.DiagnosticFormatter;
jjg@612 36 import com.sun.tools.javac.code.Lint.LintCategory;
jjg@1280 37 import com.sun.tools.javac.tree.EndPosTable;
duke@1 38 import com.sun.tools.javac.tree.JCTree;
duke@1 39
duke@1 40 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*;
duke@1 41
duke@1 42 /** An abstraction of a diagnostic message generated by the compiler.
duke@1 43 *
jjg@581 44 * <p><b>This is NOT part of any supported API.
jjg@581 45 * If you write code that depends on this, you do so at your own risk.
duke@1 46 * This code and its internal interfaces are subject to change or
duke@1 47 * deletion without notice.</b>
duke@1 48 */
duke@1 49 public class JCDiagnostic implements Diagnostic<JavaFileObject> {
duke@1 50 /** A factory for creating diagnostic objects. */
duke@1 51 public static class Factory {
duke@1 52 /** The context key for the diagnostic factory. */
duke@1 53 protected static final Context.Key<JCDiagnostic.Factory> diagnosticFactoryKey =
duke@1 54 new Context.Key<JCDiagnostic.Factory>();
duke@1 55
duke@1 56 /** Get the Factory instance for this context. */
duke@1 57 public static Factory instance(Context context) {
duke@1 58 Factory instance = context.get(diagnosticFactoryKey);
duke@1 59 if (instance == null)
duke@1 60 instance = new Factory(context);
duke@1 61 return instance;
duke@1 62 }
duke@1 63
mcimadamore@89 64 DiagnosticFormatter<JCDiagnostic> formatter;
duke@1 65 final String prefix;
jjg@726 66 final Set<DiagnosticFlag> defaultErrorFlags;
duke@1 67
duke@1 68 /** Create a new diagnostic factory. */
duke@1 69 protected Factory(Context context) {
mcimadamore@136 70 this(JavacMessages.instance(context), "compiler");
duke@1 71 context.put(diagnosticFactoryKey, this);
jjg@726 72
jjg@1135 73 final Options options = Options.instance(context);
jjg@1135 74 initOptions(options);
jjg@1135 75 options.addListener(new Runnable() {
jjg@1135 76 public void run() {
jjg@1135 77 initOptions(options);
jjg@1135 78 }
jjg@1135 79 });
jjg@1135 80 }
jjg@1135 81
jjg@1135 82 private void initOptions(Options options) {
jjg@726 83 if (options.isSet("onlySyntaxErrorsUnrecoverable"))
jjg@726 84 defaultErrorFlags.add(DiagnosticFlag.RECOVERABLE);
duke@1 85 }
duke@1 86
duke@1 87 /** Create a new diagnostic factory. */
mcimadamore@136 88 public Factory(JavacMessages messages, String prefix) {
duke@1 89 this.prefix = prefix;
mcimadamore@89 90 this.formatter = new BasicDiagnosticFormatter(messages);
jjg@726 91 defaultErrorFlags = EnumSet.of(DiagnosticFlag.MANDATORY);
duke@1 92 }
duke@1 93
duke@1 94 /**
duke@1 95 * Create an error diagnostic.
duke@1 96 * @param source The source of the compilation unit, if any, in which to report the error.
duke@1 97 * @param pos The source position at which to report the error.
duke@1 98 * @param key The key for the localized error message.
duke@1 99 * @param args Fields of the error message.
duke@1 100 */
duke@1 101 public JCDiagnostic error(
duke@1 102 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
jjg@726 103 return create(ERROR, null, defaultErrorFlags, source, pos, key, args);
duke@1 104 }
duke@1 105
duke@1 106 /**
duke@1 107 * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
duke@1 108 * @param source The source of the compilation unit, if any, in which to report the warning.
duke@1 109 * @param pos The source position at which to report the warning.
jjg@612 110 * @param key The key for the localized warning message.
jjg@612 111 * @param args Fields of the warning message.
duke@1 112 * @see MandatoryWarningHandler
duke@1 113 */
duke@1 114 public JCDiagnostic mandatoryWarning(
jjg@612 115 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
jjg@643 116 return create(WARNING, null, EnumSet.of(DiagnosticFlag.MANDATORY), source, pos, key, args);
jjg@612 117 }
jjg@612 118
jjg@612 119 /**
jjg@612 120 * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
jjg@612 121 * @param lc The lint category for the diagnostic
jjg@612 122 * @param source The source of the compilation unit, if any, in which to report the warning.
jjg@612 123 * @param pos The source position at which to report the warning.
jjg@612 124 * @param key The key for the localized warning message.
jjg@612 125 * @param args Fields of the warning message.
jjg@612 126 * @see MandatoryWarningHandler
jjg@612 127 */
jjg@612 128 public JCDiagnostic mandatoryWarning(
jjg@612 129 LintCategory lc,
jjg@612 130 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
jjg@643 131 return create(WARNING, lc, EnumSet.of(DiagnosticFlag.MANDATORY), source, pos, key, args);
jjg@612 132 }
jjg@612 133
jjg@612 134 /**
jjg@612 135 * Create a warning diagnostic.
jjg@612 136 * @param lc The lint category for the diagnostic
jjg@612 137 * @param key The key for the localized error message.
jjg@612 138 * @param args Fields of the warning message.
jjg@612 139 * @see MandatoryWarningHandler
jjg@612 140 */
jjg@612 141 public JCDiagnostic warning(
jjg@612 142 LintCategory lc, String key, Object... args) {
jjg@643 143 return create(WARNING, lc, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args);
duke@1 144 }
duke@1 145
duke@1 146 /**
duke@1 147 * Create a warning diagnostic.
duke@1 148 * @param source The source of the compilation unit, if any, in which to report the warning.
duke@1 149 * @param pos The source position at which to report the warning.
jjg@612 150 * @param key The key for the localized warning message.
jjg@612 151 * @param args Fields of the warning message.
duke@1 152 */
duke@1 153 public JCDiagnostic warning(
duke@1 154 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
jjg@643 155 return create(WARNING, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args);
jjg@612 156 }
jjg@612 157
jjg@612 158 /**
jjg@612 159 * Create a warning diagnostic.
jjg@612 160 * @param lc The lint category for the diagnostic
jjg@612 161 * @param source The source of the compilation unit, if any, in which to report the warning.
jjg@612 162 * @param pos The source position at which to report the warning.
jjg@612 163 * @param key The key for the localized warning message.
jjg@612 164 * @param args Fields of the warning message.
jjg@612 165 * @see MandatoryWarningHandler
jjg@612 166 */
jjg@612 167 public JCDiagnostic warning(
jjg@612 168 LintCategory lc, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
jjg@643 169 return create(WARNING, lc, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args);
duke@1 170 }
duke@1 171
duke@1 172 /**
duke@1 173 * Create a note diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
jjg@612 174 * @param key The key for the localized message.
jjg@612 175 * @param args Fields of the message.
duke@1 176 * @see MandatoryWarningHandler
duke@1 177 */
duke@1 178 public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) {
jjg@643 179 return create(NOTE, null, EnumSet.of(DiagnosticFlag.MANDATORY), source, null, key, args);
duke@1 180 }
duke@1 181
duke@1 182 /**
duke@1 183 * Create a note diagnostic.
duke@1 184 * @param key The key for the localized error message.
jjg@612 185 * @param args Fields of the message.
duke@1 186 */
duke@1 187 public JCDiagnostic note(String key, Object... args) {
jjg@643 188 return create(NOTE, null, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args);
duke@1 189 }
duke@1 190
duke@1 191 /**
duke@1 192 * Create a note diagnostic.
duke@1 193 * @param source The source of the compilation unit, if any, in which to report the note.
duke@1 194 * @param pos The source position at which to report the note.
jjg@612 195 * @param key The key for the localized message.
jjg@612 196 * @param args Fields of the message.
duke@1 197 */
duke@1 198 public JCDiagnostic note(
duke@1 199 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
jjg@643 200 return create(NOTE, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args);
duke@1 201 }
duke@1 202
duke@1 203 /**
duke@1 204 * Create a fragment diagnostic, for use as an argument in other diagnostics
jjg@612 205 * @param key The key for the localized message.
jjg@612 206 * @param args Fields of the message.
duke@1 207 */
duke@1 208 public JCDiagnostic fragment(String key, Object... args) {
jjg@643 209 return create(FRAGMENT, null, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args);
jjg@612 210 }
jjg@612 211
jjg@612 212 /**
jjg@612 213 * Create a new diagnostic of the given kind, which is not mandatory and which has
jjg@612 214 * no lint category.
jjg@612 215 * @param kind The diagnostic kind
jjg@612 216 * @param ls The lint category, if applicable, or null
jjg@612 217 * @param source The source of the compilation unit, if any, in which to report the message.
jjg@612 218 * @param pos The source position at which to report the message.
jjg@612 219 * @param key The key for the localized message.
jjg@612 220 * @param args Fields of the message.
jjg@612 221 */
jjg@612 222 public JCDiagnostic create(
jjg@612 223 DiagnosticType kind, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
jjg@643 224 return create(kind, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args);
mcimadamore@302 225 }
mcimadamore@302 226
mcimadamore@302 227 /**
mcimadamore@302 228 * Create a new diagnostic of the given kind.
mcimadamore@302 229 * @param kind The diagnostic kind
jjg@612 230 * @param lc The lint category, if applicable, or null
mcimadamore@302 231 * @param isMandatory is diagnostic mandatory?
jjg@612 232 * @param source The source of the compilation unit, if any, in which to report the message.
jjg@612 233 * @param pos The source position at which to report the message.
jjg@612 234 * @param key The key for the localized message.
jjg@612 235 * @param args Fields of the message.
mcimadamore@302 236 */
mcimadamore@302 237 public JCDiagnostic create(
jjg@643 238 DiagnosticType kind, LintCategory lc, Set<DiagnosticFlag> flags, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
jjg@643 239 return new JCDiagnostic(formatter, kind, lc, flags, source, pos, qualify(kind, key), args);
duke@1 240 }
duke@1 241
duke@1 242 protected String qualify(DiagnosticType t, String key) {
duke@1 243 return prefix + "." + t.key + "." + key;
duke@1 244 }
duke@1 245 }
duke@1 246
duke@1 247
duke@1 248
duke@1 249 /**
duke@1 250 * Create a fragment diagnostic, for use as an argument in other diagnostics
duke@1 251 * @param key The key for the localized error message.
duke@1 252 * @param args Fields of the error message.
mcimadamore@89 253 *
duke@1 254 */
mcimadamore@89 255 @Deprecated
duke@1 256 public static JCDiagnostic fragment(String key, Object... args) {
mcimadamore@89 257 return new JCDiagnostic(getFragmentFormatter(),
duke@1 258 FRAGMENT,
jjg@612 259 null,
jjg@643 260 EnumSet.noneOf(DiagnosticFlag.class),
duke@1 261 null,
duke@1 262 null,
duke@1 263 "compiler." + FRAGMENT.key + "." + key,
duke@1 264 args);
duke@1 265 }
mcimadamore@89 266 //where
mcimadamore@89 267 @Deprecated
mcimadamore@89 268 public static DiagnosticFormatter<JCDiagnostic> getFragmentFormatter() {
mcimadamore@89 269 if (fragmentFormatter == null) {
mcimadamore@136 270 fragmentFormatter = new BasicDiagnosticFormatter(JavacMessages.getDefaultMessages());
mcimadamore@89 271 }
mcimadamore@89 272 return fragmentFormatter;
mcimadamore@89 273 }
duke@1 274
duke@1 275 /**
duke@1 276 * A DiagnosticType defines the type of the diagnostic.
duke@1 277 **/
duke@1 278 public enum DiagnosticType {
duke@1 279 /** A fragment of an enclosing diagnostic. */
duke@1 280 FRAGMENT("misc"),
duke@1 281 /** A note: similar to, but less serious than, a warning. */
duke@1 282 NOTE("note"),
duke@1 283 /** A warning. */
duke@1 284 WARNING("warn"),
duke@1 285 /** An error. */
duke@1 286 ERROR("err");
duke@1 287
duke@1 288 final String key;
duke@1 289
duke@1 290 /** Create a DiagnosticType.
duke@1 291 * @param key A string used to create the resource key for the diagnostic.
duke@1 292 */
duke@1 293 DiagnosticType(String key) {
duke@1 294 this.key = key;
duke@1 295 }
duke@1 296 };
duke@1 297
duke@1 298 /**
duke@1 299 * A DiagnosticPosition provides information about the positions in a file
duke@1 300 * that gave rise to a diagnostic. It always defines a "preferred" position
duke@1 301 * that most accurately defines the location of the diagnostic, it may also
duke@1 302 * provide a related tree node that spans that location.
duke@1 303 */
duke@1 304 public static interface DiagnosticPosition {
duke@1 305 /** Gets the tree node, if any, to which the diagnostic applies. */
duke@1 306 JCTree getTree();
duke@1 307 /** If there is a tree node, get the start position of the tree node.
duke@1 308 * Otherwise, just returns the same as getPreferredPosition(). */
duke@1 309 int getStartPosition();
duke@1 310 /** Get the position within the file that most accurately defines the
duke@1 311 * location for the diagnostic. */
duke@1 312 int getPreferredPosition();
duke@1 313 /** If there is a tree node, and if endPositions are available, get
duke@1 314 * the end position of the tree node. Otherwise, just returns the
duke@1 315 * same as getPreferredPosition(). */
ksrini@1138 316 int getEndPosition(EndPosTable endPosTable);
duke@1 317 }
duke@1 318
duke@1 319 /**
duke@1 320 * A DiagnosticPosition that simply identifies a position, but no related
duke@1 321 * tree node, as the location for a diagnostic. Used for scanner and parser
duke@1 322 * diagnostics. */
duke@1 323 public static class SimpleDiagnosticPosition implements DiagnosticPosition {
duke@1 324 public SimpleDiagnosticPosition(int pos) {
duke@1 325 this.pos = pos;
duke@1 326 }
duke@1 327
duke@1 328 public JCTree getTree() {
duke@1 329 return null;
duke@1 330 }
duke@1 331
duke@1 332 public int getStartPosition() {
duke@1 333 return pos;
duke@1 334 }
duke@1 335
duke@1 336 public int getPreferredPosition() {
duke@1 337 return pos;
duke@1 338 }
duke@1 339
ksrini@1138 340 public int getEndPosition(EndPosTable endPosTable) {
duke@1 341 return pos;
duke@1 342 }
duke@1 343
duke@1 344 private final int pos;
duke@1 345 }
duke@1 346
jjg@643 347 public enum DiagnosticFlag {
jjg@643 348 MANDATORY,
jjg@726 349 RESOLVE_ERROR,
jjg@726 350 SYNTAX,
jjg@726 351 RECOVERABLE
jjg@643 352 }
jjg@643 353
duke@1 354 private final DiagnosticType type;
duke@1 355 private final DiagnosticSource source;
duke@1 356 private final DiagnosticPosition position;
duke@1 357 private final int line;
duke@1 358 private final int column;
duke@1 359 private final String key;
jjg@612 360 protected final Object[] args;
jjg@643 361 private final Set<DiagnosticFlag> flags;
jjg@612 362 private final LintCategory lintCategory;
duke@1 363
duke@1 364 /**
duke@1 365 * Create a diagnostic object.
jjg@612 366 * @param fomatter the formatter to use for the diagnostic
duke@1 367 * @param dt the type of diagnostic
jjg@612 368 * @param lc the lint category for the diagnostic
jjg@612 369 * @param source the name of the source file, or null if none.
duke@1 370 * @param pos the character offset within the source file, if given.
duke@1 371 * @param key a resource key to identify the text of the diagnostic
duke@1 372 * @param args arguments to be included in the text of the diagnostic
duke@1 373 */
mcimadamore@89 374 protected JCDiagnostic(DiagnosticFormatter<JCDiagnostic> formatter,
duke@1 375 DiagnosticType dt,
jjg@612 376 LintCategory lc,
jjg@643 377 Set<DiagnosticFlag> flags,
duke@1 378 DiagnosticSource source,
duke@1 379 DiagnosticPosition pos,
duke@1 380 String key,
jjg@612 381 Object... args) {
duke@1 382 if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS)
duke@1 383 throw new IllegalArgumentException();
duke@1 384
mcimadamore@89 385 this.defaultFormatter = formatter;
duke@1 386 this.type = dt;
jjg@612 387 this.lintCategory = lc;
jjg@643 388 this.flags = flags;
duke@1 389 this.source = source;
duke@1 390 this.position = pos;
duke@1 391 this.key = key;
mcimadamore@168 392 this.args = args;
duke@1 393
duke@1 394 int n = (pos == null ? Position.NOPOS : pos.getPreferredPosition());
duke@1 395 if (n == Position.NOPOS || source == null)
duke@1 396 line = column = -1;
duke@1 397 else {
duke@1 398 line = source.getLineNumber(n);
mcimadamore@100 399 column = source.getColumnNumber(n, true);
duke@1 400 }
duke@1 401 }
duke@1 402
duke@1 403 /**
duke@1 404 * Get the type of this diagnostic.
duke@1 405 * @return the type of this diagnostic
duke@1 406 */
duke@1 407 public DiagnosticType getType() {
duke@1 408 return type;
duke@1 409 }
duke@1 410
duke@1 411 /**
mcimadamore@168 412 * Get the subdiagnostic list
mcimadamore@168 413 * @return subdiagnostic list
mcimadamore@168 414 */
mcimadamore@168 415 public List<JCDiagnostic> getSubdiagnostics() {
mcimadamore@168 416 return List.nil();
mcimadamore@168 417 }
mcimadamore@168 418
mcimadamore@168 419 public boolean isMultiline() {
mcimadamore@168 420 return false;
mcimadamore@168 421 }
mcimadamore@168 422
mcimadamore@168 423 /**
duke@1 424 * Check whether or not this diagnostic is required to be shown.
duke@1 425 * @return true if this diagnostic is required to be shown.
duke@1 426 */
duke@1 427 public boolean isMandatory() {
jjg@643 428 return flags.contains(DiagnosticFlag.MANDATORY);
duke@1 429 }
duke@1 430
duke@1 431 /**
jjg@612 432 * Check whether this diagnostic has an associated lint category.
jjg@612 433 */
jjg@612 434 public boolean hasLintCategory() {
jjg@612 435 return (lintCategory != null);
jjg@612 436 }
jjg@612 437
jjg@612 438 /**
jjg@612 439 * Get the associated lint category, or null if none.
jjg@612 440 */
jjg@612 441 public LintCategory getLintCategory() {
jjg@612 442 return lintCategory;
jjg@612 443 }
jjg@612 444
jjg@612 445 /**
duke@1 446 * Get the name of the source file referred to by this diagnostic.
duke@1 447 * @return the name of the source referred to with this diagnostic, or null if none
duke@1 448 */
duke@1 449 public JavaFileObject getSource() {
duke@1 450 if (source == null)
duke@1 451 return null;
duke@1 452 else
duke@1 453 return source.getFile();
duke@1 454 }
duke@1 455
duke@1 456 /**
duke@1 457 * Get the source referred to by this diagnostic.
duke@1 458 * @return the source referred to with this diagnostic, or null if none
duke@1 459 */
duke@1 460 public DiagnosticSource getDiagnosticSource() {
duke@1 461 return source;
duke@1 462 }
duke@1 463
duke@1 464 protected int getIntStartPosition() {
duke@1 465 return (position == null ? Position.NOPOS : position.getStartPosition());
duke@1 466 }
duke@1 467
duke@1 468 protected int getIntPosition() {
duke@1 469 return (position == null ? Position.NOPOS : position.getPreferredPosition());
duke@1 470 }
duke@1 471
duke@1 472 protected int getIntEndPosition() {
duke@1 473 return (position == null ? Position.NOPOS : position.getEndPosition(source.getEndPosTable()));
duke@1 474 }
duke@1 475
duke@1 476 public long getStartPosition() {
duke@1 477 return getIntStartPosition();
duke@1 478 }
duke@1 479
duke@1 480 public long getPosition() {
duke@1 481 return getIntPosition();
duke@1 482 }
duke@1 483
duke@1 484 public long getEndPosition() {
duke@1 485 return getIntEndPosition();
duke@1 486 }
duke@1 487
duke@1 488 /**
duke@1 489 * Get the line number within the source referred to by this diagnostic.
duke@1 490 * @return the line number within the source referred to by this diagnostic
duke@1 491 */
duke@1 492 public long getLineNumber() {
duke@1 493 return line;
duke@1 494 }
duke@1 495
duke@1 496 /**
duke@1 497 * Get the column number within the line of source referred to by this diagnostic.
duke@1 498 * @return the column number within the line of source referred to by this diagnostic
duke@1 499 */
duke@1 500 public long getColumnNumber() {
duke@1 501 return column;
duke@1 502 }
duke@1 503
duke@1 504 /**
duke@1 505 * Get the arguments to be included in the text of the diagnostic.
duke@1 506 * @return the arguments to be included in the text of the diagnostic
duke@1 507 */
duke@1 508 public Object[] getArgs() {
duke@1 509 return args;
duke@1 510 }
duke@1 511
duke@1 512 /**
duke@1 513 * Get the prefix string associated with this type of diagnostic.
duke@1 514 * @return the prefix string associated with this type of diagnostic
duke@1 515 */
duke@1 516 public String getPrefix() {
duke@1 517 return getPrefix(type);
duke@1 518 }
duke@1 519
duke@1 520 /**
duke@1 521 * Get the prefix string associated with a particular type of diagnostic.
duke@1 522 * @return the prefix string associated with a particular type of diagnostic
duke@1 523 */
duke@1 524 public String getPrefix(DiagnosticType dt) {
mcimadamore@89 525 return defaultFormatter.formatKind(this, Locale.getDefault());
mcimadamore@83 526 }
mcimadamore@83 527
duke@1 528 /**
duke@1 529 * Return the standard presentation of this diagnostic.
duke@1 530 */
jjg@415 531 @Override
duke@1 532 public String toString() {
mcimadamore@89 533 return defaultFormatter.format(this,Locale.getDefault());
duke@1 534 }
duke@1 535
mcimadamore@89 536 private DiagnosticFormatter<JCDiagnostic> defaultFormatter;
mcimadamore@89 537 @Deprecated
mcimadamore@89 538 private static DiagnosticFormatter<JCDiagnostic> fragmentFormatter;
duke@1 539
duke@1 540 // Methods for javax.tools.Diagnostic
duke@1 541
duke@1 542 public Diagnostic.Kind getKind() {
duke@1 543 switch (type) {
duke@1 544 case NOTE:
duke@1 545 return Diagnostic.Kind.NOTE;
duke@1 546 case WARNING:
jjg@643 547 return flags.contains(DiagnosticFlag.MANDATORY)
jjg@643 548 ? Diagnostic.Kind.MANDATORY_WARNING
jjg@643 549 : Diagnostic.Kind.WARNING;
duke@1 550 case ERROR:
duke@1 551 return Diagnostic.Kind.ERROR;
duke@1 552 default:
duke@1 553 return Diagnostic.Kind.OTHER;
duke@1 554 }
duke@1 555 }
duke@1 556
duke@1 557 public String getCode() {
duke@1 558 return key;
duke@1 559 }
duke@1 560
duke@1 561 public String getMessage(Locale locale) {
mcimadamore@89 562 return defaultFormatter.formatMessage(this, locale);
duke@1 563 }
mcimadamore@168 564
jjg@643 565 public void setFlag(DiagnosticFlag flag) {
jjg@643 566 flags.add(flag);
jjg@726 567
jjg@726 568 if (type == DiagnosticType.ERROR) {
jjg@726 569 switch (flag) {
jjg@726 570 case SYNTAX:
jjg@726 571 flags.remove(DiagnosticFlag.RECOVERABLE);
jjg@726 572 break;
jjg@726 573 case RESOLVE_ERROR:
jjg@726 574 flags.add(DiagnosticFlag.RECOVERABLE);
jjg@726 575 break;
jjg@726 576 }
jjg@726 577 }
jjg@643 578 }
jjg@643 579
jjg@643 580 public boolean isFlagSet(DiagnosticFlag flag) {
jjg@643 581 return flags.contains(flag);
jjg@643 582 }
jjg@643 583
mcimadamore@168 584 public static class MultilineDiagnostic extends JCDiagnostic {
mcimadamore@168 585
mcimadamore@168 586 private final List<JCDiagnostic> subdiagnostics;
mcimadamore@168 587
mcimadamore@168 588 public MultilineDiagnostic(JCDiagnostic other, List<JCDiagnostic> subdiagnostics) {
mcimadamore@168 589 super(other.defaultFormatter,
mcimadamore@168 590 other.getType(),
jjg@612 591 other.getLintCategory(),
jjg@643 592 other.flags,
mcimadamore@168 593 other.getDiagnosticSource(),
mcimadamore@168 594 other.position,
mcimadamore@168 595 other.getCode(),
mcimadamore@168 596 other.getArgs());
mcimadamore@168 597 this.subdiagnostics = subdiagnostics;
mcimadamore@168 598 }
mcimadamore@168 599
mcimadamore@168 600 @Override
mcimadamore@168 601 public List<JCDiagnostic> getSubdiagnostics() {
mcimadamore@168 602 return subdiagnostics;
mcimadamore@168 603 }
mcimadamore@168 604
mcimadamore@168 605 @Override
mcimadamore@168 606 public boolean isMultiline() {
mcimadamore@168 607 return true;
mcimadamore@168 608 }
mcimadamore@168 609 }
duke@1 610 }

mercurial