Mon, 10 Jan 2011 15:08:31 -0800
6396503: javac should not require assertions enabled
Reviewed-by: mcimadamore
1 /*
2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.javac.util;
28 import java.util.HashMap;
29 import java.util.Map;
30 import javax.tools.JavaFileObject;
32 import com.sun.tools.javac.code.Lint.LintCategory;
33 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
34 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
35 import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
38 /**
39 * A base class for error logs. Reports errors and warnings, and
40 * keeps track of error numbers and positions.
41 *
42 * <p><b>This is NOT part of any supported API.
43 * If you write code that depends on this, you do so at your own risk.
44 * This code and its internal interfaces are subject to change or
45 * deletion without notice.</b>
46 */
47 public abstract class AbstractLog {
48 AbstractLog(JCDiagnostic.Factory diags) {
49 this.diags = diags;
50 sourceMap = new HashMap<JavaFileObject, DiagnosticSource>();
51 }
53 /** Re-assign source, returning previous setting.
54 */
55 public JavaFileObject useSource(JavaFileObject file) {
56 JavaFileObject prev = (source == null ? null : source.getFile());
57 source = getSource(file);
58 return prev;
59 }
61 protected DiagnosticSource getSource(JavaFileObject file) {
62 if (file == null)
63 return DiagnosticSource.NO_SOURCE;
64 DiagnosticSource s = sourceMap.get(file);
65 if (s == null) {
66 s = new DiagnosticSource(file, this);
67 sourceMap.put(file, s);
68 }
69 return s;
70 }
72 /** Return the underlying diagnostic source
73 */
74 public DiagnosticSource currentSource() {
75 return source;
76 }
78 /** Report an error, unless another error was already reported at same
79 * source position.
80 * @param key The key for the localized error message.
81 * @param args Fields of the error message.
82 */
83 public void error(String key, Object ... args) {
84 report(diags.error(source, null, key, args));
85 }
87 /** Report an error, unless another error was already reported at same
88 * source position.
89 * @param pos The source position at which to report the error.
90 * @param key The key for the localized error message.
91 * @param args Fields of the error message.
92 */
93 public void error(DiagnosticPosition pos, String key, Object ... args) {
94 report(diags.error(source, pos, key, args));
95 }
97 /** Report an error, unless another error was already reported at same
98 * source position.
99 * @param pos The source position at which to report the error.
100 * @param key The key for the localized error message.
101 * @param args Fields of the error message.
102 */
103 public void error(int pos, String key, Object ... args) {
104 report(diags.error(source, wrap(pos), key, args));
105 }
107 /** Report an error, unless another error was already reported at same
108 * source position.
109 * @param flag A flag to set on the diagnostic
110 * @param pos The source position at which to report the error.
111 * @param key The key for the localized error message.
112 * @param args Fields of the error message.
113 */
114 public void error(DiagnosticFlag flag, int pos, String key, Object ... args) {
115 JCDiagnostic d = diags.error(source, wrap(pos), key, args);
116 d.setFlag(flag);
117 report(d);
118 }
120 /** Report a warning, unless suppressed by the -nowarn option or the
121 * maximum number of warnings has been reached.
122 * @param pos The source position at which to report the warning.
123 * @param key The key for the localized warning message.
124 * @param args Fields of the warning message.
125 */
126 public void warning(String key, Object ... args) {
127 report(diags.warning(source, null, key, args));
128 }
130 /** Report a lint warning, unless suppressed by the -nowarn option or the
131 * maximum number of warnings has been reached.
132 * @param lc The lint category for the diagnostic
133 * @param key The key for the localized warning message.
134 * @param args Fields of the warning message.
135 */
136 public void warning(LintCategory lc, String key, Object ... args) {
137 report(diags.warning(lc, key, args));
138 }
140 /** Report a warning, unless suppressed by the -nowarn option or the
141 * maximum number of warnings has been reached.
142 * @param pos The source position at which to report the warning.
143 * @param key The key for the localized warning message.
144 * @param args Fields of the warning message.
145 */
146 public void warning(DiagnosticPosition pos, String key, Object ... args) {
147 report(diags.warning(source, pos, key, args));
148 }
150 /** Report a lint warning, unless suppressed by the -nowarn option or the
151 * maximum number of warnings has been reached.
152 * @param lc The lint category for the diagnostic
153 * @param pos The source position at which to report the warning.
154 * @param key The key for the localized warning message.
155 * @param args Fields of the warning message.
156 */
157 public void warning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) {
158 report(diags.warning(lc, source, pos, key, args));
159 }
161 /** Report a warning, unless suppressed by the -nowarn option or the
162 * maximum number of warnings has been reached.
163 * @param pos The source position at which to report the warning.
164 * @param key The key for the localized warning message.
165 * @param args Fields of the warning message.
166 */
167 public void warning(int pos, String key, Object ... args) {
168 report(diags.warning(source, wrap(pos), key, args));
169 }
171 /** Report a warning.
172 * @param pos The source position at which to report the warning.
173 * @param key The key for the localized warning message.
174 * @param args Fields of the warning message.
175 */
176 public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) {
177 report(diags.mandatoryWarning(source, pos, key, args));
178 }
180 /** Report a warning.
181 * @param lc The lint category for the diagnostic
182 * @param pos The source position at which to report the warning.
183 * @param key The key for the localized warning message.
184 * @param args Fields of the warning message.
185 */
186 public void mandatoryWarning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) {
187 report(diags.mandatoryWarning(lc, source, pos, key, args));
188 }
190 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
191 * @param key The key for the localized notification message.
192 * @param args Fields of the notint an error or warning message:
193 */
194 public void note(String key, Object ... args) {
195 report(diags.note(source, null, key, args));
196 }
198 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
199 * @param key The key for the localized notification message.
200 * @param args Fields of the notification message.
201 */
202 public void note(DiagnosticPosition pos, String key, Object ... args) {
203 report(diags.note(source, pos, key, args));
204 }
206 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
207 * @param key The key for the localized notification message.
208 * @param args Fields of the notification message.
209 */
210 public void note(int pos, String key, Object ... args) {
211 report(diags.note(source, wrap(pos), key, args));
212 }
214 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
215 * @param key The key for the localized notification message.
216 * @param args Fields of the notification message.
217 */
218 public void note(JavaFileObject file, String key, Object ... args) {
219 report(diags.note(getSource(file), null, key, args));
220 }
222 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
223 * @param key The key for the localized notification message.
224 * @param args Fields of the notification message.
225 */
226 public void mandatoryNote(final JavaFileObject file, String key, Object ... args) {
227 report(diags.mandatoryNote(getSource(file), key, args));
228 }
230 protected abstract void report(JCDiagnostic diagnostic);
232 protected abstract void directError(String key, Object... args);
234 private DiagnosticPosition wrap(int pos) {
235 return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos));
236 }
238 /** Factory for diagnostics
239 */
240 protected JCDiagnostic.Factory diags;
242 /** The file that's currently being translated.
243 */
244 protected DiagnosticSource source;
246 /** A cache of lightweight DiagnosticSource objects.
247 */
248 protected Map<JavaFileObject, DiagnosticSource> sourceMap;
249 }