001    /*
002     * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.javac.util;
027    
028    import java.util.HashMap;
029    import java.util.Map;
030    import javax.tools.JavaFileObject;
031    
032    import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
033    import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
034    
035    
036    /**
037     *  A base class for error logs. Reports errors and warnings, and
038     *  keeps track of error numbers and positions.
039     *
040     *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
041     *  you write code that depends on this, you do so at your own risk.
042     *  This code and its internal interfaces are subject to change or
043     *  deletion without notice.</b>
044     */
045    public abstract class AbstractLog {
046        AbstractLog(JCDiagnostic.Factory diags) {
047            this.diags = diags;
048            sourceMap = new HashMap<JavaFileObject, DiagnosticSource>();
049        }
050    
051        /** Re-assign source, returning previous setting.
052         */
053        public JavaFileObject useSource(JavaFileObject file) {
054            JavaFileObject prev = (source == null ? null : source.getFile());
055            source = getSource(file);
056            return prev;
057        }
058    
059        protected DiagnosticSource getSource(JavaFileObject file) {
060            if (file == null)
061                return null;
062            DiagnosticSource s = sourceMap.get(file);
063            if (s == null) {
064                s = new DiagnosticSource(file, this);
065                sourceMap.put(file, s);
066            }
067            return s;
068        }
069    
070        /** Return the underlying diagnostic source
071         */
072        public DiagnosticSource currentSource() {
073            return source;
074        }
075    
076        /** Report an error, unless another error was already reported at same
077         *  source position.
078         *  @param key    The key for the localized error message.
079         *  @param args   Fields of the error message.
080         */
081        public void error(String key, Object ... args) {
082            report(diags.error(source, null, key, args));
083        }
084    
085        /** Report an error, unless another error was already reported at same
086         *  source position.
087         *  @param pos    The source position at which to report the error.
088         *  @param key    The key for the localized error message.
089         *  @param args   Fields of the error message.
090         */
091        public void error(DiagnosticPosition pos, String key, Object ... args) {
092            report(diags.error(source, pos, key, args));
093        }
094    
095        /** Report an error, unless another error was already reported at same
096         *  source position.
097         *  @param pos    The source position at which to report the error.
098         *  @param key    The key for the localized error message.
099         *  @param args   Fields of the error message.
100         */
101        public void error(int pos, String key, Object ... args) {
102            report(diags.error(source, wrap(pos), key, args));
103        }
104    
105        /** Report a warning, unless suppressed by the  -nowarn option or the
106         *  maximum number of warnings has been reached.
107         *  @param pos    The source position at which to report the warning.
108         *  @param key    The key for the localized warning message.
109         *  @param args   Fields of the warning message.
110         */
111        public void warning(String key, Object ... args) {
112            report(diags.warning(source, null, key, args));
113        }
114    
115        /** Report a warning, unless suppressed by the  -nowarn option or the
116         *  maximum number of warnings has been reached.
117         *  @param pos    The source position at which to report the warning.
118         *  @param key    The key for the localized warning message.
119         *  @param args   Fields of the warning message.
120         */
121        public void warning(DiagnosticPosition pos, String key, Object ... args) {
122            report(diags.warning(source, pos, key, args));
123        }
124    
125        /** Report a warning, unless suppressed by the  -nowarn option or the
126         *  maximum number of warnings has been reached.
127         *  @param pos    The source position at which to report the warning.
128         *  @param key    The key for the localized warning message.
129         *  @param args   Fields of the warning message.
130         */
131        public void warning(int pos, String key, Object ... args) {
132            report(diags.warning(source, wrap(pos), key, args));
133        }
134    
135        /** Report a warning.
136         *  @param pos    The source position at which to report the warning.
137         *  @param key    The key for the localized warning message.
138         *  @param args   Fields of the warning message.
139         */
140        public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) {
141            report(diags.mandatoryWarning(source, pos, key, args));
142        }
143    
144        /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
145         *  @param key    The key for the localized notification message.
146         *  @param args   Fields of the notint an error or warning message:
147         */
148        public void note(String key, Object ... args) {
149            report(diags.note(source, null, key, args));
150        }
151    
152        /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
153         *  @param key    The key for the localized notification message.
154         *  @param args   Fields of the notification message.
155         */
156        public void note(DiagnosticPosition pos, String key, Object ... args) {
157            report(diags.note(source, pos, key, args));
158        }
159    
160        /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
161         *  @param key    The key for the localized notification message.
162         *  @param args   Fields of the notification message.
163         */
164        public void note(int pos, String key, Object ... args) {
165            report(diags.note(source, wrap(pos), key, args));
166        }
167    
168        /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
169         *  @param key    The key for the localized notification message.
170         *  @param args   Fields of the notification message.
171         */
172        public void note(JavaFileObject file, String key, Object ... args) {
173            report(diags.note(getSource(file), null, key, args));
174        }
175    
176        /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
177         *  @param key    The key for the localized notification message.
178         *  @param args   Fields of the notification message.
179         */
180        public void mandatoryNote(final JavaFileObject file, String key, Object ... args) {
181            report(diags.mandatoryNote(getSource(file), key, args));
182        }
183    
184        protected abstract void report(JCDiagnostic diagnostic);
185    
186        protected abstract void directError(String key, Object... args);
187    
188        private DiagnosticPosition wrap(int pos) {
189            return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos));
190        }
191    
192        /** Factory for diagnostics
193         */
194        protected JCDiagnostic.Factory diags;
195    
196        /** The file that's currently being translated.
197         */
198        protected DiagnosticSource source;
199    
200        /** A cache of lightweight DiagnosticSource objects.
201         */
202        protected Map<JavaFileObject, DiagnosticSource> sourceMap;
203    }