jjg@73: /*
jjg@1358: * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
jjg@73: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@73: *
jjg@73: * This code is free software; you can redistribute it and/or modify it
jjg@73: * under the terms of the GNU General Public License version 2 only, as
ohair@554: * published by the Free Software Foundation. Oracle designates this
jjg@73: * particular file as subject to the "Classpath" exception as provided
ohair@554: * by Oracle in the LICENSE file that accompanied this code.
jjg@73: *
jjg@73: * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@73: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@73: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@73: * version 2 for more details (a copy is included in the LICENSE file that
jjg@73: * accompanied this code).
jjg@73: *
jjg@73: * You should have received a copy of the GNU General Public License version
jjg@73: * 2 along with this work; if not, write to the Free Software Foundation,
jjg@73: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@73: *
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.
jjg@73: */
jjg@73:
jjg@73: package com.sun.tools.javac.util;
jjg@73:
jjg@73: import java.util.HashMap;
jjg@73: import java.util.Map;
jjg@73: import javax.tools.JavaFileObject;
jjg@73:
jjg@612: import com.sun.tools.javac.code.Lint.LintCategory;
jjg@726: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
jjg@73: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
jjg@73: import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
jjg@73:
jjg@73:
jjg@73: /**
jjg@73: * A base class for error logs. Reports errors and warnings, and
jjg@73: * keeps track of error numbers and positions.
jjg@73: *
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.
jjg@73: * This code and its internal interfaces are subject to change or
jjg@73: * deletion without notice.
jjg@73: */
jjg@73: public abstract class AbstractLog {
jjg@73: AbstractLog(JCDiagnostic.Factory diags) {
jjg@73: this.diags = diags;
jjg@73: sourceMap = new HashMap();
jjg@73: }
jjg@73:
jjg@73: /** Re-assign source, returning previous setting.
jjg@73: */
jjg@73: public JavaFileObject useSource(JavaFileObject file) {
jjg@73: JavaFileObject prev = (source == null ? null : source.getFile());
jjg@73: source = getSource(file);
jjg@73: return prev;
jjg@73: }
jjg@73:
jjg@73: protected DiagnosticSource getSource(JavaFileObject file) {
jjg@73: if (file == null)
mcimadamore@303: return DiagnosticSource.NO_SOURCE;
jjg@73: DiagnosticSource s = sourceMap.get(file);
jjg@73: if (s == null) {
jjg@73: s = new DiagnosticSource(file, this);
jjg@73: sourceMap.put(file, s);
jjg@73: }
jjg@73: return s;
jjg@73: }
jjg@73:
mcimadamore@168: /** Return the underlying diagnostic source
mcimadamore@168: */
mcimadamore@168: public DiagnosticSource currentSource() {
mcimadamore@168: return source;
mcimadamore@168: }
mcimadamore@168:
jjg@73: /** Report an error, unless another error was already reported at same
jjg@73: * source position.
jjg@73: * @param key The key for the localized error message.
jjg@73: * @param args Fields of the error message.
jjg@73: */
jjg@73: public void error(String key, Object ... args) {
jjg@73: report(diags.error(source, null, key, args));
jjg@73: }
jjg@73:
jjg@73: /** Report an error, unless another error was already reported at same
jjg@73: * source position.
jjg@73: * @param pos The source position at which to report the error.
jjg@73: * @param key The key for the localized error message.
jjg@73: * @param args Fields of the error message.
jjg@73: */
jjg@73: public void error(DiagnosticPosition pos, String key, Object ... args) {
jjg@73: report(diags.error(source, pos, key, args));
jjg@73: }
jjg@73:
jjg@73: /** Report an error, unless another error was already reported at same
jjg@73: * source position.
ksrini@1074: * @param flag A flag to set on the diagnostic
ksrini@1074: * @param pos The source position at which to report the error.
ksrini@1074: * @param key The key for the localized error message.
ksrini@1074: * @param args Fields of the error message.
ksrini@1074: */
ksrini@1074: public void error(DiagnosticFlag flag, DiagnosticPosition pos, String key, Object ... args) {
ksrini@1074: JCDiagnostic d = diags.error(source, pos, key, args);
ksrini@1074: d.setFlag(flag);
ksrini@1074: report(d);
ksrini@1074: }
ksrini@1074:
ksrini@1074: /** Report an error, unless another error was already reported at same
ksrini@1074: * source position.
jjg@73: * @param pos The source position at which to report the error.
jjg@73: * @param key The key for the localized error message.
jjg@73: * @param args Fields of the error message.
jjg@73: */
jjg@73: public void error(int pos, String key, Object ... args) {
jjg@73: report(diags.error(source, wrap(pos), key, args));
jjg@73: }
jjg@73:
jjg@726: /** Report an error, unless another error was already reported at same
jjg@726: * source position.
jjg@726: * @param flag A flag to set on the diagnostic
jjg@726: * @param pos The source position at which to report the error.
jjg@726: * @param key The key for the localized error message.
jjg@726: * @param args Fields of the error message.
jjg@726: */
jjg@726: public void error(DiagnosticFlag flag, int pos, String key, Object ... args) {
jjg@726: JCDiagnostic d = diags.error(source, wrap(pos), key, args);
jjg@726: d.setFlag(flag);
jjg@726: report(d);
jjg@726: }
jjg@726:
jjg@73: /** Report a warning, unless suppressed by the -nowarn option or the
jjg@73: * maximum number of warnings has been reached.
jjg@73: * @param key The key for the localized warning message.
jjg@73: * @param args Fields of the warning message.
jjg@73: */
jjg@73: public void warning(String key, Object ... args) {
jjg@73: report(diags.warning(source, null, key, args));
jjg@73: }
jjg@73:
jjg@612: /** Report a lint warning, unless suppressed by the -nowarn option or the
jjg@612: * maximum number of warnings has been reached.
jjg@612: * @param lc The lint category for the diagnostic
jjg@612: * @param key The key for the localized warning message.
jjg@612: * @param args Fields of the warning message.
jjg@612: */
jjg@612: public void warning(LintCategory lc, String key, Object ... args) {
jjg@612: report(diags.warning(lc, key, args));
jjg@612: }
jjg@612:
jjg@73: /** Report a warning, unless suppressed by the -nowarn option or the
jjg@73: * maximum number of warnings has been reached.
jjg@73: * @param pos The source position at which to report the warning.
jjg@73: * @param key The key for the localized warning message.
jjg@73: * @param args Fields of the warning message.
jjg@73: */
jjg@73: public void warning(DiagnosticPosition pos, String key, Object ... args) {
jjg@73: report(diags.warning(source, pos, key, args));
jjg@73: }
jjg@73:
jjg@612: /** Report a lint warning, unless suppressed by the -nowarn option or the
jjg@612: * maximum number of warnings has been reached.
jjg@612: * @param lc The lint category for the diagnostic
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: */
jjg@612: public void warning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) {
jjg@612: report(diags.warning(lc, source, pos, key, args));
jjg@612: }
jjg@612:
jjg@73: /** Report a warning, unless suppressed by the -nowarn option or the
jjg@73: * maximum number of warnings has been reached.
jjg@73: * @param pos The source position at which to report the warning.
jjg@73: * @param key The key for the localized warning message.
jjg@73: * @param args Fields of the warning message.
jjg@73: */
jjg@73: public void warning(int pos, String key, Object ... args) {
jjg@73: report(diags.warning(source, wrap(pos), key, args));
jjg@73: }
jjg@73:
jjg@73: /** Report a warning.
jjg@73: * @param pos The source position at which to report the warning.
jjg@73: * @param key The key for the localized warning message.
jjg@73: * @param args Fields of the warning message.
jjg@73: */
jjg@73: public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) {
jjg@73: report(diags.mandatoryWarning(source, pos, key, args));
jjg@73: }
jjg@73:
jjg@612: /** Report a warning.
jjg@612: * @param lc The lint category for the diagnostic
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: */
jjg@612: public void mandatoryWarning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) {
jjg@612: report(diags.mandatoryWarning(lc, source, pos, key, args));
jjg@612: }
jjg@612:
jjg@73: /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73: * @param key The key for the localized notification message.
jjg@73: * @param args Fields of the notint an error or warning message:
jjg@73: */
jjg@73: public void note(String key, Object ... args) {
jjg@73: report(diags.note(source, null, key, args));
jjg@73: }
jjg@73:
jjg@73: /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73: * @param key The key for the localized notification message.
jjg@73: * @param args Fields of the notification message.
jjg@73: */
jjg@73: public void note(DiagnosticPosition pos, String key, Object ... args) {
jjg@73: report(diags.note(source, pos, key, args));
jjg@73: }
jjg@73:
jjg@73: /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73: * @param key The key for the localized notification message.
jjg@73: * @param args Fields of the notification message.
jjg@73: */
jjg@73: public void note(int pos, String key, Object ... args) {
jjg@73: report(diags.note(source, wrap(pos), key, args));
jjg@73: }
jjg@73:
jjg@73: /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73: * @param key The key for the localized notification message.
jjg@73: * @param args Fields of the notification message.
jjg@73: */
jjg@73: public void note(JavaFileObject file, String key, Object ... args) {
jjg@73: report(diags.note(getSource(file), null, key, args));
jjg@73: }
jjg@73:
jjg@73: /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73: * @param key The key for the localized notification message.
jjg@73: * @param args Fields of the notification message.
jjg@73: */
jjg@73: public void mandatoryNote(final JavaFileObject file, String key, Object ... args) {
jjg@73: report(diags.mandatoryNote(getSource(file), key, args));
jjg@73: }
jjg@73:
jjg@73: protected abstract void report(JCDiagnostic diagnostic);
jjg@73:
jjg@73: protected abstract void directError(String key, Object... args);
jjg@73:
jjg@73: private DiagnosticPosition wrap(int pos) {
jjg@73: return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos));
jjg@73: }
jjg@73:
jjg@73: /** Factory for diagnostics
jjg@73: */
jjg@73: protected JCDiagnostic.Factory diags;
jjg@73:
jjg@73: /** The file that's currently being translated.
jjg@73: */
jjg@73: protected DiagnosticSource source;
jjg@73:
jjg@73: /** A cache of lightweight DiagnosticSource objects.
jjg@73: */
jjg@73: protected Map sourceMap;
jjg@73: }