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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1358
fc123bdeddb8
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial