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

Thu, 09 Oct 2008 16:19:13 +0100

author
mcimadamore
date
Thu, 09 Oct 2008 16:19:13 +0100
changeset 137
e4eaddca54b7
parent 73
1cf29847eb6e
child 168
4cdaaf4c5dca
permissions
-rw-r--r--

6731573: diagnostic output should optionally include source line
Summary: Added an -XD option to optionally prints out source lines in error messages
Reviewed-by: jjg

jjg@73 1 /*
jjg@73 2 * Copyright 1999-2008 Sun Microsystems, Inc. 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
jjg@73 7 * published by the Free Software Foundation. Sun designates this
jjg@73 8 * particular file as subject to the "Classpath" exception as provided
jjg@73 9 * by Sun 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 *
jjg@73 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@73 22 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@73 23 * have any 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@73 32 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
jjg@73 33 import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
jjg@73 34
jjg@73 35
jjg@73 36 /**
jjg@73 37 * A base class for error logs. Reports errors and warnings, and
jjg@73 38 * keeps track of error numbers and positions.
jjg@73 39 *
jjg@73 40 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
jjg@73 41 * you write code that depends on this, you do so at your own risk.
jjg@73 42 * This code and its internal interfaces are subject to change or
jjg@73 43 * deletion without notice.</b>
jjg@73 44 */
jjg@73 45 public abstract class AbstractLog {
jjg@73 46 AbstractLog(JCDiagnostic.Factory diags) {
jjg@73 47 this.diags = diags;
jjg@73 48 sourceMap = new HashMap<JavaFileObject, DiagnosticSource>();
jjg@73 49 }
jjg@73 50
jjg@73 51 /** Re-assign source, returning previous setting.
jjg@73 52 */
jjg@73 53 public JavaFileObject useSource(JavaFileObject file) {
jjg@73 54 JavaFileObject prev = (source == null ? null : source.getFile());
jjg@73 55 source = getSource(file);
jjg@73 56 return prev;
jjg@73 57 }
jjg@73 58
jjg@73 59 protected DiagnosticSource getSource(JavaFileObject file) {
jjg@73 60 if (file == null)
jjg@73 61 return null;
jjg@73 62 DiagnosticSource s = sourceMap.get(file);
jjg@73 63 if (s == null) {
jjg@73 64 s = new DiagnosticSource(file, this);
jjg@73 65 sourceMap.put(file, s);
jjg@73 66 }
jjg@73 67 return s;
jjg@73 68 }
jjg@73 69
jjg@73 70 /** Report an error, unless another error was already reported at same
jjg@73 71 * source position.
jjg@73 72 * @param key The key for the localized error message.
jjg@73 73 * @param args Fields of the error message.
jjg@73 74 */
jjg@73 75 public void error(String key, Object ... args) {
jjg@73 76 report(diags.error(source, null, key, args));
jjg@73 77 }
jjg@73 78
jjg@73 79 /** Report an error, unless another error was already reported at same
jjg@73 80 * source position.
jjg@73 81 * @param pos The source position at which to report the error.
jjg@73 82 * @param key The key for the localized error message.
jjg@73 83 * @param args Fields of the error message.
jjg@73 84 */
jjg@73 85 public void error(DiagnosticPosition pos, String key, Object ... args) {
jjg@73 86 report(diags.error(source, pos, key, args));
jjg@73 87 }
jjg@73 88
jjg@73 89 /** Report an error, unless another error was already reported at same
jjg@73 90 * source position.
jjg@73 91 * @param pos The source position at which to report the error.
jjg@73 92 * @param key The key for the localized error message.
jjg@73 93 * @param args Fields of the error message.
jjg@73 94 */
jjg@73 95 public void error(int pos, String key, Object ... args) {
jjg@73 96 report(diags.error(source, wrap(pos), key, args));
jjg@73 97 }
jjg@73 98
jjg@73 99 /** Report a warning, unless suppressed by the -nowarn option or the
jjg@73 100 * maximum number of warnings has been reached.
jjg@73 101 * @param pos The source position at which to report the warning.
jjg@73 102 * @param key The key for the localized warning message.
jjg@73 103 * @param args Fields of the warning message.
jjg@73 104 */
jjg@73 105 public void warning(String key, Object ... args) {
jjg@73 106 report(diags.warning(source, null, key, args));
jjg@73 107 }
jjg@73 108
jjg@73 109 /** Report a warning, unless suppressed by the -nowarn option or the
jjg@73 110 * maximum number of warnings has been reached.
jjg@73 111 * @param pos The source position at which to report the warning.
jjg@73 112 * @param key The key for the localized warning message.
jjg@73 113 * @param args Fields of the warning message.
jjg@73 114 */
jjg@73 115 public void warning(DiagnosticPosition pos, String key, Object ... args) {
jjg@73 116 report(diags.warning(source, pos, key, args));
jjg@73 117 }
jjg@73 118
jjg@73 119 /** Report a warning, unless suppressed by the -nowarn option or the
jjg@73 120 * maximum number of warnings has been reached.
jjg@73 121 * @param pos The source position at which to report the warning.
jjg@73 122 * @param key The key for the localized warning message.
jjg@73 123 * @param args Fields of the warning message.
jjg@73 124 */
jjg@73 125 public void warning(int pos, String key, Object ... args) {
jjg@73 126 report(diags.warning(source, wrap(pos), key, args));
jjg@73 127 }
jjg@73 128
jjg@73 129 /** Report a warning.
jjg@73 130 * @param pos The source position at which to report the warning.
jjg@73 131 * @param key The key for the localized warning message.
jjg@73 132 * @param args Fields of the warning message.
jjg@73 133 */
jjg@73 134 public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) {
jjg@73 135 report(diags.mandatoryWarning(source, pos, key, args));
jjg@73 136 }
jjg@73 137
jjg@73 138 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73 139 * @param key The key for the localized notification message.
jjg@73 140 * @param args Fields of the notint an error or warning message:
jjg@73 141 */
jjg@73 142 public void note(String key, Object ... args) {
jjg@73 143 report(diags.note(source, null, key, args));
jjg@73 144 }
jjg@73 145
jjg@73 146 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73 147 * @param key The key for the localized notification message.
jjg@73 148 * @param args Fields of the notification message.
jjg@73 149 */
jjg@73 150 public void note(DiagnosticPosition pos, String key, Object ... args) {
jjg@73 151 report(diags.note(source, pos, key, args));
jjg@73 152 }
jjg@73 153
jjg@73 154 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73 155 * @param key The key for the localized notification message.
jjg@73 156 * @param args Fields of the notification message.
jjg@73 157 */
jjg@73 158 public void note(int pos, String key, Object ... args) {
jjg@73 159 report(diags.note(source, wrap(pos), key, args));
jjg@73 160 }
jjg@73 161
jjg@73 162 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73 163 * @param key The key for the localized notification message.
jjg@73 164 * @param args Fields of the notification message.
jjg@73 165 */
jjg@73 166 public void note(JavaFileObject file, String key, Object ... args) {
jjg@73 167 report(diags.note(getSource(file), null, key, args));
jjg@73 168 }
jjg@73 169
jjg@73 170 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
jjg@73 171 * @param key The key for the localized notification message.
jjg@73 172 * @param args Fields of the notification message.
jjg@73 173 */
jjg@73 174 public void mandatoryNote(final JavaFileObject file, String key, Object ... args) {
jjg@73 175 report(diags.mandatoryNote(getSource(file), key, args));
jjg@73 176 }
jjg@73 177
jjg@73 178 protected abstract void report(JCDiagnostic diagnostic);
jjg@73 179
jjg@73 180 protected abstract void directError(String key, Object... args);
jjg@73 181
jjg@73 182 private DiagnosticPosition wrap(int pos) {
jjg@73 183 return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos));
jjg@73 184 }
jjg@73 185
jjg@73 186 /** Factory for diagnostics
jjg@73 187 */
jjg@73 188 protected JCDiagnostic.Factory diags;
jjg@73 189
jjg@73 190 /** The file that's currently being translated.
jjg@73 191 */
jjg@73 192 protected DiagnosticSource source;
jjg@73 193
jjg@73 194 /** A cache of lightweight DiagnosticSource objects.
jjg@73 195 */
jjg@73 196 protected Map<JavaFileObject, DiagnosticSource> sourceMap;
jjg@73 197 }

mercurial