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

Mon, 10 Jan 2011 15:08:31 -0800

author
jjg
date
Mon, 10 Jan 2011 15:08:31 -0800
changeset 816
7c537f4298fb
parent 798
4868a36f6fd8
child 1135
36553cb94345
permissions
-rw-r--r--

6396503: javac should not require assertions enabled
Reviewed-by: mcimadamore

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

mercurial