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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial