aoqi@0: /*
aoqi@0: * Copyright (c) 1999, 2012, 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.HashMap;
aoqi@0: import java.util.Map;
aoqi@0: import javax.tools.JavaFileObject;
aoqi@0:
aoqi@0: import com.sun.tools.javac.code.Lint.LintCategory;
aoqi@0: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
aoqi@0: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
aoqi@0: import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
aoqi@0:
aoqi@0:
aoqi@0: /**
aoqi@0: * A base class for error logs. Reports errors and warnings, and
aoqi@0: * keeps track of error numbers and positions.
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 abstract class AbstractLog {
aoqi@0: AbstractLog(JCDiagnostic.Factory diags) {
aoqi@0: this.diags = diags;
aoqi@0: sourceMap = new HashMap();
aoqi@0: }
aoqi@0:
aoqi@0: /** Re-assign source, returning previous setting.
aoqi@0: */
aoqi@0: public JavaFileObject useSource(JavaFileObject file) {
aoqi@0: JavaFileObject prev = (source == null ? null : source.getFile());
aoqi@0: source = getSource(file);
aoqi@0: return prev;
aoqi@0: }
aoqi@0:
aoqi@0: protected DiagnosticSource getSource(JavaFileObject file) {
aoqi@0: if (file == null)
aoqi@0: return DiagnosticSource.NO_SOURCE;
aoqi@0: DiagnosticSource s = sourceMap.get(file);
aoqi@0: if (s == null) {
aoqi@0: s = new DiagnosticSource(file, this);
aoqi@0: sourceMap.put(file, s);
aoqi@0: }
aoqi@0: return s;
aoqi@0: }
aoqi@0:
aoqi@0: /** Return the underlying diagnostic source
aoqi@0: */
aoqi@0: public DiagnosticSource currentSource() {
aoqi@0: return source;
aoqi@0: }
aoqi@0:
aoqi@0: /** Report an error, unless another error was already reported at same
aoqi@0: * source position.
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 void error(String key, Object ... args) {
aoqi@0: report(diags.error(source, null, key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Report an error, unless another error was already reported at same
aoqi@0: * source position.
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 void error(DiagnosticPosition pos, String key, Object ... args) {
aoqi@0: report(diags.error(source, pos, key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Report an error, unless another error was already reported at same
aoqi@0: * source position.
aoqi@0: * @param flag A flag to set on the diagnostic
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 void error(DiagnosticFlag flag, DiagnosticPosition pos, String key, Object ... args) {
aoqi@0: JCDiagnostic d = diags.error(source, pos, key, args);
aoqi@0: d.setFlag(flag);
aoqi@0: report(d);
aoqi@0: }
aoqi@0:
aoqi@0: /** Report an error, unless another error was already reported at same
aoqi@0: * source position.
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 void error(int pos, String key, Object ... args) {
aoqi@0: report(diags.error(source, wrap(pos), key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Report an error, unless another error was already reported at same
aoqi@0: * source position.
aoqi@0: * @param flag A flag to set on the diagnostic
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 void error(DiagnosticFlag flag, int pos, String key, Object ... args) {
aoqi@0: JCDiagnostic d = diags.error(source, wrap(pos), key, args);
aoqi@0: d.setFlag(flag);
aoqi@0: report(d);
aoqi@0: }
aoqi@0:
aoqi@0: /** Report a warning, unless suppressed by the -nowarn option or the
aoqi@0: * maximum number of warnings has been reached.
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 void warning(String key, Object ... args) {
aoqi@0: report(diags.warning(source, null, key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Report a lint warning, unless suppressed by the -nowarn option or the
aoqi@0: * maximum number of warnings has been reached.
aoqi@0: * @param lc The lint category for the diagnostic
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 void warning(LintCategory lc, String key, Object ... args) {
aoqi@0: report(diags.warning(lc, key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Report a warning, unless suppressed by the -nowarn option or the
aoqi@0: * maximum number of warnings has been reached.
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 void warning(DiagnosticPosition pos, String key, Object ... args) {
aoqi@0: report(diags.warning(source, pos, key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Report a lint warning, unless suppressed by the -nowarn option or the
aoqi@0: * maximum number of warnings has been reached.
aoqi@0: * @param lc The lint category for the diagnostic
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 void warning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) {
aoqi@0: report(diags.warning(lc, source, pos, key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Report a warning, unless suppressed by the -nowarn option or the
aoqi@0: * maximum number of warnings has been reached.
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 void warning(int pos, String key, Object ... args) {
aoqi@0: report(diags.warning(source, wrap(pos), key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Report a 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 void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) {
aoqi@0: report(diags.mandatoryWarning(source, pos, key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Report a warning.
aoqi@0: * @param lc The lint category for the diagnostic
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 void mandatoryWarning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) {
aoqi@0: report(diags.mandatoryWarning(lc, source, pos, key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
aoqi@0: * @param key The key for the localized notification message.
aoqi@0: * @param args Fields of the notint an error or warning message:
aoqi@0: */
aoqi@0: public void note(String key, Object ... args) {
aoqi@0: report(diags.note(source, null, key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
aoqi@0: * @param key The key for the localized notification message.
aoqi@0: * @param args Fields of the notification message.
aoqi@0: */
aoqi@0: public void note(DiagnosticPosition pos, String key, Object ... args) {
aoqi@0: report(diags.note(source, pos, key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
aoqi@0: * @param key The key for the localized notification message.
aoqi@0: * @param args Fields of the notification message.
aoqi@0: */
aoqi@0: public void note(int pos, String key, Object ... args) {
aoqi@0: report(diags.note(source, wrap(pos), key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
aoqi@0: * @param key The key for the localized notification message.
aoqi@0: * @param args Fields of the notification message.
aoqi@0: */
aoqi@0: public void note(JavaFileObject file, String key, Object ... args) {
aoqi@0: report(diags.note(getSource(file), null, key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
aoqi@0: * @param key The key for the localized notification message.
aoqi@0: * @param args Fields of the notification message.
aoqi@0: */
aoqi@0: public void mandatoryNote(final JavaFileObject file, String key, Object ... args) {
aoqi@0: report(diags.mandatoryNote(getSource(file), key, args));
aoqi@0: }
aoqi@0:
aoqi@0: protected abstract void report(JCDiagnostic diagnostic);
aoqi@0:
aoqi@0: protected abstract void directError(String key, Object... args);
aoqi@0:
aoqi@0: private DiagnosticPosition wrap(int pos) {
aoqi@0: return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos));
aoqi@0: }
aoqi@0:
aoqi@0: /** Factory for diagnostics
aoqi@0: */
aoqi@0: protected JCDiagnostic.Factory diags;
aoqi@0:
aoqi@0: /** The file that's currently being translated.
aoqi@0: */
aoqi@0: protected DiagnosticSource source;
aoqi@0:
aoqi@0: /** A cache of lightweight DiagnosticSource objects.
aoqi@0: */
aoqi@0: protected Map sourceMap;
aoqi@0: }