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

Mon, 14 Nov 2011 15:11:10 -0800

author
ksrini
date
Mon, 14 Nov 2011 15:11:10 -0800
changeset 1138
7375d4979bd3
parent 1074
04f983e3e825
child 1358
fc123bdeddb8
permissions
-rw-r--r--

7106166: (javac) re-factor EndPos parser
Reviewed-by: jjg

jjg@73 1 /*
ksrini@1074 2 * Copyright (c) 1999, 2011, 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.
ksrini@1074 99 * @param flag A flag to set on the diagnostic
ksrini@1074 100 * @param pos The source position at which to report the error.
ksrini@1074 101 * @param key The key for the localized error message.
ksrini@1074 102 * @param args Fields of the error message.
ksrini@1074 103 */
ksrini@1074 104 public void error(DiagnosticFlag flag, DiagnosticPosition pos, String key, Object ... args) {
ksrini@1074 105 JCDiagnostic d = diags.error(source, pos, key, args);
ksrini@1074 106 d.setFlag(flag);
ksrini@1074 107 report(d);
ksrini@1074 108 }
ksrini@1074 109
ksrini@1074 110 /** Report an error, unless another error was already reported at same
ksrini@1074 111 * source position.
jjg@73 112 * @param pos The source position at which to report the error.
jjg@73 113 * @param key The key for the localized error message.
jjg@73 114 * @param args Fields of the error message.
jjg@73 115 */
jjg@73 116 public void error(int pos, String key, Object ... args) {
jjg@73 117 report(diags.error(source, wrap(pos), key, args));
jjg@73 118 }
jjg@73 119
jjg@726 120 /** Report an error, unless another error was already reported at same
jjg@726 121 * source position.
jjg@726 122 * @param flag A flag to set on the diagnostic
jjg@726 123 * @param pos The source position at which to report the error.
jjg@726 124 * @param key The key for the localized error message.
jjg@726 125 * @param args Fields of the error message.
jjg@726 126 */
jjg@726 127 public void error(DiagnosticFlag flag, int pos, String key, Object ... args) {
jjg@726 128 JCDiagnostic d = diags.error(source, wrap(pos), key, args);
jjg@726 129 d.setFlag(flag);
jjg@726 130 report(d);
jjg@726 131 }
jjg@726 132
jjg@73 133 /** Report a warning, unless suppressed by the -nowarn option or the
jjg@73 134 * maximum number of warnings has been reached.
jjg@73 135 * @param pos The source position at which to report the warning.
jjg@73 136 * @param key The key for the localized warning message.
jjg@73 137 * @param args Fields of the warning message.
jjg@73 138 */
jjg@73 139 public void warning(String key, Object ... args) {
jjg@73 140 report(diags.warning(source, null, key, args));
jjg@73 141 }
jjg@73 142
jjg@612 143 /** Report a lint warning, unless suppressed by the -nowarn option or the
jjg@612 144 * maximum number of warnings has been reached.
jjg@612 145 * @param lc The lint category for the diagnostic
jjg@612 146 * @param key The key for the localized warning message.
jjg@612 147 * @param args Fields of the warning message.
jjg@612 148 */
jjg@612 149 public void warning(LintCategory lc, String key, Object ... args) {
jjg@612 150 report(diags.warning(lc, key, args));
jjg@612 151 }
jjg@612 152
jjg@73 153 /** Report a warning, unless suppressed by the -nowarn option or the
jjg@73 154 * maximum number of warnings has been reached.
jjg@73 155 * @param pos The source position at which to report the warning.
jjg@73 156 * @param key The key for the localized warning message.
jjg@73 157 * @param args Fields of the warning message.
jjg@73 158 */
jjg@73 159 public void warning(DiagnosticPosition pos, String key, Object ... args) {
jjg@73 160 report(diags.warning(source, pos, key, args));
jjg@73 161 }
jjg@73 162
jjg@612 163 /** Report a lint warning, unless suppressed by the -nowarn option or the
jjg@612 164 * maximum number of warnings has been reached.
jjg@612 165 * @param lc The lint category for the diagnostic
jjg@612 166 * @param pos The source position at which to report the warning.
jjg@612 167 * @param key The key for the localized warning message.
jjg@612 168 * @param args Fields of the warning message.
jjg@612 169 */
jjg@612 170 public void warning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) {
jjg@612 171 report(diags.warning(lc, source, pos, key, args));
jjg@612 172 }
jjg@612 173
jjg@73 174 /** Report a warning, unless suppressed by the -nowarn option or the
jjg@73 175 * maximum number of warnings has been reached.
jjg@73 176 * @param pos The source position at which to report the warning.
jjg@73 177 * @param key The key for the localized warning message.
jjg@73 178 * @param args Fields of the warning message.
jjg@73 179 */
jjg@73 180 public void warning(int pos, String key, Object ... args) {
jjg@73 181 report(diags.warning(source, wrap(pos), key, args));
jjg@73 182 }
jjg@73 183
jjg@73 184 /** Report a warning.
jjg@73 185 * @param pos The source position at which to report the warning.
jjg@73 186 * @param key The key for the localized warning message.
jjg@73 187 * @param args Fields of the warning message.
jjg@73 188 */
jjg@73 189 public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) {
jjg@73 190 report(diags.mandatoryWarning(source, pos, key, args));
jjg@73 191 }
jjg@73 192
jjg@612 193 /** Report a warning.
jjg@612 194 * @param lc The lint category for the diagnostic
jjg@612 195 * @param pos The source position at which to report the warning.
jjg@612 196 * @param key The key for the localized warning message.
jjg@612 197 * @param args Fields of the warning message.
jjg@612 198 */
jjg@612 199 public void mandatoryWarning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) {
jjg@612 200 report(diags.mandatoryWarning(lc, source, pos, key, args));
jjg@612 201 }
jjg@612 202
jjg@73 203 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73 204 * @param key The key for the localized notification message.
jjg@73 205 * @param args Fields of the notint an error or warning message:
jjg@73 206 */
jjg@73 207 public void note(String key, Object ... args) {
jjg@73 208 report(diags.note(source, null, key, args));
jjg@73 209 }
jjg@73 210
jjg@73 211 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73 212 * @param key The key for the localized notification message.
jjg@73 213 * @param args Fields of the notification message.
jjg@73 214 */
jjg@73 215 public void note(DiagnosticPosition pos, String key, Object ... args) {
jjg@73 216 report(diags.note(source, pos, key, args));
jjg@73 217 }
jjg@73 218
jjg@73 219 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73 220 * @param key The key for the localized notification message.
jjg@73 221 * @param args Fields of the notification message.
jjg@73 222 */
jjg@73 223 public void note(int pos, String key, Object ... args) {
jjg@73 224 report(diags.note(source, wrap(pos), key, args));
jjg@73 225 }
jjg@73 226
jjg@73 227 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73 228 * @param key The key for the localized notification message.
jjg@73 229 * @param args Fields of the notification message.
jjg@73 230 */
jjg@73 231 public void note(JavaFileObject file, String key, Object ... args) {
jjg@73 232 report(diags.note(getSource(file), null, key, args));
jjg@73 233 }
jjg@73 234
jjg@73 235 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73 236 * @param key The key for the localized notification message.
jjg@73 237 * @param args Fields of the notification message.
jjg@73 238 */
jjg@73 239 public void mandatoryNote(final JavaFileObject file, String key, Object ... args) {
jjg@73 240 report(diags.mandatoryNote(getSource(file), key, args));
jjg@73 241 }
jjg@73 242
jjg@73 243 protected abstract void report(JCDiagnostic diagnostic);
jjg@73 244
jjg@73 245 protected abstract void directError(String key, Object... args);
jjg@73 246
jjg@73 247 private DiagnosticPosition wrap(int pos) {
jjg@73 248 return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos));
jjg@73 249 }
jjg@73 250
jjg@73 251 /** Factory for diagnostics
jjg@73 252 */
jjg@73 253 protected JCDiagnostic.Factory diags;
jjg@73 254
jjg@73 255 /** The file that's currently being translated.
jjg@73 256 */
jjg@73 257 protected DiagnosticSource source;
jjg@73 258
jjg@73 259 /** A cache of lightweight DiagnosticSource objects.
jjg@73 260 */
jjg@73 261 protected Map<JavaFileObject, DiagnosticSource> sourceMap;
jjg@73 262 }

mercurial