AbstractLog.java revision 2571:10fc81ac75b4
1272953Srodrigc/* 2264790Sbapt * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. 3264790Sbapt * 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 */ 25 26package com.sun.tools.javac.util; 27 28import java.util.HashMap; 29import java.util.Map; 30import javax.tools.JavaFileObject; 31 32import com.sun.tools.javac.code.Lint.LintCategory; 33import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag; 34import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 35import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition; 36 37 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 */ 47public abstract class AbstractLog { 48 /** Factory for diagnostics 49 */ 50 protected JCDiagnostic.Factory diags; 51 52 /** The file that's currently being translated. 53 */ 54 protected DiagnosticSource source; 55 56 /** A cache of lightweight DiagnosticSource objects. 57 */ 58 protected Map<JavaFileObject, DiagnosticSource> sourceMap; 59 60 AbstractLog(JCDiagnostic.Factory diags) { 61 this.diags = diags; 62 sourceMap = new HashMap<>(); 63 } 64 65 /** Re-assign source, returning previous setting. 66 */ 67 public JavaFileObject useSource(JavaFileObject file) { 68 JavaFileObject prev = (source == null ? null : source.getFile()); 69 source = getSource(file); 70 return prev; 71 } 72 73 protected DiagnosticSource getSource(JavaFileObject file) { 74 if (file == null) 75 return DiagnosticSource.NO_SOURCE; 76 DiagnosticSource s = sourceMap.get(file); 77 if (s == null) { 78 s = new DiagnosticSource(file, this); 79 sourceMap.put(file, s); 80 } 81 return s; 82 } 83 84 /** Return the underlying diagnostic source 85 */ 86 public DiagnosticSource currentSource() { 87 return source; 88 } 89 90 /** Report an error, unless another error was already reported at same 91 * source position. 92 * @param key The key for the localized error message. 93 * @param args Fields of the error message. 94 */ 95 public void error(String key, Object ... args) { 96 report(diags.error(source, null, key, args)); 97 } 98 99 /** Report an error, unless another error was already reported at same 100 * source position. 101 * @param pos The source position at which to report the error. 102 * @param key The key for the localized error message. 103 * @param args Fields of the error message. 104 */ 105 public void error(DiagnosticPosition pos, String key, Object ... args) { 106 report(diags.error(source, pos, key, args)); 107 } 108 109 /** Report an error, unless another error was already reported at same 110 * source position. 111 * @param flag A flag to set on the diagnostic 112 * @param pos The source position at which to report the error. 113 * @param key The key for the localized error message. 114 * @param args Fields of the error message. 115 */ 116 public void error(DiagnosticFlag flag, DiagnosticPosition pos, String key, Object ... args) { 117 JCDiagnostic d = diags.error(source, pos, key, args); 118 d.setFlag(flag); 119 report(d); 120 } 121 122 /** Report an error, unless another error was already reported at same 123 * source position. 124 * @param pos The source position at which to report the error. 125 * @param key The key for the localized error message. 126 * @param args Fields of the error message. 127 */ 128 public void error(int pos, String key, Object ... args) { 129 report(diags.error(source, wrap(pos), key, args)); 130 } 131 132 /** Report an error, unless another error was already reported at same 133 * source position. 134 * @param flag A flag to set on the diagnostic 135 * @param pos The source position at which to report the error. 136 * @param key The key for the localized error message. 137 * @param args Fields of the error message. 138 */ 139 public void error(DiagnosticFlag flag, int pos, String key, Object ... args) { 140 JCDiagnostic d = diags.error(source, wrap(pos), key, args); 141 d.setFlag(flag); 142 report(d); 143 } 144 145 /** Report a warning, unless suppressed by the -nowarn option or the 146 * maximum number of warnings has been reached. 147 * @param key The key for the localized warning message. 148 * @param args Fields of the warning message. 149 */ 150 public void warning(String key, Object ... args) { 151 report(diags.warning(source, null, key, args)); 152 } 153 154 /** Report a lint warning, unless suppressed by the -nowarn option or the 155 * maximum number of warnings has been reached. 156 * @param lc The lint category for the diagnostic 157 * @param key The key for the localized warning message. 158 * @param args Fields of the warning message. 159 */ 160 public void warning(LintCategory lc, String key, Object ... args) { 161 report(diags.warning(lc, key, args)); 162 } 163 164 /** Report a warning, unless suppressed by the -nowarn option or the 165 * maximum number of warnings has been reached. 166 * @param pos The source position at which to report the warning. 167 * @param key The key for the localized warning message. 168 * @param args Fields of the warning message. 169 */ 170 public void warning(DiagnosticPosition pos, String key, Object ... args) { 171 report(diags.warning(source, pos, key, args)); 172 } 173 174 /** Report a lint warning, unless suppressed by the -nowarn option or the 175 * maximum number of warnings has been reached. 176 * @param lc The lint category for the diagnostic 177 * @param pos The source position at which to report the warning. 178 * @param key The key for the localized warning message. 179 * @param args Fields of the warning message. 180 */ 181 public void warning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) { 182 report(diags.warning(lc, source, pos, key, args)); 183 } 184 185 /** Report a warning, unless suppressed by the -nowarn option or the 186 * maximum number of warnings has been reached. 187 * @param pos The source position at which to report the warning. 188 * @param key The key for the localized warning message. 189 * @param args Fields of the warning message. 190 */ 191 public void warning(int pos, String key, Object ... args) { 192 report(diags.warning(source, wrap(pos), key, args)); 193 } 194 195 /** Report a warning. 196 * @param pos The source position at which to report the warning. 197 * @param key The key for the localized warning message. 198 * @param args Fields of the warning message. 199 */ 200 public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) { 201 report(diags.mandatoryWarning(source, pos, key, args)); 202 } 203 204 /** Report a warning. 205 * @param lc The lint category for the diagnostic 206 * @param pos The source position at which to report the warning. 207 * @param key The key for the localized warning message. 208 * @param args Fields of the warning message. 209 */ 210 public void mandatoryWarning(LintCategory lc, DiagnosticPosition pos, String key, Object ... args) { 211 report(diags.mandatoryWarning(lc, source, pos, key, args)); 212 } 213 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 notint an error or warning message: 217 */ 218 public void note(String key, Object ... args) { 219 report(diags.note(source, null, key, args)); 220 } 221 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 note(DiagnosticPosition pos, String key, Object ... args) { 227 report(diags.note(source, pos, key, args)); 228 } 229 230 /** Provide a non-fatal notification, unless suppressed by the -nowarn option. 231 * @param key The key for the localized notification message. 232 * @param args Fields of the notification message. 233 */ 234 public void note(int pos, String key, Object ... args) { 235 report(diags.note(source, wrap(pos), key, args)); 236 } 237 238 /** Provide a non-fatal notification, unless suppressed by the -nowarn option. 239 * @param key The key for the localized notification message. 240 * @param args Fields of the notification message. 241 */ 242 public void note(JavaFileObject file, String key, Object ... args) { 243 report(diags.note(getSource(file), null, key, args)); 244 } 245 246 /** Provide a non-fatal notification, unless suppressed by the -nowarn option. 247 * @param key The key for the localized notification message. 248 * @param args Fields of the notification message. 249 */ 250 public void mandatoryNote(final JavaFileObject file, String key, Object ... args) { 251 report(diags.mandatoryNote(getSource(file), key, args)); 252 } 253 254 protected abstract void report(JCDiagnostic diagnostic); 255 256 protected abstract void directError(String key, Object... args); 257 258 private DiagnosticPosition wrap(int pos) { 259 return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos)); 260 } 261} 262