1.1 --- a/src/share/classes/com/sun/tools/javac/util/Log.java Fri Jul 11 14:59:48 2008 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/util/Log.java Tue Jul 15 09:50:36 2008 -0700 1.3 @@ -26,8 +26,6 @@ 1.4 package com.sun.tools.javac.util; 1.5 1.6 import java.io.*; 1.7 -import java.nio.CharBuffer; 1.8 -import java.util.HashMap; 1.9 import java.util.HashSet; 1.10 import java.util.Map; 1.11 import java.util.Set; 1.12 @@ -38,9 +36,6 @@ 1.13 import com.sun.tools.javac.tree.JCTree; 1.14 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 1.15 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; 1.16 -import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition; 1.17 - 1.18 -import static com.sun.tools.javac.util.LayoutCharacters.*; 1.19 1.20 /** A class for error logs. Reports errors and warnings, and 1.21 * keeps track of error numbers and positions. 1.22 @@ -50,7 +45,7 @@ 1.23 * This code and its internal interfaces are subject to change or 1.24 * deletion without notice.</b> 1.25 */ 1.26 -public class Log { 1.27 +public class Log extends AbstractLog { 1.28 /** The context key for the log. */ 1.29 public static final Context.Key<Log> logKey 1.30 = new Context.Key<Log>(); 1.31 @@ -98,28 +93,22 @@ 1.32 * interface to javac (JSR 199). 1.33 */ 1.34 protected DiagnosticListener<? super JavaFileObject> diagListener; 1.35 + 1.36 /** 1.37 * Formatter for diagnostics 1.38 */ 1.39 private DiagnosticFormatter diagFormatter; 1.40 1.41 - /** 1.42 - * Factory for diagnostics 1.43 - */ 1.44 - private JCDiagnostic.Factory diags; 1.45 - 1.46 - 1.47 /** Construct a log with given I/O redirections. 1.48 */ 1.49 @Deprecated 1.50 protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { 1.51 + super(JCDiagnostic.Factory.instance(context)); 1.52 context.put(logKey, this); 1.53 this.errWriter = errWriter; 1.54 this.warnWriter = warnWriter; 1.55 this.noticeWriter = noticeWriter; 1.56 1.57 - this.diags = JCDiagnostic.Factory.instance(context); 1.58 - 1.59 Options options = Options.instance(context); 1.60 this.dumpOnError = options.get("-doe") != null; 1.61 this.promptOnError = options.get("-prompt") != null; 1.62 @@ -174,10 +163,6 @@ 1.63 return instance; 1.64 } 1.65 1.66 - /** The file that's currently translated. 1.67 - */ 1.68 - protected JCDiagnostic.DiagnosticSource source; 1.69 - 1.70 /** The number of errors encountered so far. 1.71 */ 1.72 public int nerrors = 0; 1.73 @@ -192,77 +177,13 @@ 1.74 */ 1.75 private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>(); 1.76 1.77 - private Map<JavaFileObject, Map<JCTree, Integer>> endPosTables; 1.78 - 1.79 - /** The buffer containing the file that's currently translated. 1.80 - */ 1.81 - private char[] buf = null; 1.82 - 1.83 - /** The length of useful data in buf 1.84 - */ 1.85 - private int bufLen = 0; 1.86 - 1.87 - /** The position in the buffer at which last error was reported 1.88 - */ 1.89 - private int bp; 1.90 - 1.91 - /** number of the current source line; first line is 1 1.92 - */ 1.93 - private int line; 1.94 - 1.95 - /** buffer index of the first character of the current source line 1.96 - */ 1.97 - private int lineStart; 1.98 - 1.99 public boolean hasDiagnosticListener() { 1.100 return diagListener != null; 1.101 } 1.102 1.103 public void setEndPosTable(JavaFileObject name, Map<JCTree, Integer> table) { 1.104 - if (endPosTables == null) 1.105 - endPosTables = new HashMap<JavaFileObject, Map<JCTree, Integer>>(); 1.106 - endPosTables.put(name, table); 1.107 - } 1.108 - 1.109 - /** Re-assign source, returning previous setting. 1.110 - */ 1.111 - public JavaFileObject useSource(final JavaFileObject name) { 1.112 - JavaFileObject prev = currentSource(); 1.113 - if (name != prev) { 1.114 - source = new JCDiagnostic.DiagnosticSource() { 1.115 - public JavaFileObject getFile() { 1.116 - return name; 1.117 - } 1.118 - public CharSequence getName() { 1.119 - return JavacFileManager.getJavacBaseFileName(getFile()); 1.120 - } 1.121 - public int getLineNumber(int pos) { 1.122 - return Log.this.getLineNumber(pos); 1.123 - } 1.124 - public int getColumnNumber(int pos) { 1.125 - return Log.this.getColumnNumber(pos); 1.126 - } 1.127 - public Map<JCTree, Integer> getEndPosTable() { 1.128 - return (endPosTables == null ? null : endPosTables.get(name)); 1.129 - } 1.130 - }; 1.131 - buf = null; 1.132 - } 1.133 - return prev; 1.134 - } 1.135 - 1.136 - /** Re-assign source buffer for existing source name. 1.137 - */ 1.138 - protected void setBuf(char[] newBuf) { 1.139 - buf = newBuf; 1.140 - bufLen = buf.length; 1.141 - bp = 0; 1.142 - lineStart = 0; 1.143 - line = 1; 1.144 - } 1.145 - 1.146 - protected char[] getBuf() { 1.147 - return buf; 1.148 + name.getClass(); // null check 1.149 + getSource(name).setEndPosTable(table); 1.150 } 1.151 1.152 /** Return current source name. 1.153 @@ -320,74 +241,19 @@ 1.154 * @param pos Buffer index of the error position, must be on current line 1.155 */ 1.156 private void printErrLine(int pos, PrintWriter writer) { 1.157 - if (!findLine(pos)) 1.158 + String line = (source == null ? null : source.getLine(pos)); 1.159 + if (line == null) 1.160 return; 1.161 + int col = source.getColumnNumber(pos); 1.162 1.163 - int lineEnd = lineStart; 1.164 - while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF) 1.165 - lineEnd++; 1.166 - if (lineEnd - lineStart == 0) 1.167 - return; 1.168 - printLines(writer, new String(buf, lineStart, lineEnd - lineStart)); 1.169 - for (bp = lineStart; bp < pos; bp++) { 1.170 - writer.print((buf[bp] == '\t') ? "\t" : " "); 1.171 + printLines(writer, line); 1.172 + for (int i = 0; i < col - 1; i++) { 1.173 + writer.print((line.charAt(i) == '\t') ? "\t" : " "); 1.174 } 1.175 writer.println("^"); 1.176 writer.flush(); 1.177 } 1.178 1.179 - protected void initBuf(JavaFileObject fileObject) throws IOException { 1.180 - CharSequence cs = fileObject.getCharContent(true); 1.181 - if (cs instanceof CharBuffer) { 1.182 - CharBuffer cb = (CharBuffer) cs; 1.183 - buf = JavacFileManager.toArray(cb); 1.184 - bufLen = cb.limit(); 1.185 - } else { 1.186 - buf = cs.toString().toCharArray(); 1.187 - bufLen = buf.length; 1.188 - } 1.189 - } 1.190 - 1.191 - /** Find the line in the buffer that contains the current position 1.192 - * @param pos Character offset into the buffer 1.193 - */ 1.194 - private boolean findLine(int pos) { 1.195 - if (pos == Position.NOPOS || currentSource() == null) 1.196 - return false; 1.197 - try { 1.198 - if (buf == null) { 1.199 - initBuf(currentSource()); 1.200 - lineStart = 0; 1.201 - line = 1; 1.202 - } else if (lineStart > pos) { // messages don't come in order 1.203 - lineStart = 0; 1.204 - line = 1; 1.205 - } 1.206 - bp = lineStart; 1.207 - while (bp < bufLen && bp < pos) { 1.208 - switch (buf[bp++]) { 1.209 - case CR: 1.210 - if (bp < bufLen && buf[bp] == LF) bp++; 1.211 - line++; 1.212 - lineStart = bp; 1.213 - break; 1.214 - case LF: 1.215 - line++; 1.216 - lineStart = bp; 1.217 - break; 1.218 - } 1.219 - } 1.220 - return bp <= bufLen; 1.221 - } catch (IOException e) { 1.222 - //e.printStackTrace(); 1.223 - // FIXME: include e.getLocalizedMessage() in error message 1.224 - printLines(errWriter, getLocalizedString("source.unavailable")); 1.225 - errWriter.flush(); 1.226 - buf = new char[0]; 1.227 - } 1.228 - return false; 1.229 - } 1.230 - 1.231 /** Print the text of a message, translating newlines appropriately 1.232 * for the platform. 1.233 */ 1.234 @@ -400,72 +266,9 @@ 1.235 if (msg.length() != 0) writer.println(msg); 1.236 } 1.237 1.238 - /** Report an error, unless another error was already reported at same 1.239 - * source position. 1.240 - * @param key The key for the localized error message. 1.241 - * @param args Fields of the error message. 1.242 - */ 1.243 - public void error(String key, Object ... args) { 1.244 - report(diags.error(source, null, key, args)); 1.245 - } 1.246 - 1.247 - /** Report an error, unless another error was already reported at same 1.248 - * source position. 1.249 - * @param pos The source position at which to report the error. 1.250 - * @param key The key for the localized error message. 1.251 - * @param args Fields of the error message. 1.252 - */ 1.253 - public void error(DiagnosticPosition pos, String key, Object ... args) { 1.254 - report(diags.error(source, pos, key, args)); 1.255 - } 1.256 - 1.257 - /** Report an error, unless another error was already reported at same 1.258 - * source position. 1.259 - * @param pos The source position at which to report the error. 1.260 - * @param key The key for the localized error message. 1.261 - * @param args Fields of the error message. 1.262 - */ 1.263 - public void error(int pos, String key, Object ... args) { 1.264 - report(diags.error(source, wrap(pos), key, args)); 1.265 - } 1.266 - 1.267 - /** Report a warning, unless suppressed by the -nowarn option or the 1.268 - * maximum number of warnings has been reached. 1.269 - * @param pos The source position at which to report the warning. 1.270 - * @param key The key for the localized warning message. 1.271 - * @param args Fields of the warning message. 1.272 - */ 1.273 - public void warning(String key, Object ... args) { 1.274 - report(diags.warning(source, null, key, args)); 1.275 - } 1.276 - 1.277 - /** Report a warning, unless suppressed by the -nowarn option or the 1.278 - * maximum number of warnings has been reached. 1.279 - * @param pos The source position at which to report the warning. 1.280 - * @param key The key for the localized warning message. 1.281 - * @param args Fields of the warning message. 1.282 - */ 1.283 - public void warning(DiagnosticPosition pos, String key, Object ... args) { 1.284 - report(diags.warning(source, pos, key, args)); 1.285 - } 1.286 - 1.287 - /** Report a warning, unless suppressed by the -nowarn option or the 1.288 - * maximum number of warnings has been reached. 1.289 - * @param pos The source position at which to report the warning. 1.290 - * @param key The key for the localized warning message. 1.291 - * @param args Fields of the warning message. 1.292 - */ 1.293 - public void warning(int pos, String key, Object ... args) { 1.294 - report(diags.warning(source, wrap(pos), key, args)); 1.295 - } 1.296 - 1.297 - /** Report a warning. 1.298 - * @param pos The source position at which to report the warning. 1.299 - * @param key The key for the localized warning message. 1.300 - * @param args Fields of the warning message. 1.301 - */ 1.302 - public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) { 1.303 - report(diags.mandatoryWarning(source, pos, key, args)); 1.304 + protected void directError(String key, Object... args) { 1.305 + printLines(errWriter, getLocalizedString(key, args)); 1.306 + errWriter.flush(); 1.307 } 1.308 1.309 /** Report a warning that cannot be suppressed. 1.310 @@ -478,74 +281,6 @@ 1.311 nwarnings++; 1.312 } 1.313 1.314 - /** Provide a non-fatal notification, unless suppressed by the -nowarn option. 1.315 - * @param key The key for the localized notification message. 1.316 - * @param args Fields of the notification message. 1.317 - */ 1.318 - public void note(String key, Object ... args) { 1.319 - report(diags.note(source, null, key, args)); 1.320 - } 1.321 - 1.322 - /** Provide a non-fatal notification, unless suppressed by the -nowarn option. 1.323 - * @param key The key for the localized notification message. 1.324 - * @param args Fields of the notification message. 1.325 - */ 1.326 - public void note(DiagnosticPosition pos, String key, Object ... args) { 1.327 - report(diags.note(source, pos, key, args)); 1.328 - } 1.329 - 1.330 - /** Provide a non-fatal notification, unless suppressed by the -nowarn option. 1.331 - * @param key The key for the localized notification message. 1.332 - * @param args Fields of the notification message. 1.333 - */ 1.334 - public void note(int pos, String key, Object ... args) { 1.335 - report(diags.note(source, wrap(pos), key, args)); 1.336 - } 1.337 - 1.338 - /** Provide a non-fatal notification, unless suppressed by the -nowarn option. 1.339 - * @param file The file to which the note applies. 1.340 - * @param key The key for the localized notification message. 1.341 - * @param args Fields of the notification message. 1.342 - */ 1.343 - public void note(JavaFileObject file, String key, Object ... args) { 1.344 - report(diags.note(wrap(file), null, key, args)); 1.345 - } 1.346 - 1.347 - /** Provide a non-fatal notification, unless suppressed by the -nowarn option. 1.348 - * @param key The key for the localized notification message. 1.349 - * @param args Fields of the notification message. 1.350 - */ 1.351 - public void mandatoryNote(final JavaFileObject file, String key, Object ... args) { 1.352 - report(diags.mandatoryNote(wrap(file), key, args)); 1.353 - } 1.354 - 1.355 - private JCDiagnostic.DiagnosticSource wrap(final JavaFileObject file) { 1.356 - if (file == null) { 1.357 - return null; 1.358 - } 1.359 - return new JCDiagnostic.DiagnosticSource() { 1.360 - public JavaFileObject getFile() { 1.361 - return file; 1.362 - } 1.363 - public CharSequence getName() { 1.364 - return JavacFileManager.getJavacBaseFileName(getFile()); 1.365 - } 1.366 - public int getLineNumber(int pos) { 1.367 - return Log.this.getLineNumber(pos); 1.368 - } 1.369 - public int getColumnNumber(int pos) { 1.370 - return Log.this.getColumnNumber(pos); 1.371 - } 1.372 - public Map<JCTree, Integer> getEndPosTable() { 1.373 - return (endPosTables == null ? null : endPosTables.get(file)); 1.374 - } 1.375 - }; 1.376 - } 1.377 - 1.378 - private DiagnosticPosition wrap(int pos) { 1.379 - return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos)); 1.380 - } 1.381 - 1.382 /** 1.383 * Common diagnostic handling. 1.384 * The diagnostic is counted, and depending on the options and how many diagnostics have been 1.385 @@ -657,12 +392,13 @@ 1.386 * and quick prototyping 1.387 ***************************************************************************/ 1.388 1.389 -/** print an error or warning message: 1.390 - */ 1.391 + /** print an error or warning message: 1.392 + */ 1.393 private void printRawError(int pos, String msg) { 1.394 - if (!findLine(pos)) { 1.395 + if (source == null || pos == Position.NOPOS) { 1.396 printLines(errWriter, "error: " + msg); 1.397 } else { 1.398 + int line = source.getLineNumber(pos); 1.399 JavaFileObject file = currentSource(); 1.400 if (file != null) 1.401 printLines(errWriter, 1.402 @@ -673,8 +409,8 @@ 1.403 errWriter.flush(); 1.404 } 1.405 1.406 -/** report an error: 1.407 - */ 1.408 + /** report an error: 1.409 + */ 1.410 public void rawError(int pos, String msg) { 1.411 if (nerrors < MaxErrors && shouldReport(currentSource(), pos)) { 1.412 printRawError(pos, msg); 1.413 @@ -684,8 +420,8 @@ 1.414 errWriter.flush(); 1.415 } 1.416 1.417 -/** report a warning: 1.418 - */ 1.419 + /** report a warning: 1.420 + */ 1.421 public void rawWarning(int pos, String msg) { 1.422 if (nwarnings < MaxWarnings && emitWarnings) { 1.423 printRawError(pos, "warning: " + msg); 1.424 @@ -695,36 +431,6 @@ 1.425 errWriter.flush(); 1.426 } 1.427 1.428 - /** Return the one-based line number associated with a given pos 1.429 - * for the current source file. Zero is returned if no line exists 1.430 - * for the given position. 1.431 - */ 1.432 - protected int getLineNumber(int pos) { 1.433 - if (findLine(pos)) 1.434 - return line; 1.435 - return 0; 1.436 - } 1.437 - 1.438 - /** Return the one-based column number associated with a given pos 1.439 - * for the current source file. Zero is returned if no column exists 1.440 - * for the given position. 1.441 - */ 1.442 - protected int getColumnNumber(int pos) { 1.443 - if (findLine(pos)) { 1.444 - int column = 0; 1.445 - for (bp = lineStart; bp < pos; bp++) { 1.446 - if (bp >= bufLen) 1.447 - return 0; 1.448 - if (buf[bp] == '\t') 1.449 - column = (column / TabInc * TabInc) + TabInc; 1.450 - else 1.451 - column++; 1.452 - } 1.453 - return column + 1; // positions are one-based 1.454 - } 1.455 - return 0; 1.456 - } 1.457 - 1.458 public static String format(String fmt, Object... args) { 1.459 return String.format((java.util.Locale)null, fmt, args); 1.460 }