aoqi@0: /*
aoqi@0: * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.tools.javac.util;
aoqi@0:
aoqi@0: import java.util.EnumSet;
aoqi@0: import java.util.Locale;
aoqi@0: import java.util.Set;
aoqi@0:
aoqi@0: import javax.tools.Diagnostic;
aoqi@0: import javax.tools.JavaFileObject;
aoqi@0:
aoqi@0: import com.sun.tools.javac.api.DiagnosticFormatter;
aoqi@0: import com.sun.tools.javac.code.Lint.LintCategory;
aoqi@0: import com.sun.tools.javac.tree.EndPosTable;
aoqi@0: import com.sun.tools.javac.tree.JCTree;
aoqi@0:
aoqi@0: import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*;
aoqi@0:
aoqi@0: /** An abstraction of a diagnostic message generated by the compiler.
aoqi@0: *
aoqi@0: *
This is NOT part of any supported API.
aoqi@0: * If you write code that depends on this, you do so at your own risk.
aoqi@0: * This code and its internal interfaces are subject to change or
aoqi@0: * deletion without notice.
aoqi@0: */
aoqi@0: public class JCDiagnostic implements Diagnostic {
aoqi@0: /** A factory for creating diagnostic objects. */
aoqi@0: public static class Factory {
aoqi@0: /** The context key for the diagnostic factory. */
aoqi@0: protected static final Context.Key diagnosticFactoryKey =
aoqi@0: new Context.Key();
aoqi@0:
aoqi@0: /** Get the Factory instance for this context. */
aoqi@0: public static Factory instance(Context context) {
aoqi@0: Factory instance = context.get(diagnosticFactoryKey);
aoqi@0: if (instance == null)
aoqi@0: instance = new Factory(context);
aoqi@0: return instance;
aoqi@0: }
aoqi@0:
aoqi@0: DiagnosticFormatter formatter;
aoqi@0: final String prefix;
aoqi@0: final Set defaultErrorFlags;
aoqi@0:
aoqi@0: /** Create a new diagnostic factory. */
aoqi@0: protected Factory(Context context) {
aoqi@0: this(JavacMessages.instance(context), "compiler");
aoqi@0: context.put(diagnosticFactoryKey, this);
aoqi@0:
aoqi@0: final Options options = Options.instance(context);
aoqi@0: initOptions(options);
aoqi@0: options.addListener(new Runnable() {
aoqi@0: public void run() {
aoqi@0: initOptions(options);
aoqi@0: }
aoqi@0: });
aoqi@0: }
aoqi@0:
aoqi@0: private void initOptions(Options options) {
aoqi@0: if (options.isSet("onlySyntaxErrorsUnrecoverable"))
aoqi@0: defaultErrorFlags.add(DiagnosticFlag.RECOVERABLE);
aoqi@0: }
aoqi@0:
aoqi@0: /** Create a new diagnostic factory. */
aoqi@0: public Factory(JavacMessages messages, String prefix) {
aoqi@0: this.prefix = prefix;
aoqi@0: this.formatter = new BasicDiagnosticFormatter(messages);
aoqi@0: defaultErrorFlags = EnumSet.of(DiagnosticFlag.MANDATORY);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create an error diagnostic.
aoqi@0: * @param source The source of the compilation unit, if any, in which to report the error.
aoqi@0: * @param pos The source position at which to report the error.
aoqi@0: * @param key The key for the localized error message.
aoqi@0: * @param args Fields of the error message.
aoqi@0: */
aoqi@0: public JCDiagnostic error(
aoqi@0: DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
aoqi@0: return create(ERROR, null, defaultErrorFlags, source, pos, key, args);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
aoqi@0: * @param source The source of the compilation unit, if any, in which to report the warning.
aoqi@0: * @param pos The source position at which to report the warning.
aoqi@0: * @param key The key for the localized warning message.
aoqi@0: * @param args Fields of the warning message.
aoqi@0: * @see MandatoryWarningHandler
aoqi@0: */
aoqi@0: public JCDiagnostic mandatoryWarning(
aoqi@0: DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
aoqi@0: return create(WARNING, null, EnumSet.of(DiagnosticFlag.MANDATORY), source, pos, key, args);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
aoqi@0: * @param lc The lint category for the diagnostic
aoqi@0: * @param source The source of the compilation unit, if any, in which to report the warning.
aoqi@0: * @param pos The source position at which to report the warning.
aoqi@0: * @param key The key for the localized warning message.
aoqi@0: * @param args Fields of the warning message.
aoqi@0: * @see MandatoryWarningHandler
aoqi@0: */
aoqi@0: public JCDiagnostic mandatoryWarning(
aoqi@0: LintCategory lc,
aoqi@0: DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
aoqi@0: return create(WARNING, lc, EnumSet.of(DiagnosticFlag.MANDATORY), source, pos, key, args);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a warning diagnostic.
aoqi@0: * @param lc The lint category for the diagnostic
aoqi@0: * @param key The key for the localized error message.
aoqi@0: * @param args Fields of the warning message.
aoqi@0: * @see MandatoryWarningHandler
aoqi@0: */
aoqi@0: public JCDiagnostic warning(
aoqi@0: LintCategory lc, String key, Object... args) {
aoqi@0: return create(WARNING, lc, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a warning diagnostic.
aoqi@0: * @param source The source of the compilation unit, if any, in which to report the warning.
aoqi@0: * @param pos The source position at which to report the warning.
aoqi@0: * @param key The key for the localized warning message.
aoqi@0: * @param args Fields of the warning message.
aoqi@0: */
aoqi@0: public JCDiagnostic warning(
aoqi@0: DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
aoqi@0: return create(WARNING, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a warning diagnostic.
aoqi@0: * @param lc The lint category for the diagnostic
aoqi@0: * @param source The source of the compilation unit, if any, in which to report the warning.
aoqi@0: * @param pos The source position at which to report the warning.
aoqi@0: * @param key The key for the localized warning message.
aoqi@0: * @param args Fields of the warning message.
aoqi@0: * @see MandatoryWarningHandler
aoqi@0: */
aoqi@0: public JCDiagnostic warning(
aoqi@0: LintCategory lc, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
aoqi@0: return create(WARNING, lc, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a note diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
aoqi@0: * @param key The key for the localized message.
aoqi@0: * @param args Fields of the message.
aoqi@0: * @see MandatoryWarningHandler
aoqi@0: */
aoqi@0: public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) {
aoqi@0: return create(NOTE, null, EnumSet.of(DiagnosticFlag.MANDATORY), source, null, key, args);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a note diagnostic.
aoqi@0: * @param key The key for the localized error message.
aoqi@0: * @param args Fields of the message.
aoqi@0: */
aoqi@0: public JCDiagnostic note(String key, Object... args) {
aoqi@0: return create(NOTE, null, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a note diagnostic.
aoqi@0: * @param source The source of the compilation unit, if any, in which to report the note.
aoqi@0: * @param pos The source position at which to report the note.
aoqi@0: * @param key The key for the localized message.
aoqi@0: * @param args Fields of the message.
aoqi@0: */
aoqi@0: public JCDiagnostic note(
aoqi@0: DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
aoqi@0: return create(NOTE, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a fragment diagnostic, for use as an argument in other diagnostics
aoqi@0: * @param key The key for the localized message.
aoqi@0: * @param args Fields of the message.
aoqi@0: */
aoqi@0: public JCDiagnostic fragment(String key, Object... args) {
aoqi@0: return create(FRAGMENT, null, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a new diagnostic of the given kind, which is not mandatory and which has
aoqi@0: * no lint category.
aoqi@0: * @param kind The diagnostic kind
aoqi@0: * @param source The source of the compilation unit, if any, in which to report the message.
aoqi@0: * @param pos The source position at which to report the message.
aoqi@0: * @param key The key for the localized message.
aoqi@0: * @param args Fields of the message.
aoqi@0: */
aoqi@0: public JCDiagnostic create(
aoqi@0: DiagnosticType kind, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
aoqi@0: return create(kind, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a new diagnostic of the given kind.
aoqi@0: * @param kind The diagnostic kind
aoqi@0: * @param lc The lint category, if applicable, or null
aoqi@0: * @param flags The set of flags for the diagnostic
aoqi@0: * @param source The source of the compilation unit, if any, in which to report the message.
aoqi@0: * @param pos The source position at which to report the message.
aoqi@0: * @param key The key for the localized message.
aoqi@0: * @param args Fields of the message.
aoqi@0: */
aoqi@0: public JCDiagnostic create(
aoqi@0: DiagnosticType kind, LintCategory lc, Set flags, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
aoqi@0: return new JCDiagnostic(formatter, kind, lc, flags, source, pos, qualify(kind, key), args);
aoqi@0: }
aoqi@0:
aoqi@0: protected String qualify(DiagnosticType t, String key) {
aoqi@0: return prefix + "." + t.key + "." + key;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a fragment diagnostic, for use as an argument in other diagnostics
aoqi@0: * @param key The key for the localized error message.
aoqi@0: * @param args Fields of the error message.
aoqi@0: *
aoqi@0: */
aoqi@0: @Deprecated
aoqi@0: public static JCDiagnostic fragment(String key, Object... args) {
aoqi@0: return new JCDiagnostic(getFragmentFormatter(),
aoqi@0: FRAGMENT,
aoqi@0: null,
aoqi@0: EnumSet.noneOf(DiagnosticFlag.class),
aoqi@0: null,
aoqi@0: null,
aoqi@0: "compiler." + FRAGMENT.key + "." + key,
aoqi@0: args);
aoqi@0: }
aoqi@0: //where
aoqi@0: @Deprecated
aoqi@0: public static DiagnosticFormatter getFragmentFormatter() {
aoqi@0: if (fragmentFormatter == null) {
aoqi@0: fragmentFormatter = new BasicDiagnosticFormatter(JavacMessages.getDefaultMessages());
aoqi@0: }
aoqi@0: return fragmentFormatter;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * A DiagnosticType defines the type of the diagnostic.
aoqi@0: **/
aoqi@0: public enum DiagnosticType {
aoqi@0: /** A fragment of an enclosing diagnostic. */
aoqi@0: FRAGMENT("misc"),
aoqi@0: /** A note: similar to, but less serious than, a warning. */
aoqi@0: NOTE("note"),
aoqi@0: /** A warning. */
aoqi@0: WARNING("warn"),
aoqi@0: /** An error. */
aoqi@0: ERROR("err");
aoqi@0:
aoqi@0: final String key;
aoqi@0:
aoqi@0: /** Create a DiagnosticType.
aoqi@0: * @param key A string used to create the resource key for the diagnostic.
aoqi@0: */
aoqi@0: DiagnosticType(String key) {
aoqi@0: this.key = key;
aoqi@0: }
aoqi@0: };
aoqi@0:
aoqi@0: /**
aoqi@0: * A DiagnosticPosition provides information about the positions in a file
aoqi@0: * that gave rise to a diagnostic. It always defines a "preferred" position
aoqi@0: * that most accurately defines the location of the diagnostic, it may also
aoqi@0: * provide a related tree node that spans that location.
aoqi@0: */
aoqi@0: public static interface DiagnosticPosition {
aoqi@0: /** Gets the tree node, if any, to which the diagnostic applies. */
aoqi@0: JCTree getTree();
aoqi@0: /** If there is a tree node, get the start position of the tree node.
aoqi@0: * Otherwise, just returns the same as getPreferredPosition(). */
aoqi@0: int getStartPosition();
aoqi@0: /** Get the position within the file that most accurately defines the
aoqi@0: * location for the diagnostic. */
aoqi@0: int getPreferredPosition();
aoqi@0: /** If there is a tree node, and if endPositions are available, get
aoqi@0: * the end position of the tree node. Otherwise, just returns the
aoqi@0: * same as getPreferredPosition(). */
aoqi@0: int getEndPosition(EndPosTable endPosTable);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * A DiagnosticPosition that simply identifies a position, but no related
aoqi@0: * tree node, as the location for a diagnostic. Used for scanner and parser
aoqi@0: * diagnostics. */
aoqi@0: public static class SimpleDiagnosticPosition implements DiagnosticPosition {
aoqi@0: public SimpleDiagnosticPosition(int pos) {
aoqi@0: this.pos = pos;
aoqi@0: }
aoqi@0:
aoqi@0: public JCTree getTree() {
aoqi@0: return null;
aoqi@0: }
aoqi@0:
aoqi@0: public int getStartPosition() {
aoqi@0: return pos;
aoqi@0: }
aoqi@0:
aoqi@0: public int getPreferredPosition() {
aoqi@0: return pos;
aoqi@0: }
aoqi@0:
aoqi@0: public int getEndPosition(EndPosTable endPosTable) {
aoqi@0: return pos;
aoqi@0: }
aoqi@0:
aoqi@0: private final int pos;
aoqi@0: }
aoqi@0:
aoqi@0: public enum DiagnosticFlag {
aoqi@0: MANDATORY,
aoqi@0: RESOLVE_ERROR,
aoqi@0: SYNTAX,
aoqi@0: RECOVERABLE,
aoqi@0: NON_DEFERRABLE,
aoqi@0: COMPRESSED
aoqi@0: }
aoqi@0:
aoqi@0: private final DiagnosticType type;
aoqi@0: private final DiagnosticSource source;
aoqi@0: private final DiagnosticPosition position;
aoqi@0: private final String key;
aoqi@0: protected final Object[] args;
aoqi@0: private final Set flags;
aoqi@0: private final LintCategory lintCategory;
aoqi@0:
aoqi@0: /** source line position (set lazily) */
aoqi@0: private SourcePosition sourcePosition;
aoqi@0:
aoqi@0: /**
aoqi@0: * This class is used to defer the line/column position fetch logic after diagnostic construction.
aoqi@0: */
aoqi@0: class SourcePosition {
aoqi@0:
aoqi@0: private final int line;
aoqi@0: private final int column;
aoqi@0:
aoqi@0: SourcePosition() {
aoqi@0: int n = (position == null ? Position.NOPOS : position.getPreferredPosition());
aoqi@0: if (n == Position.NOPOS || source == null)
aoqi@0: line = column = -1;
aoqi@0: else {
aoqi@0: line = source.getLineNumber(n);
aoqi@0: column = source.getColumnNumber(n, true);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public int getLineNumber() {
aoqi@0: return line;
aoqi@0: }
aoqi@0:
aoqi@0: public int getColumnNumber() {
aoqi@0: return column;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a diagnostic object.
aoqi@0: * @param formatter the formatter to use for the diagnostic
aoqi@0: * @param dt the type of diagnostic
aoqi@0: * @param lc the lint category for the diagnostic
aoqi@0: * @param source the name of the source file, or null if none.
aoqi@0: * @param pos the character offset within the source file, if given.
aoqi@0: * @param key a resource key to identify the text of the diagnostic
aoqi@0: * @param args arguments to be included in the text of the diagnostic
aoqi@0: */
aoqi@0: protected JCDiagnostic(DiagnosticFormatter formatter,
aoqi@0: DiagnosticType dt,
aoqi@0: LintCategory lc,
aoqi@0: Set flags,
aoqi@0: DiagnosticSource source,
aoqi@0: DiagnosticPosition pos,
aoqi@0: String key,
aoqi@0: Object... args) {
aoqi@0: if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS)
aoqi@0: throw new IllegalArgumentException();
aoqi@0:
aoqi@0: this.defaultFormatter = formatter;
aoqi@0: this.type = dt;
aoqi@0: this.lintCategory = lc;
aoqi@0: this.flags = flags;
aoqi@0: this.source = source;
aoqi@0: this.position = pos;
aoqi@0: this.key = key;
aoqi@0: this.args = args;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the type of this diagnostic.
aoqi@0: * @return the type of this diagnostic
aoqi@0: */
aoqi@0: public DiagnosticType getType() {
aoqi@0: return type;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the subdiagnostic list
aoqi@0: * @return subdiagnostic list
aoqi@0: */
aoqi@0: public List getSubdiagnostics() {
aoqi@0: return List.nil();
aoqi@0: }
aoqi@0:
aoqi@0: public boolean isMultiline() {
aoqi@0: return false;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Check whether or not this diagnostic is required to be shown.
aoqi@0: * @return true if this diagnostic is required to be shown.
aoqi@0: */
aoqi@0: public boolean isMandatory() {
aoqi@0: return flags.contains(DiagnosticFlag.MANDATORY);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Check whether this diagnostic has an associated lint category.
aoqi@0: */
aoqi@0: public boolean hasLintCategory() {
aoqi@0: return (lintCategory != null);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the associated lint category, or null if none.
aoqi@0: */
aoqi@0: public LintCategory getLintCategory() {
aoqi@0: return lintCategory;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the name of the source file referred to by this diagnostic.
aoqi@0: * @return the name of the source referred to with this diagnostic, or null if none
aoqi@0: */
aoqi@0: public JavaFileObject getSource() {
aoqi@0: if (source == null)
aoqi@0: return null;
aoqi@0: else
aoqi@0: return source.getFile();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the source referred to by this diagnostic.
aoqi@0: * @return the source referred to with this diagnostic, or null if none
aoqi@0: */
aoqi@0: public DiagnosticSource getDiagnosticSource() {
aoqi@0: return source;
aoqi@0: }
aoqi@0:
aoqi@0: protected int getIntStartPosition() {
aoqi@0: return (position == null ? Position.NOPOS : position.getStartPosition());
aoqi@0: }
aoqi@0:
aoqi@0: protected int getIntPosition() {
aoqi@0: return (position == null ? Position.NOPOS : position.getPreferredPosition());
aoqi@0: }
aoqi@0:
aoqi@0: protected int getIntEndPosition() {
aoqi@0: return (position == null ? Position.NOPOS : position.getEndPosition(source.getEndPosTable()));
aoqi@0: }
aoqi@0:
aoqi@0: public long getStartPosition() {
aoqi@0: return getIntStartPosition();
aoqi@0: }
aoqi@0:
aoqi@0: public long getPosition() {
aoqi@0: return getIntPosition();
aoqi@0: }
aoqi@0:
aoqi@0: public long getEndPosition() {
aoqi@0: return getIntEndPosition();
aoqi@0: }
aoqi@0:
aoqi@0: public DiagnosticPosition getDiagnosticPosition() {
aoqi@0: return position;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the line number within the source referred to by this diagnostic.
aoqi@0: * @return the line number within the source referred to by this diagnostic
aoqi@0: */
aoqi@0: public long getLineNumber() {
aoqi@0: if (sourcePosition == null) {
aoqi@0: sourcePosition = new SourcePosition();
aoqi@0: }
aoqi@0: return sourcePosition.getLineNumber();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the column number within the line of source referred to by this diagnostic.
aoqi@0: * @return the column number within the line of source referred to by this diagnostic
aoqi@0: */
aoqi@0: public long getColumnNumber() {
aoqi@0: if (sourcePosition == null) {
aoqi@0: sourcePosition = new SourcePosition();
aoqi@0: }
aoqi@0: return sourcePosition.getColumnNumber();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the arguments to be included in the text of the diagnostic.
aoqi@0: * @return the arguments to be included in the text of the diagnostic
aoqi@0: */
aoqi@0: public Object[] getArgs() {
aoqi@0: return args;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the prefix string associated with this type of diagnostic.
aoqi@0: * @return the prefix string associated with this type of diagnostic
aoqi@0: */
aoqi@0: public String getPrefix() {
aoqi@0: return getPrefix(type);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Get the prefix string associated with a particular type of diagnostic.
aoqi@0: * @return the prefix string associated with a particular type of diagnostic
aoqi@0: */
aoqi@0: public String getPrefix(DiagnosticType dt) {
aoqi@0: return defaultFormatter.formatKind(this, Locale.getDefault());
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Return the standard presentation of this diagnostic.
aoqi@0: */
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: return defaultFormatter.format(this,Locale.getDefault());
aoqi@0: }
aoqi@0:
aoqi@0: private DiagnosticFormatter defaultFormatter;
aoqi@0: @Deprecated
aoqi@0: private static DiagnosticFormatter fragmentFormatter;
aoqi@0:
aoqi@0: // Methods for javax.tools.Diagnostic
aoqi@0:
aoqi@0: public Diagnostic.Kind getKind() {
aoqi@0: switch (type) {
aoqi@0: case NOTE:
aoqi@0: return Diagnostic.Kind.NOTE;
aoqi@0: case WARNING:
aoqi@0: return flags.contains(DiagnosticFlag.MANDATORY)
aoqi@0: ? Diagnostic.Kind.MANDATORY_WARNING
aoqi@0: : Diagnostic.Kind.WARNING;
aoqi@0: case ERROR:
aoqi@0: return Diagnostic.Kind.ERROR;
aoqi@0: default:
aoqi@0: return Diagnostic.Kind.OTHER;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public String getCode() {
aoqi@0: return key;
aoqi@0: }
aoqi@0:
aoqi@0: public String getMessage(Locale locale) {
aoqi@0: return defaultFormatter.formatMessage(this, locale);
aoqi@0: }
aoqi@0:
aoqi@0: public void setFlag(DiagnosticFlag flag) {
aoqi@0: flags.add(flag);
aoqi@0:
aoqi@0: if (type == DiagnosticType.ERROR) {
aoqi@0: switch (flag) {
aoqi@0: case SYNTAX:
aoqi@0: flags.remove(DiagnosticFlag.RECOVERABLE);
aoqi@0: break;
aoqi@0: case RESOLVE_ERROR:
aoqi@0: flags.add(DiagnosticFlag.RECOVERABLE);
aoqi@0: break;
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public boolean isFlagSet(DiagnosticFlag flag) {
aoqi@0: return flags.contains(flag);
aoqi@0: }
aoqi@0:
aoqi@0: public static class MultilineDiagnostic extends JCDiagnostic {
aoqi@0:
aoqi@0: private final List subdiagnostics;
aoqi@0:
aoqi@0: public MultilineDiagnostic(JCDiagnostic other, List subdiagnostics) {
aoqi@0: super(other.defaultFormatter,
aoqi@0: other.getType(),
aoqi@0: other.getLintCategory(),
aoqi@0: other.flags,
aoqi@0: other.getDiagnosticSource(),
aoqi@0: other.position,
aoqi@0: other.getCode(),
aoqi@0: other.getArgs());
aoqi@0: this.subdiagnostics = subdiagnostics;
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public List getSubdiagnostics() {
aoqi@0: return subdiagnostics;
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public boolean isMultiline() {
aoqi@0: return true;
aoqi@0: }
aoqi@0: }
aoqi@0: }