duke@1: /* jjg@1135: * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.util; duke@1: jjg@643: import java.util.EnumSet; duke@1: import java.util.Locale; duke@1: import java.util.Map; jjg@643: import java.util.Set; duke@1: duke@1: import javax.tools.Diagnostic; duke@1: import javax.tools.JavaFileObject; duke@1: mcimadamore@83: import com.sun.tools.javac.api.DiagnosticFormatter; jjg@612: import com.sun.tools.javac.code.Lint.LintCategory; ksrini@1138: import com.sun.tools.javac.parser.EndPosTable; duke@1: import com.sun.tools.javac.tree.JCTree; duke@1: duke@1: import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*; duke@1: duke@1: /** An abstraction of a diagnostic message generated by the compiler. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public class JCDiagnostic implements Diagnostic { duke@1: /** A factory for creating diagnostic objects. */ duke@1: public static class Factory { duke@1: /** The context key for the diagnostic factory. */ duke@1: protected static final Context.Key diagnosticFactoryKey = duke@1: new Context.Key(); duke@1: duke@1: /** Get the Factory instance for this context. */ duke@1: public static Factory instance(Context context) { duke@1: Factory instance = context.get(diagnosticFactoryKey); duke@1: if (instance == null) duke@1: instance = new Factory(context); duke@1: return instance; duke@1: } duke@1: mcimadamore@89: DiagnosticFormatter formatter; duke@1: final String prefix; jjg@726: final Set defaultErrorFlags; duke@1: duke@1: /** Create a new diagnostic factory. */ duke@1: protected Factory(Context context) { mcimadamore@136: this(JavacMessages.instance(context), "compiler"); duke@1: context.put(diagnosticFactoryKey, this); jjg@726: jjg@1135: final Options options = Options.instance(context); jjg@1135: initOptions(options); jjg@1135: options.addListener(new Runnable() { jjg@1135: public void run() { jjg@1135: initOptions(options); jjg@1135: } jjg@1135: }); jjg@1135: } jjg@1135: jjg@1135: private void initOptions(Options options) { jjg@726: if (options.isSet("onlySyntaxErrorsUnrecoverable")) jjg@726: defaultErrorFlags.add(DiagnosticFlag.RECOVERABLE); duke@1: } duke@1: duke@1: /** Create a new diagnostic factory. */ mcimadamore@136: public Factory(JavacMessages messages, String prefix) { duke@1: this.prefix = prefix; mcimadamore@89: this.formatter = new BasicDiagnosticFormatter(messages); jjg@726: defaultErrorFlags = EnumSet.of(DiagnosticFlag.MANDATORY); duke@1: } duke@1: duke@1: /** duke@1: * Create an error diagnostic. duke@1: * @param source The source of the compilation unit, if any, in which to report the error. duke@1: * @param pos The source position at which to report the error. duke@1: * @param key The key for the localized error message. duke@1: * @param args Fields of the error message. duke@1: */ duke@1: public JCDiagnostic error( duke@1: DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { jjg@726: return create(ERROR, null, defaultErrorFlags, source, pos, key, args); duke@1: } duke@1: duke@1: /** duke@1: * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options. duke@1: * @param source The source of the compilation unit, if any, in which to report the warning. duke@1: * @param pos The source position at which to report the warning. jjg@612: * @param key The key for the localized warning message. jjg@612: * @param args Fields of the warning message. duke@1: * @see MandatoryWarningHandler duke@1: */ duke@1: public JCDiagnostic mandatoryWarning( jjg@612: DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { jjg@643: return create(WARNING, null, EnumSet.of(DiagnosticFlag.MANDATORY), source, pos, key, args); jjg@612: } jjg@612: jjg@612: /** jjg@612: * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options. jjg@612: * @param lc The lint category for the diagnostic jjg@612: * @param source The source of the compilation unit, if any, in which to report the warning. jjg@612: * @param pos The source position at which to report the warning. jjg@612: * @param key The key for the localized warning message. jjg@612: * @param args Fields of the warning message. jjg@612: * @see MandatoryWarningHandler jjg@612: */ jjg@612: public JCDiagnostic mandatoryWarning( jjg@612: LintCategory lc, jjg@612: DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { jjg@643: return create(WARNING, lc, EnumSet.of(DiagnosticFlag.MANDATORY), source, pos, key, args); jjg@612: } jjg@612: jjg@612: /** jjg@612: * Create a warning diagnostic. jjg@612: * @param lc The lint category for the diagnostic jjg@612: * @param key The key for the localized error message. jjg@612: * @param args Fields of the warning message. jjg@612: * @see MandatoryWarningHandler jjg@612: */ jjg@612: public JCDiagnostic warning( jjg@612: LintCategory lc, String key, Object... args) { jjg@643: return create(WARNING, lc, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args); duke@1: } duke@1: duke@1: /** duke@1: * Create a warning diagnostic. duke@1: * @param source The source of the compilation unit, if any, in which to report the warning. duke@1: * @param pos The source position at which to report the warning. jjg@612: * @param key The key for the localized warning message. jjg@612: * @param args Fields of the warning message. duke@1: */ duke@1: public JCDiagnostic warning( duke@1: DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { jjg@643: return create(WARNING, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args); jjg@612: } jjg@612: jjg@612: /** jjg@612: * Create a warning diagnostic. jjg@612: * @param lc The lint category for the diagnostic jjg@612: * @param source The source of the compilation unit, if any, in which to report the warning. jjg@612: * @param pos The source position at which to report the warning. jjg@612: * @param key The key for the localized warning message. jjg@612: * @param args Fields of the warning message. jjg@612: * @see MandatoryWarningHandler jjg@612: */ jjg@612: public JCDiagnostic warning( jjg@612: LintCategory lc, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { jjg@643: return create(WARNING, lc, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args); duke@1: } duke@1: duke@1: /** duke@1: * Create a note diagnostic that will not be hidden by the -nowarn or -Xlint:none options. jjg@612: * @param key The key for the localized message. jjg@612: * @param args Fields of the message. duke@1: * @see MandatoryWarningHandler duke@1: */ duke@1: public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) { jjg@643: return create(NOTE, null, EnumSet.of(DiagnosticFlag.MANDATORY), source, null, key, args); duke@1: } duke@1: duke@1: /** duke@1: * Create a note diagnostic. duke@1: * @param key The key for the localized error message. jjg@612: * @param args Fields of the message. duke@1: */ duke@1: public JCDiagnostic note(String key, Object... args) { jjg@643: return create(NOTE, null, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args); duke@1: } duke@1: duke@1: /** duke@1: * Create a note diagnostic. duke@1: * @param source The source of the compilation unit, if any, in which to report the note. duke@1: * @param pos The source position at which to report the note. jjg@612: * @param key The key for the localized message. jjg@612: * @param args Fields of the message. duke@1: */ duke@1: public JCDiagnostic note( duke@1: DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { jjg@643: return create(NOTE, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args); duke@1: } duke@1: duke@1: /** duke@1: * Create a fragment diagnostic, for use as an argument in other diagnostics jjg@612: * @param key The key for the localized message. jjg@612: * @param args Fields of the message. duke@1: */ duke@1: public JCDiagnostic fragment(String key, Object... args) { jjg@643: return create(FRAGMENT, null, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args); jjg@612: } jjg@612: jjg@612: /** jjg@612: * Create a new diagnostic of the given kind, which is not mandatory and which has jjg@612: * no lint category. jjg@612: * @param kind The diagnostic kind jjg@612: * @param ls The lint category, if applicable, or null jjg@612: * @param source The source of the compilation unit, if any, in which to report the message. jjg@612: * @param pos The source position at which to report the message. jjg@612: * @param key The key for the localized message. jjg@612: * @param args Fields of the message. jjg@612: */ jjg@612: public JCDiagnostic create( jjg@612: DiagnosticType kind, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { jjg@643: return create(kind, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args); mcimadamore@302: } mcimadamore@302: mcimadamore@302: /** mcimadamore@302: * Create a new diagnostic of the given kind. mcimadamore@302: * @param kind The diagnostic kind jjg@612: * @param lc The lint category, if applicable, or null mcimadamore@302: * @param isMandatory is diagnostic mandatory? jjg@612: * @param source The source of the compilation unit, if any, in which to report the message. jjg@612: * @param pos The source position at which to report the message. jjg@612: * @param key The key for the localized message. jjg@612: * @param args Fields of the message. mcimadamore@302: */ mcimadamore@302: public JCDiagnostic create( jjg@643: DiagnosticType kind, LintCategory lc, Set flags, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { jjg@643: return new JCDiagnostic(formatter, kind, lc, flags, source, pos, qualify(kind, key), args); duke@1: } duke@1: duke@1: protected String qualify(DiagnosticType t, String key) { duke@1: return prefix + "." + t.key + "." + key; duke@1: } duke@1: } duke@1: duke@1: duke@1: duke@1: /** duke@1: * Create a fragment diagnostic, for use as an argument in other diagnostics duke@1: * @param key The key for the localized error message. duke@1: * @param args Fields of the error message. mcimadamore@89: * duke@1: */ mcimadamore@89: @Deprecated duke@1: public static JCDiagnostic fragment(String key, Object... args) { mcimadamore@89: return new JCDiagnostic(getFragmentFormatter(), duke@1: FRAGMENT, jjg@612: null, jjg@643: EnumSet.noneOf(DiagnosticFlag.class), duke@1: null, duke@1: null, duke@1: "compiler." + FRAGMENT.key + "." + key, duke@1: args); duke@1: } mcimadamore@89: //where mcimadamore@89: @Deprecated mcimadamore@89: public static DiagnosticFormatter getFragmentFormatter() { mcimadamore@89: if (fragmentFormatter == null) { mcimadamore@136: fragmentFormatter = new BasicDiagnosticFormatter(JavacMessages.getDefaultMessages()); mcimadamore@89: } mcimadamore@89: return fragmentFormatter; mcimadamore@89: } duke@1: duke@1: /** duke@1: * A DiagnosticType defines the type of the diagnostic. duke@1: **/ duke@1: public enum DiagnosticType { duke@1: /** A fragment of an enclosing diagnostic. */ duke@1: FRAGMENT("misc"), duke@1: /** A note: similar to, but less serious than, a warning. */ duke@1: NOTE("note"), duke@1: /** A warning. */ duke@1: WARNING("warn"), duke@1: /** An error. */ duke@1: ERROR("err"); duke@1: duke@1: final String key; duke@1: duke@1: /** Create a DiagnosticType. duke@1: * @param key A string used to create the resource key for the diagnostic. duke@1: */ duke@1: DiagnosticType(String key) { duke@1: this.key = key; duke@1: } duke@1: }; duke@1: duke@1: /** duke@1: * A DiagnosticPosition provides information about the positions in a file duke@1: * that gave rise to a diagnostic. It always defines a "preferred" position duke@1: * that most accurately defines the location of the diagnostic, it may also duke@1: * provide a related tree node that spans that location. duke@1: */ duke@1: public static interface DiagnosticPosition { duke@1: /** Gets the tree node, if any, to which the diagnostic applies. */ duke@1: JCTree getTree(); duke@1: /** If there is a tree node, get the start position of the tree node. duke@1: * Otherwise, just returns the same as getPreferredPosition(). */ duke@1: int getStartPosition(); duke@1: /** Get the position within the file that most accurately defines the duke@1: * location for the diagnostic. */ duke@1: int getPreferredPosition(); duke@1: /** If there is a tree node, and if endPositions are available, get duke@1: * the end position of the tree node. Otherwise, just returns the duke@1: * same as getPreferredPosition(). */ ksrini@1138: int getEndPosition(EndPosTable endPosTable); duke@1: } duke@1: duke@1: /** duke@1: * A DiagnosticPosition that simply identifies a position, but no related duke@1: * tree node, as the location for a diagnostic. Used for scanner and parser duke@1: * diagnostics. */ duke@1: public static class SimpleDiagnosticPosition implements DiagnosticPosition { duke@1: public SimpleDiagnosticPosition(int pos) { duke@1: this.pos = pos; duke@1: } duke@1: duke@1: public JCTree getTree() { duke@1: return null; duke@1: } duke@1: duke@1: public int getStartPosition() { duke@1: return pos; duke@1: } duke@1: duke@1: public int getPreferredPosition() { duke@1: return pos; duke@1: } duke@1: ksrini@1138: public int getEndPosition(EndPosTable endPosTable) { duke@1: return pos; duke@1: } duke@1: duke@1: private final int pos; duke@1: } duke@1: jjg@643: public enum DiagnosticFlag { jjg@643: MANDATORY, jjg@726: RESOLVE_ERROR, jjg@726: SYNTAX, jjg@726: RECOVERABLE jjg@643: } jjg@643: duke@1: private final DiagnosticType type; duke@1: private final DiagnosticSource source; duke@1: private final DiagnosticPosition position; duke@1: private final int line; duke@1: private final int column; duke@1: private final String key; jjg@612: protected final Object[] args; jjg@643: private final Set flags; jjg@612: private final LintCategory lintCategory; duke@1: duke@1: /** duke@1: * Create a diagnostic object. jjg@612: * @param fomatter the formatter to use for the diagnostic duke@1: * @param dt the type of diagnostic jjg@612: * @param lc the lint category for the diagnostic jjg@612: * @param source the name of the source file, or null if none. duke@1: * @param pos the character offset within the source file, if given. duke@1: * @param key a resource key to identify the text of the diagnostic duke@1: * @param args arguments to be included in the text of the diagnostic duke@1: */ mcimadamore@89: protected JCDiagnostic(DiagnosticFormatter formatter, duke@1: DiagnosticType dt, jjg@612: LintCategory lc, jjg@643: Set flags, duke@1: DiagnosticSource source, duke@1: DiagnosticPosition pos, duke@1: String key, jjg@612: Object... args) { duke@1: if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS) duke@1: throw new IllegalArgumentException(); duke@1: mcimadamore@89: this.defaultFormatter = formatter; duke@1: this.type = dt; jjg@612: this.lintCategory = lc; jjg@643: this.flags = flags; duke@1: this.source = source; duke@1: this.position = pos; duke@1: this.key = key; mcimadamore@168: this.args = args; duke@1: duke@1: int n = (pos == null ? Position.NOPOS : pos.getPreferredPosition()); duke@1: if (n == Position.NOPOS || source == null) duke@1: line = column = -1; duke@1: else { duke@1: line = source.getLineNumber(n); mcimadamore@100: column = source.getColumnNumber(n, true); duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Get the type of this diagnostic. duke@1: * @return the type of this diagnostic duke@1: */ duke@1: public DiagnosticType getType() { duke@1: return type; duke@1: } duke@1: duke@1: /** mcimadamore@168: * Get the subdiagnostic list mcimadamore@168: * @return subdiagnostic list mcimadamore@168: */ mcimadamore@168: public List getSubdiagnostics() { mcimadamore@168: return List.nil(); mcimadamore@168: } mcimadamore@168: mcimadamore@168: public boolean isMultiline() { mcimadamore@168: return false; mcimadamore@168: } mcimadamore@168: mcimadamore@168: /** duke@1: * Check whether or not this diagnostic is required to be shown. duke@1: * @return true if this diagnostic is required to be shown. duke@1: */ duke@1: public boolean isMandatory() { jjg@643: return flags.contains(DiagnosticFlag.MANDATORY); duke@1: } duke@1: duke@1: /** jjg@612: * Check whether this diagnostic has an associated lint category. jjg@612: */ jjg@612: public boolean hasLintCategory() { jjg@612: return (lintCategory != null); jjg@612: } jjg@612: jjg@612: /** jjg@612: * Get the associated lint category, or null if none. jjg@612: */ jjg@612: public LintCategory getLintCategory() { jjg@612: return lintCategory; jjg@612: } jjg@612: jjg@612: /** duke@1: * Get the name of the source file referred to by this diagnostic. duke@1: * @return the name of the source referred to with this diagnostic, or null if none duke@1: */ duke@1: public JavaFileObject getSource() { duke@1: if (source == null) duke@1: return null; duke@1: else duke@1: return source.getFile(); duke@1: } duke@1: duke@1: /** duke@1: * Get the source referred to by this diagnostic. duke@1: * @return the source referred to with this diagnostic, or null if none duke@1: */ duke@1: public DiagnosticSource getDiagnosticSource() { duke@1: return source; duke@1: } duke@1: duke@1: protected int getIntStartPosition() { duke@1: return (position == null ? Position.NOPOS : position.getStartPosition()); duke@1: } duke@1: duke@1: protected int getIntPosition() { duke@1: return (position == null ? Position.NOPOS : position.getPreferredPosition()); duke@1: } duke@1: duke@1: protected int getIntEndPosition() { duke@1: return (position == null ? Position.NOPOS : position.getEndPosition(source.getEndPosTable())); duke@1: } duke@1: duke@1: public long getStartPosition() { duke@1: return getIntStartPosition(); duke@1: } duke@1: duke@1: public long getPosition() { duke@1: return getIntPosition(); duke@1: } duke@1: duke@1: public long getEndPosition() { duke@1: return getIntEndPosition(); duke@1: } duke@1: duke@1: /** duke@1: * Get the line number within the source referred to by this diagnostic. duke@1: * @return the line number within the source referred to by this diagnostic duke@1: */ duke@1: public long getLineNumber() { duke@1: return line; duke@1: } duke@1: duke@1: /** duke@1: * Get the column number within the line of source referred to by this diagnostic. duke@1: * @return the column number within the line of source referred to by this diagnostic duke@1: */ duke@1: public long getColumnNumber() { duke@1: return column; duke@1: } duke@1: duke@1: /** duke@1: * Get the arguments to be included in the text of the diagnostic. duke@1: * @return the arguments to be included in the text of the diagnostic duke@1: */ duke@1: public Object[] getArgs() { duke@1: return args; duke@1: } duke@1: duke@1: /** duke@1: * Get the prefix string associated with this type of diagnostic. duke@1: * @return the prefix string associated with this type of diagnostic duke@1: */ duke@1: public String getPrefix() { duke@1: return getPrefix(type); duke@1: } duke@1: duke@1: /** duke@1: * Get the prefix string associated with a particular type of diagnostic. duke@1: * @return the prefix string associated with a particular type of diagnostic duke@1: */ duke@1: public String getPrefix(DiagnosticType dt) { mcimadamore@89: return defaultFormatter.formatKind(this, Locale.getDefault()); mcimadamore@83: } mcimadamore@83: duke@1: /** duke@1: * Return the standard presentation of this diagnostic. duke@1: */ jjg@415: @Override duke@1: public String toString() { mcimadamore@89: return defaultFormatter.format(this,Locale.getDefault()); duke@1: } duke@1: mcimadamore@89: private DiagnosticFormatter defaultFormatter; mcimadamore@89: @Deprecated mcimadamore@89: private static DiagnosticFormatter fragmentFormatter; duke@1: duke@1: // Methods for javax.tools.Diagnostic duke@1: duke@1: public Diagnostic.Kind getKind() { duke@1: switch (type) { duke@1: case NOTE: duke@1: return Diagnostic.Kind.NOTE; duke@1: case WARNING: jjg@643: return flags.contains(DiagnosticFlag.MANDATORY) jjg@643: ? Diagnostic.Kind.MANDATORY_WARNING jjg@643: : Diagnostic.Kind.WARNING; duke@1: case ERROR: duke@1: return Diagnostic.Kind.ERROR; duke@1: default: duke@1: return Diagnostic.Kind.OTHER; duke@1: } duke@1: } duke@1: duke@1: public String getCode() { duke@1: return key; duke@1: } duke@1: duke@1: public String getMessage(Locale locale) { mcimadamore@89: return defaultFormatter.formatMessage(this, locale); duke@1: } mcimadamore@168: jjg@643: public void setFlag(DiagnosticFlag flag) { jjg@643: flags.add(flag); jjg@726: jjg@726: if (type == DiagnosticType.ERROR) { jjg@726: switch (flag) { jjg@726: case SYNTAX: jjg@726: flags.remove(DiagnosticFlag.RECOVERABLE); jjg@726: break; jjg@726: case RESOLVE_ERROR: jjg@726: flags.add(DiagnosticFlag.RECOVERABLE); jjg@726: break; jjg@726: } jjg@726: } jjg@643: } jjg@643: jjg@643: public boolean isFlagSet(DiagnosticFlag flag) { jjg@643: return flags.contains(flag); jjg@643: } jjg@643: mcimadamore@168: public static class MultilineDiagnostic extends JCDiagnostic { mcimadamore@168: mcimadamore@168: private final List subdiagnostics; mcimadamore@168: mcimadamore@168: public MultilineDiagnostic(JCDiagnostic other, List subdiagnostics) { mcimadamore@168: super(other.defaultFormatter, mcimadamore@168: other.getType(), jjg@612: other.getLintCategory(), jjg@643: other.flags, mcimadamore@168: other.getDiagnosticSource(), mcimadamore@168: other.position, mcimadamore@168: other.getCode(), mcimadamore@168: other.getArgs()); mcimadamore@168: this.subdiagnostics = subdiagnostics; mcimadamore@168: } mcimadamore@168: mcimadamore@168: @Override mcimadamore@168: public List getSubdiagnostics() { mcimadamore@168: return subdiagnostics; mcimadamore@168: } mcimadamore@168: mcimadamore@168: @Override mcimadamore@168: public boolean isMultiline() { mcimadamore@168: return true; mcimadamore@168: } mcimadamore@168: } duke@1: }