src/share/classes/com/sun/tools/javac/util/AbstractLog.java

Mon, 10 Jan 2011 15:08:31 -0800

author
jjg
date
Mon, 10 Jan 2011 15:08:31 -0800
changeset 816
7c537f4298fb
parent 798
4868a36f6fd8
child 1074
04f983e3e825
permissions
-rw-r--r--

6396503: javac should not require assertions enabled
Reviewed-by: mcimadamore

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

mercurial