jjg@73: /*
jjg@73: * Copyright 1999-2008 Sun Microsystems, Inc. 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
jjg@73: * published by the Free Software Foundation. Sun designates this
jjg@73: * particular file as subject to the "Classpath" exception as provided
jjg@73: * by Sun 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: *
jjg@73: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@73: * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@73: * have any 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@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@73: *
This is NOT part of any API supported by Sun Microsystems. If
jjg@73: * 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)
jjg@73: return null;
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:
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.
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@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(String key, Object ... args) {
jjg@73: report(diags.warning(source, null, key, args));
jjg@73: }
jjg@73:
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@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@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: }