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

Mon, 14 Mar 2011 11:42:15 -0700

author
jjg
date
Mon, 14 Mar 2011 11:42:15 -0700
changeset 929
e2890b8369f7
parent 798
4868a36f6fd8
child 1358
fc123bdeddb8
permissions
-rw-r--r--

7010608: the string 'error' should appear in error messages
Reviewed-by: mcimadamore

mcimadamore@83 1 /*
jjg@929 2 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
mcimadamore@83 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@83 4 *
mcimadamore@83 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@83 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
mcimadamore@83 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
mcimadamore@83 10 *
mcimadamore@83 11 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@83 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@83 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@83 14 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@83 15 * accompanied this code).
mcimadamore@83 16 *
mcimadamore@83 17 * You should have received a copy of the GNU General Public License version
mcimadamore@83 18 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@83 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@83 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
mcimadamore@83 24 */
mcimadamore@83 25
mcimadamore@83 26 package com.sun.tools.javac.util;
mcimadamore@83 27
mcimadamore@221 28 import java.util.Collection;
jjg@929 29 import java.util.EnumMap;
mcimadamore@221 30 import java.util.EnumSet;
mcimadamore@83 31 import java.util.HashMap;
mcimadamore@83 32 import java.util.Locale;
mcimadamore@83 33 import java.util.Map;
mcimadamore@221 34 import java.util.regex.Matcher;
mcimadamore@83 35 import javax.tools.JavaFileObject;
mcimadamore@83 36
mcimadamore@221 37 import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
mcimadamore@221 38 import com.sun.tools.javac.util.BasicDiagnosticFormatter.BasicConfiguration;
mcimadamore@221 39
mcimadamore@83 40 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
mcimadamore@221 41 import static com.sun.tools.javac.util.BasicDiagnosticFormatter.BasicConfiguration.*;
mcimadamore@221 42 import static com.sun.tools.javac.util.LayoutCharacters.*;
mcimadamore@83 43
mcimadamore@83 44 /**
mcimadamore@83 45 * A basic formatter for diagnostic messages.
mcimadamore@83 46 * The basic formatter will format a diagnostic according to one of three format patterns, depending on whether
mcimadamore@83 47 * or not the source name and position are set. The formatter supports a printf-like string for patterns
mcimadamore@83 48 * with the following special characters:
mcimadamore@83 49 * <ul>
mcimadamore@83 50 * <li>%b: the base of the source name
mcimadamore@83 51 * <li>%f: the source name (full absolute path)
mcimadamore@83 52 * <li>%l: the line number of the diagnostic, derived from the character offset
mcimadamore@83 53 * <li>%c: the column number of the diagnostic, derived from the character offset
mcimadamore@83 54 * <li>%o: the character offset of the diagnostic if set
mcimadamore@83 55 * <li>%p: the prefix for the diagnostic, derived from the diagnostic type
mcimadamore@83 56 * <li>%t: the prefix as it normally appears in standard diagnostics. In this case, no prefix is
mcimadamore@83 57 * shown if the type is ERROR and if a source name is set
mcimadamore@83 58 * <li>%m: the text or the diagnostic, including any appropriate arguments
mcimadamore@83 59 * <li>%_: space delimiter, useful for formatting purposes
mcimadamore@83 60 * </ul>
jjg@333 61 *
jjg@581 62 * <p><b>This is NOT part of any supported API.
jjg@333 63 * If you write code that depends on this, you do so at your own risk.
jjg@333 64 * This code and its internal interfaces are subject to change or
jjg@333 65 * deletion without notice.</b>
mcimadamore@83 66 */
mcimadamore@83 67 public class BasicDiagnosticFormatter extends AbstractDiagnosticFormatter {
mcimadamore@83 68
mcimadamore@83 69 /**
mcimadamore@83 70 * Create a basic formatter based on the supplied options.
mcimadamore@83 71 *
mcimadamore@83 72 * @param opts list of command-line options
mcimadamore@136 73 * @param msgs JavacMessages object used for i18n
mcimadamore@83 74 */
mcimadamore@221 75 public BasicDiagnosticFormatter(Options options, JavacMessages msgs) {
mcimadamore@221 76 super(msgs, new BasicConfiguration(options));
mcimadamore@83 77 }
mcimadamore@83 78
mcimadamore@83 79 /**
mcimadamore@83 80 * Create a standard basic formatter
mcimadamore@83 81 *
mcimadamore@136 82 * @param msgs JavacMessages object used for i18n
mcimadamore@83 83 */
mcimadamore@136 84 public BasicDiagnosticFormatter(JavacMessages msgs) {
mcimadamore@221 85 super(msgs, new BasicConfiguration());
mcimadamore@83 86 }
mcimadamore@83 87
mcimadamore@238 88 public String formatDiagnostic(JCDiagnostic d, Locale l) {
mcimadamore@136 89 if (l == null)
mcimadamore@136 90 l = messages.getCurrentLocale();
mcimadamore@83 91 String format = selectFormat(d);
mcimadamore@83 92 StringBuilder buf = new StringBuilder();
mcimadamore@83 93 for (int i = 0; i < format.length(); i++) {
mcimadamore@83 94 char c = format.charAt(i);
mcimadamore@83 95 boolean meta = false;
mcimadamore@83 96 if (c == '%' && i < format.length() - 1) {
mcimadamore@83 97 meta = true;
mcimadamore@83 98 c = format.charAt(++i);
mcimadamore@83 99 }
mcimadamore@83 100 buf.append(meta ? formatMeta(c, d, l) : String.valueOf(c));
mcimadamore@83 101 }
mcimadamore@221 102 if (depth == 0)
mcimadamore@221 103 return addSourceLineIfNeeded(d, buf.toString());
mcimadamore@221 104 else
mcimadamore@221 105 return buf.toString();
mcimadamore@221 106 }
mcimadamore@221 107
mcimadamore@221 108 public String formatMessage(JCDiagnostic d, Locale l) {
mcimadamore@689 109 int currentIndentation = 0;
mcimadamore@689 110 StringBuilder buf = new StringBuilder();
mcimadamore@689 111 Collection<String> args = formatArguments(d, l);
mcimadamore@689 112 String msg = localize(l, d.getCode(), args.toArray());
mcimadamore@689 113 String[] lines = msg.split("\n");
mcimadamore@689 114 if (getConfiguration().getVisible().contains(DiagnosticPart.SUMMARY)) {
mcimadamore@689 115 currentIndentation += getConfiguration().getIndentation(DiagnosticPart.SUMMARY);
mcimadamore@689 116 buf.append(indent(lines[0], currentIndentation)); //summary
mcimadamore@689 117 }
mcimadamore@689 118 if (lines.length > 1 && getConfiguration().getVisible().contains(DiagnosticPart.DETAILS)) {
mcimadamore@689 119 currentIndentation += getConfiguration().getIndentation(DiagnosticPart.DETAILS);
mcimadamore@689 120 for (int i = 1;i < lines.length; i++) {
mcimadamore@689 121 buf.append("\n" + indent(lines[i], currentIndentation));
mcimadamore@221 122 }
mcimadamore@689 123 }
mcimadamore@689 124 if (d.isMultiline() && getConfiguration().getVisible().contains(DiagnosticPart.SUBDIAGNOSTICS)) {
mcimadamore@689 125 currentIndentation += getConfiguration().getIndentation(DiagnosticPart.SUBDIAGNOSTICS);
mcimadamore@689 126 for (String sub : formatSubdiagnostics(d, l)) {
mcimadamore@689 127 buf.append("\n" + indent(sub, currentIndentation));
mcimadamore@221 128 }
mcimadamore@137 129 }
mcimadamore@689 130 return buf.toString();
mcimadamore@221 131 }
mcimadamore@221 132
mcimadamore@221 133 protected String addSourceLineIfNeeded(JCDiagnostic d, String msg) {
mcimadamore@221 134 if (!displaySource(d))
mcimadamore@221 135 return msg;
mcimadamore@221 136 else {
mcimadamore@221 137 BasicConfiguration conf = getConfiguration();
mcimadamore@221 138 int indentSource = conf.getIndentation(DiagnosticPart.SOURCE);
mcimadamore@221 139 String sourceLine = "\n" + formatSourceLine(d, indentSource);
mcimadamore@221 140 boolean singleLine = msg.indexOf("\n") == -1;
mcimadamore@221 141 if (singleLine || getConfiguration().getSourcePosition() == SourcePosition.BOTTOM)
mcimadamore@221 142 return msg + sourceLine;
mcimadamore@221 143 else
mcimadamore@221 144 return msg.replaceFirst("\n", Matcher.quoteReplacement(sourceLine) + "\n");
mcimadamore@221 145 }
mcimadamore@83 146 }
mcimadamore@83 147
mcimadamore@83 148 protected String formatMeta(char c, JCDiagnostic d, Locale l) {
mcimadamore@83 149 switch (c) {
mcimadamore@83 150 case 'b':
mcimadamore@100 151 return formatSource(d, false, l);
mcimadamore@83 152 case 'e':
mcimadamore@83 153 return formatPosition(d, END, l);
mcimadamore@83 154 case 'f':
mcimadamore@100 155 return formatSource(d, true, l);
mcimadamore@83 156 case 'l':
mcimadamore@83 157 return formatPosition(d, LINE, l);
mcimadamore@83 158 case 'c':
mcimadamore@83 159 return formatPosition(d, COLUMN, l);
mcimadamore@83 160 case 'o':
mcimadamore@83 161 return formatPosition(d, OFFSET, l);
mcimadamore@83 162 case 'p':
mcimadamore@83 163 return formatKind(d, l);
mcimadamore@83 164 case 's':
mcimadamore@83 165 return formatPosition(d, START, l);
mcimadamore@83 166 case 't': {
mcimadamore@83 167 boolean usePrefix;
mcimadamore@83 168 switch (d.getType()) {
mcimadamore@83 169 case FRAGMENT:
mcimadamore@83 170 usePrefix = false;
mcimadamore@83 171 break;
mcimadamore@83 172 case ERROR:
mcimadamore@83 173 usePrefix = (d.getIntPosition() == Position.NOPOS);
mcimadamore@83 174 break;
mcimadamore@83 175 default:
mcimadamore@83 176 usePrefix = true;
mcimadamore@83 177 }
mcimadamore@83 178 if (usePrefix)
mcimadamore@83 179 return formatKind(d, l);
mcimadamore@83 180 else
mcimadamore@83 181 return "";
mcimadamore@83 182 }
mcimadamore@83 183 case 'm':
mcimadamore@83 184 return formatMessage(d, l);
jjg@612 185 case 'L':
jjg@612 186 return formatLintCategory(d, l);
mcimadamore@83 187 case '_':
mcimadamore@83 188 return " ";
mcimadamore@83 189 case '%':
mcimadamore@83 190 return "%";
mcimadamore@83 191 default:
mcimadamore@83 192 return String.valueOf(c);
mcimadamore@83 193 }
mcimadamore@83 194 }
mcimadamore@83 195
mcimadamore@83 196 private String selectFormat(JCDiagnostic d) {
mcimadamore@83 197 DiagnosticSource source = d.getDiagnosticSource();
mcimadamore@221 198 String format = getConfiguration().getFormat(BasicFormatKind.DEFAULT_NO_POS_FORMAT);
jjg@519 199 if (source != null && source != DiagnosticSource.NO_SOURCE) {
mcimadamore@83 200 if (d.getIntPosition() != Position.NOPOS) {
mcimadamore@221 201 format = getConfiguration().getFormat(BasicFormatKind.DEFAULT_POS_FORMAT);
mcimadamore@83 202 } else if (source.getFile() != null &&
mcimadamore@83 203 source.getFile().getKind() == JavaFileObject.Kind.CLASS) {
mcimadamore@221 204 format = getConfiguration().getFormat(BasicFormatKind.DEFAULT_CLASS_FORMAT);
mcimadamore@83 205 }
mcimadamore@83 206 }
mcimadamore@83 207 return format;
mcimadamore@83 208 }
mcimadamore@83 209
mcimadamore@221 210 @Override
mcimadamore@221 211 public BasicConfiguration getConfiguration() {
mcimadamore@288 212 //the following cast is always safe - see init
mcimadamore@221 213 return (BasicConfiguration)super.getConfiguration();
mcimadamore@221 214 }
mcimadamore@221 215
mcimadamore@221 216 static public class BasicConfiguration extends SimpleConfiguration {
mcimadamore@221 217
mcimadamore@221 218 protected Map<DiagnosticPart, Integer> indentationLevels;
mcimadamore@221 219 protected Map<BasicFormatKind, String> availableFormats;
mcimadamore@221 220 protected SourcePosition sourcePosition;
mcimadamore@221 221
mcimadamore@221 222 @SuppressWarnings("fallthrough")
mcimadamore@221 223 public BasicConfiguration(Options options) {
mcimadamore@221 224 super(options, EnumSet.of(DiagnosticPart.SUMMARY,
mcimadamore@221 225 DiagnosticPart.DETAILS,
mcimadamore@221 226 DiagnosticPart.SUBDIAGNOSTICS,
mcimadamore@221 227 DiagnosticPart.SOURCE));
mcimadamore@221 228 initFormat();
mcimadamore@221 229 initIndentation();
jjg@929 230 if (options.isSet("oldDiags"))
jjg@929 231 initOldFormat();
mcimadamore@221 232 String fmt = options.get("diagsFormat");
mcimadamore@221 233 if (fmt != null) {
jjg@929 234 if (fmt.equals("OLD"))
jjg@929 235 initOldFormat();
jjg@929 236 else
jjg@929 237 initFormats(fmt);
mcimadamore@221 238 }
jjg@612 239 String srcPos = null;
jjg@612 240 if ((((srcPos = options.get("sourcePosition")) != null)) &&
jjg@612 241 srcPos.equals("bottom"))
mcimadamore@221 242 setSourcePosition(SourcePosition.BOTTOM);
mcimadamore@221 243 else
mcimadamore@221 244 setSourcePosition(SourcePosition.AFTER_SUMMARY);
mcimadamore@221 245 String indent = options.get("diagsIndentation");
mcimadamore@221 246 if (indent != null) {
mcimadamore@221 247 String[] levels = indent.split("\\|");
mcimadamore@221 248 try {
mcimadamore@221 249 switch (levels.length) {
mcimadamore@221 250 case 5:
mcimadamore@221 251 setIndentation(DiagnosticPart.JLS,
mcimadamore@221 252 Integer.parseInt(levels[4]));
mcimadamore@221 253 case 4:
mcimadamore@221 254 setIndentation(DiagnosticPart.SUBDIAGNOSTICS,
mcimadamore@221 255 Integer.parseInt(levels[3]));
mcimadamore@221 256 case 3:
mcimadamore@221 257 setIndentation(DiagnosticPart.SOURCE,
mcimadamore@221 258 Integer.parseInt(levels[2]));
mcimadamore@221 259 case 2:
mcimadamore@221 260 setIndentation(DiagnosticPart.DETAILS,
mcimadamore@221 261 Integer.parseInt(levels[1]));
mcimadamore@221 262 default:
mcimadamore@221 263 setIndentation(DiagnosticPart.SUMMARY,
mcimadamore@221 264 Integer.parseInt(levels[0]));
mcimadamore@221 265 }
mcimadamore@221 266 }
mcimadamore@221 267 catch (NumberFormatException ex) {
mcimadamore@221 268 initIndentation();
mcimadamore@221 269 }
mcimadamore@221 270 }
mcimadamore@221 271 }
mcimadamore@221 272
mcimadamore@221 273 public BasicConfiguration() {
mcimadamore@221 274 super(EnumSet.of(DiagnosticPart.SUMMARY,
mcimadamore@221 275 DiagnosticPart.DETAILS,
mcimadamore@221 276 DiagnosticPart.SUBDIAGNOSTICS,
mcimadamore@221 277 DiagnosticPart.SOURCE));
mcimadamore@221 278 initFormat();
mcimadamore@221 279 initIndentation();
mcimadamore@221 280 }
jjg@929 281
mcimadamore@221 282 private void initFormat() {
jjg@929 283 initFormats("%f:%l:%_%p%L%m", "%p%L%m", "%f:%_%p%L%m");
mcimadamore@221 284 }
jjg@929 285
jjg@929 286 private void initOldFormat() {
jjg@929 287 initFormats("%f:%l:%_%t%L%m", "%p%L%m", "%f:%_%t%L%m");
jjg@929 288 }
jjg@929 289
jjg@929 290 private void initFormats(String pos, String nopos, String clazz) {
jjg@929 291 availableFormats = new EnumMap<BasicFormatKind, String>(BasicFormatKind.class);
jjg@929 292 setFormat(BasicFormatKind.DEFAULT_POS_FORMAT, pos);
jjg@929 293 setFormat(BasicFormatKind.DEFAULT_NO_POS_FORMAT, nopos);
jjg@929 294 setFormat(BasicFormatKind.DEFAULT_CLASS_FORMAT, clazz);
jjg@929 295 }
jjg@929 296
jjg@929 297 @SuppressWarnings("fallthrough")
jjg@929 298 private void initFormats(String fmt) {
jjg@929 299 String[] formats = fmt.split("\\|");
jjg@929 300 switch (formats.length) {
jjg@929 301 case 3:
jjg@929 302 setFormat(BasicFormatKind.DEFAULT_CLASS_FORMAT, formats[2]);
jjg@929 303 case 2:
jjg@929 304 setFormat(BasicFormatKind.DEFAULT_NO_POS_FORMAT, formats[1]);
jjg@929 305 default:
jjg@929 306 setFormat(BasicFormatKind.DEFAULT_POS_FORMAT, formats[0]);
jjg@929 307 }
jjg@929 308 }
jjg@929 309
mcimadamore@221 310 private void initIndentation() {
mcimadamore@221 311 indentationLevels = new HashMap<DiagnosticPart, Integer>();
mcimadamore@221 312 setIndentation(DiagnosticPart.SUMMARY, 0);
mcimadamore@221 313 setIndentation(DiagnosticPart.DETAILS, DetailsInc);
mcimadamore@221 314 setIndentation(DiagnosticPart.SUBDIAGNOSTICS, DiagInc);
mcimadamore@221 315 setIndentation(DiagnosticPart.SOURCE, 0);
mcimadamore@221 316 }
mcimadamore@221 317
mcimadamore@83 318 /**
mcimadamore@221 319 * Get the amount of spaces for a given indentation kind
mcimadamore@221 320 * @param diagPart the diagnostic part for which the indentation is
mcimadamore@221 321 * to be retrieved
mcimadamore@221 322 * @return the amount of spaces used for the specified indentation kind
mcimadamore@221 323 */
mcimadamore@221 324 public int getIndentation(DiagnosticPart diagPart) {
mcimadamore@221 325 return indentationLevels.get(diagPart);
mcimadamore@221 326 }
mcimadamore@221 327
mcimadamore@83 328 /**
mcimadamore@221 329 * Set the indentation level for various element of a given diagnostic -
mcimadamore@221 330 * this might lead to more readable diagnostics
mcimadamore@221 331 *
mcimadamore@221 332 * @param indentationKind kind of indentation to be set
mcimadamore@221 333 * @param nSpaces amount of spaces for the specified diagnostic part
mcimadamore@221 334 */
mcimadamore@221 335 public void setIndentation(DiagnosticPart diagPart, int nSpaces) {
mcimadamore@221 336 indentationLevels.put(diagPart, nSpaces);
mcimadamore@221 337 }
mcimadamore@221 338
mcimadamore@83 339 /**
mcimadamore@221 340 * Set the source line positioning used by this formatter
mcimadamore@221 341 *
mcimadamore@221 342 * @param sourcePos a positioning value for source line
mcimadamore@221 343 */
mcimadamore@221 344 public void setSourcePosition(SourcePosition sourcePos) {
mcimadamore@221 345 sourcePosition = sourcePos;
mcimadamore@221 346 }
mcimadamore@221 347
mcimadamore@221 348 /**
mcimadamore@221 349 * Get the source line positioning used by this formatter
mcimadamore@221 350 *
mcimadamore@221 351 * @return the positioning value used by this formatter
mcimadamore@221 352 */
mcimadamore@221 353 public SourcePosition getSourcePosition() {
mcimadamore@221 354 return sourcePosition;
mcimadamore@221 355 }
mcimadamore@221 356 //where
mcimadamore@221 357 /**
mcimadamore@221 358 * A source positioning value controls the position (within a given
mcimadamore@221 359 * diagnostic message) in which the source line the diagnostic refers to
mcimadamore@221 360 * should be displayed (if applicable)
mcimadamore@221 361 */
mcimadamore@221 362 public enum SourcePosition {
mcimadamore@221 363 /**
mcimadamore@221 364 * Source line is displayed after the diagnostic message
mcimadamore@221 365 */
mcimadamore@221 366 BOTTOM,
mcimadamore@221 367 /**
mcimadamore@221 368 * Source line is displayed after the first line of the diagnostic
mcimadamore@221 369 * message
mcimadamore@221 370 */
mcimadamore@221 371 AFTER_SUMMARY;
mcimadamore@221 372 }
mcimadamore@221 373
mcimadamore@221 374 /**
mcimadamore@221 375 * Set a metachar string for a specific format
mcimadamore@221 376 *
mcimadamore@221 377 * @param kind the format kind to be set
mcimadamore@221 378 * @param s the metachar string specifying the format
mcimadamore@221 379 */
mcimadamore@221 380 public void setFormat(BasicFormatKind kind, String s) {
mcimadamore@221 381 availableFormats.put(kind, s);
mcimadamore@221 382 }
mcimadamore@221 383
mcimadamore@221 384 /**
mcimadamore@221 385 * Get a metachar string for a specific format
mcimadamore@221 386 *
mcimadamore@221 387 * @param sourcePos a positioning value for source line
mcimadamore@221 388 */
mcimadamore@221 389 public String getFormat(BasicFormatKind kind) {
mcimadamore@221 390 return availableFormats.get(kind);
mcimadamore@221 391 }
mcimadamore@221 392 //where
mcimadamore@221 393 /**
mcimadamore@221 394 * This enum contains all the kinds of formatting patterns supported
mcimadamore@221 395 * by a basic diagnostic formatter.
mcimadamore@221 396 */
mcimadamore@221 397 public enum BasicFormatKind {
mcimadamore@221 398 /**
mcimadamore@221 399 * A format string to be used for diagnostics with a given position.
mcimadamore@221 400 */
mcimadamore@221 401 DEFAULT_POS_FORMAT,
mcimadamore@221 402 /**
mcimadamore@221 403 * A format string to be used for diagnostics without a given position.
mcimadamore@221 404 */
mcimadamore@221 405 DEFAULT_NO_POS_FORMAT,
mcimadamore@221 406 /**
mcimadamore@221 407 * A format string to be used for diagnostics regarding classfiles
mcimadamore@221 408 */
mcimadamore@221 409 DEFAULT_CLASS_FORMAT;
mcimadamore@221 410 }
mcimadamore@83 411 }
mcimadamore@83 412 }

mercurial