duke@1: /* xdono@54: * Copyright 2003-2008 Sun Microsystems, Inc. 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 duke@1: * published by the Free Software Foundation. Sun designates this duke@1: * particular file as subject to the "Classpath" exception as provided duke@1: * by Sun 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: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.util; duke@1: duke@1: import java.util.Locale; duke@1: import java.util.Map; 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@50: import com.sun.tools.javac.file.JavacFileManager; 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: * duke@1: *

This is NOT part of any API supported by Sun Microsystems. If duke@1: * 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; duke@1: duke@1: /** Create a new diagnostic factory. */ duke@1: protected Factory(Context context) { mcimadamore@89: this(Messages.instance(context), "compiler"); duke@1: context.put(diagnosticFactoryKey, this); duke@1: } duke@1: duke@1: /** Create a new diagnostic factory. */ duke@1: public Factory(Messages messages, String prefix) { duke@1: this.prefix = prefix; mcimadamore@89: this.formatter = new BasicDiagnosticFormatter(messages); 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) { mcimadamore@89: return new JCDiagnostic(formatter, ERROR, true, source, pos, qualify(ERROR, 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. duke@1: * @param key The key for the localized error message. duke@1: * @param args Fields of the error message. duke@1: * @see MandatoryWarningHandler duke@1: */ duke@1: public JCDiagnostic mandatoryWarning( duke@1: DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { mcimadamore@89: return new JCDiagnostic(formatter, WARNING, true, source, pos, qualify(WARNING, 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. 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 warning( duke@1: DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { mcimadamore@89: return new JCDiagnostic(formatter, WARNING, false, source, pos, qualify(WARNING, 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. duke@1: * @param key The key for the localized error message. duke@1: * @param args Fields of the error message. duke@1: * @see MandatoryWarningHandler duke@1: */ duke@1: public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) { mcimadamore@89: return new JCDiagnostic(formatter, NOTE, true, source, null, qualify(NOTE, 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. duke@1: * @param args Fields of the error message. duke@1: */ duke@1: public JCDiagnostic note(String key, Object... args) { duke@1: return note(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. 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 note( duke@1: DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { mcimadamore@89: return new JCDiagnostic(formatter, NOTE, false, source, pos, qualify(NOTE, key), args); 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. duke@1: */ duke@1: public JCDiagnostic fragment(String key, Object... args) { mcimadamore@89: return new JCDiagnostic(formatter, FRAGMENT, false, null, null, qualify(FRAGMENT, 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, duke@1: false, 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@89: fragmentFormatter = new BasicDiagnosticFormatter(Messages.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(). */ duke@1: int getEndPosition(Map 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: duke@1: public int getEndPosition(Map endPosTable) { duke@1: return pos; duke@1: } duke@1: duke@1: private final int pos; duke@1: } duke@1: 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; mcimadamore@83: protected Object[] args; duke@1: private boolean mandatory; duke@1: duke@1: /** duke@1: * Create a diagnostic object. duke@1: * @param messages the resource for localized messages duke@1: * @param dt the type of diagnostic duke@1: * @param name 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, duke@1: boolean mandatory, duke@1: DiagnosticSource source, duke@1: DiagnosticPosition pos, duke@1: String key, duke@1: 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; duke@1: this.mandatory = mandatory; duke@1: this.source = source; duke@1: this.position = pos; duke@1: this.key = key; duke@1: 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: /** 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() { duke@1: return mandatory; duke@1: } duke@1: duke@1: /** 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 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 String getSourceName() { duke@1: JavaFileObject s = getSource(); duke@1: return s == null ? null : JavacFileManager.getJavacFileName(s); 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: */ 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: duke@1: return mandatory ? Diagnostic.Kind.MANDATORY_WARNING duke@1: : 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) { duke@1: // RFE 6406133: JCDiagnostic.getMessage ignores locale argument mcimadamore@89: return defaultFormatter.formatMessage(this, locale); duke@1: } duke@1: }