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