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

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2008, 2013, 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 package com.sun.tools.javac.util;
aoqi@0 26
aoqi@0 27 import java.util.Arrays;
aoqi@0 28 import java.util.Collection;
aoqi@0 29 import java.util.EnumSet;
aoqi@0 30 import java.util.HashMap;
aoqi@0 31 import java.util.Locale;
aoqi@0 32 import java.util.Map;
aoqi@0 33 import java.util.Set;
aoqi@0 34
aoqi@0 35 import javax.tools.JavaFileObject;
aoqi@0 36
aoqi@0 37 import com.sun.tools.javac.api.DiagnosticFormatter;
aoqi@0 38 import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.DiagnosticPart;
aoqi@0 39 import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.MultilineLimit;
aoqi@0 40 import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind;
aoqi@0 41 import com.sun.tools.javac.api.Formattable;
aoqi@0 42 import com.sun.tools.javac.code.Lint.LintCategory;
aoqi@0 43 import com.sun.tools.javac.code.Printer;
aoqi@0 44 import com.sun.tools.javac.code.Symbol;
aoqi@0 45 import com.sun.tools.javac.code.Type;
aoqi@0 46 import com.sun.tools.javac.code.Type.CapturedType;
aoqi@0 47 import com.sun.tools.javac.file.BaseFileObject;
aoqi@0 48 import com.sun.tools.javac.jvm.Profile;
aoqi@0 49 import com.sun.tools.javac.tree.JCTree.*;
aoqi@0 50 import com.sun.tools.javac.tree.Pretty;
aoqi@0 51 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*;
aoqi@0 52
aoqi@0 53 /**
aoqi@0 54 * This abstract class provides a basic implementation of the functionalities that should be provided
aoqi@0 55 * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are:
aoqi@0 56 *
aoqi@0 57 * <ul>
aoqi@0 58 * <li> Provides a standard implementation of the visitor-like methods defined in the interface DiagnisticFormatter.
aoqi@0 59 * Those implementations are specifically targeting JCDiagnostic objects.
aoqi@0 60 * <li> Provides basic support for i18n and a method for executing all locale-dependent conversions
aoqi@0 61 * <li> Provides the formatting logic for rendering the arguments of a JCDiagnostic object.
aoqi@0 62 * <ul>
aoqi@0 63 *
aoqi@0 64 * <p><b>This is NOT part of any supported API.
aoqi@0 65 * If you write code that depends on this, you do so at your own risk.
aoqi@0 66 * This code and its internal interfaces are subject to change or
aoqi@0 67 * deletion without notice.</b>
aoqi@0 68 */
aoqi@0 69 public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter<JCDiagnostic> {
aoqi@0 70
aoqi@0 71 /**
aoqi@0 72 * JavacMessages object used by this formatter for i18n.
aoqi@0 73 */
aoqi@0 74 protected JavacMessages messages;
aoqi@0 75
aoqi@0 76 /**
aoqi@0 77 * Configuration object used by this formatter
aoqi@0 78 */
aoqi@0 79 private SimpleConfiguration config;
aoqi@0 80
aoqi@0 81 /**
aoqi@0 82 * Current depth level of the disgnostic being formatted
aoqi@0 83 * (!= 0 for subdiagnostics)
aoqi@0 84 */
aoqi@0 85 protected int depth = 0;
aoqi@0 86
aoqi@0 87 /**
aoqi@0 88 * All captured types that have been encountered during diagnostic formatting.
aoqi@0 89 * This info is used by the FormatterPrinter in order to print friendly unique
aoqi@0 90 * ids for captured types
aoqi@0 91 */
aoqi@0 92 private List<Type> allCaptured = List.nil();
aoqi@0 93
aoqi@0 94 /**
aoqi@0 95 * Initialize an AbstractDiagnosticFormatter by setting its JavacMessages object.
aoqi@0 96 * @param messages
aoqi@0 97 */
aoqi@0 98 protected AbstractDiagnosticFormatter(JavacMessages messages, SimpleConfiguration config) {
aoqi@0 99 this.messages = messages;
aoqi@0 100 this.config = config;
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 public String formatKind(JCDiagnostic d, Locale l) {
aoqi@0 104 switch (d.getType()) {
aoqi@0 105 case FRAGMENT: return "";
aoqi@0 106 case NOTE: return localize(l, "compiler.note.note");
aoqi@0 107 case WARNING: return localize(l, "compiler.warn.warning");
aoqi@0 108 case ERROR: return localize(l, "compiler.err.error");
aoqi@0 109 default:
aoqi@0 110 throw new AssertionError("Unknown diagnostic type: " + d.getType());
aoqi@0 111 }
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 @Override
aoqi@0 115 public String format(JCDiagnostic d, Locale locale) {
aoqi@0 116 allCaptured = List.nil();
aoqi@0 117 return formatDiagnostic(d, locale);
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 protected abstract String formatDiagnostic(JCDiagnostic d, Locale locale);
aoqi@0 121
aoqi@0 122 public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
aoqi@0 123 Assert.check(d.getPosition() != Position.NOPOS);
aoqi@0 124 return String.valueOf(getPosition(d, pk));
aoqi@0 125 }
aoqi@0 126 //where
aoqi@0 127 private long getPosition(JCDiagnostic d, PositionKind pk) {
aoqi@0 128 switch (pk) {
aoqi@0 129 case START: return d.getIntStartPosition();
aoqi@0 130 case END: return d.getIntEndPosition();
aoqi@0 131 case LINE: return d.getLineNumber();
aoqi@0 132 case COLUMN: return d.getColumnNumber();
aoqi@0 133 case OFFSET: return d.getIntPosition();
aoqi@0 134 default:
aoqi@0 135 throw new AssertionError("Unknown diagnostic position: " + pk);
aoqi@0 136 }
aoqi@0 137 }
aoqi@0 138
aoqi@0 139 public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
aoqi@0 140 JavaFileObject fo = d.getSource();
aoqi@0 141 if (fo == null)
aoqi@0 142 throw new IllegalArgumentException(); // d should have source set
aoqi@0 143 if (fullname)
aoqi@0 144 return fo.getName();
aoqi@0 145 else if (fo instanceof BaseFileObject)
aoqi@0 146 return ((BaseFileObject) fo).getShortName();
aoqi@0 147 else
aoqi@0 148 return BaseFileObject.getSimpleName(fo);
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 /**
aoqi@0 152 * Format the arguments of a given diagnostic.
aoqi@0 153 *
aoqi@0 154 * @param d diagnostic whose arguments are to be formatted
aoqi@0 155 * @param l locale object to be used for i18n
aoqi@0 156 * @return a Collection whose elements are the formatted arguments of the diagnostic
aoqi@0 157 */
aoqi@0 158 protected Collection<String> formatArguments(JCDiagnostic d, Locale l) {
aoqi@0 159 ListBuffer<String> buf = new ListBuffer<String>();
aoqi@0 160 for (Object o : d.getArgs()) {
aoqi@0 161 buf.append(formatArgument(d, o, l));
aoqi@0 162 }
aoqi@0 163 return buf.toList();
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 /**
aoqi@0 167 * Format a single argument of a given diagnostic.
aoqi@0 168 *
aoqi@0 169 * @param d diagnostic whose argument is to be formatted
aoqi@0 170 * @param arg argument to be formatted
aoqi@0 171 * @param l locale object to be used for i18n
aoqi@0 172 * @return string representation of the diagnostic argument
aoqi@0 173 */
aoqi@0 174 protected String formatArgument(JCDiagnostic d, Object arg, Locale l) {
aoqi@0 175 if (arg instanceof JCDiagnostic) {
aoqi@0 176 String s = null;
aoqi@0 177 depth++;
aoqi@0 178 try {
aoqi@0 179 s = formatMessage((JCDiagnostic)arg, l);
aoqi@0 180 }
aoqi@0 181 finally {
aoqi@0 182 depth--;
aoqi@0 183 }
aoqi@0 184 return s;
aoqi@0 185 }
aoqi@0 186 else if (arg instanceof JCExpression) {
aoqi@0 187 return expr2String((JCExpression)arg);
aoqi@0 188 }
aoqi@0 189 else if (arg instanceof Iterable<?>) {
aoqi@0 190 return formatIterable(d, (Iterable<?>)arg, l);
aoqi@0 191 }
aoqi@0 192 else if (arg instanceof Type) {
aoqi@0 193 return printer.visit((Type)arg, l);
aoqi@0 194 }
aoqi@0 195 else if (arg instanceof Symbol) {
aoqi@0 196 return printer.visit((Symbol)arg, l);
aoqi@0 197 }
aoqi@0 198 else if (arg instanceof JavaFileObject) {
aoqi@0 199 return ((JavaFileObject)arg).getName();
aoqi@0 200 }
aoqi@0 201 else if (arg instanceof Profile) {
aoqi@0 202 return ((Profile)arg).name;
aoqi@0 203 }
aoqi@0 204 else if (arg instanceof Formattable) {
aoqi@0 205 return ((Formattable)arg).toString(l, messages);
aoqi@0 206 }
aoqi@0 207 else {
aoqi@0 208 return String.valueOf(arg);
aoqi@0 209 }
aoqi@0 210 }
aoqi@0 211 //where
aoqi@0 212 private String expr2String(JCExpression tree) {
aoqi@0 213 switch(tree.getTag()) {
aoqi@0 214 case PARENS:
aoqi@0 215 return expr2String(((JCParens)tree).expr);
aoqi@0 216 case LAMBDA:
aoqi@0 217 case REFERENCE:
aoqi@0 218 case CONDEXPR:
aoqi@0 219 return Pretty.toSimpleString(tree);
aoqi@0 220 default:
aoqi@0 221 Assert.error("unexpected tree kind " + tree.getKind());
aoqi@0 222 return null;
aoqi@0 223 }
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 /**
aoqi@0 227 * Format an iterable argument of a given diagnostic.
aoqi@0 228 *
aoqi@0 229 * @param d diagnostic whose argument is to be formatted
aoqi@0 230 * @param it iterable argument to be formatted
aoqi@0 231 * @param l locale object to be used for i18n
aoqi@0 232 * @return string representation of the diagnostic iterable argument
aoqi@0 233 */
aoqi@0 234 protected String formatIterable(JCDiagnostic d, Iterable<?> it, Locale l) {
aoqi@0 235 StringBuilder sbuf = new StringBuilder();
aoqi@0 236 String sep = "";
aoqi@0 237 for (Object o : it) {
aoqi@0 238 sbuf.append(sep);
aoqi@0 239 sbuf.append(formatArgument(d, o, l));
aoqi@0 240 sep = ",";
aoqi@0 241 }
aoqi@0 242 return sbuf.toString();
aoqi@0 243 }
aoqi@0 244
aoqi@0 245 /**
aoqi@0 246 * Format all the subdiagnostics attached to a given diagnostic.
aoqi@0 247 *
aoqi@0 248 * @param d diagnostic whose subdiagnostics are to be formatted
aoqi@0 249 * @param l locale object to be used for i18n
aoqi@0 250 * @return list of all string representations of the subdiagnostics
aoqi@0 251 */
aoqi@0 252 protected List<String> formatSubdiagnostics(JCDiagnostic d, Locale l) {
aoqi@0 253 List<String> subdiagnostics = List.nil();
aoqi@0 254 int maxDepth = config.getMultilineLimit(MultilineLimit.DEPTH);
aoqi@0 255 if (maxDepth == -1 || depth < maxDepth) {
aoqi@0 256 depth++;
aoqi@0 257 try {
aoqi@0 258 int maxCount = config.getMultilineLimit(MultilineLimit.LENGTH);
aoqi@0 259 int count = 0;
aoqi@0 260 for (JCDiagnostic d2 : d.getSubdiagnostics()) {
aoqi@0 261 if (maxCount == -1 || count < maxCount) {
aoqi@0 262 subdiagnostics = subdiagnostics.append(formatSubdiagnostic(d, d2, l));
aoqi@0 263 count++;
aoqi@0 264 }
aoqi@0 265 else
aoqi@0 266 break;
aoqi@0 267 }
aoqi@0 268 }
aoqi@0 269 finally {
aoqi@0 270 depth--;
aoqi@0 271 }
aoqi@0 272 }
aoqi@0 273 return subdiagnostics;
aoqi@0 274 }
aoqi@0 275
aoqi@0 276 /**
aoqi@0 277 * Format a subdiagnostics attached to a given diagnostic.
aoqi@0 278 *
aoqi@0 279 * @param parent multiline diagnostic whose subdiagnostics is to be formatted
aoqi@0 280 * @param sub subdiagnostic to be formatted
aoqi@0 281 * @param l locale object to be used for i18n
aoqi@0 282 * @return string representation of the subdiagnostics
aoqi@0 283 */
aoqi@0 284 protected String formatSubdiagnostic(JCDiagnostic parent, JCDiagnostic sub, Locale l) {
aoqi@0 285 return formatMessage(sub, l);
aoqi@0 286 }
aoqi@0 287
aoqi@0 288 /** Format the faulty source code line and point to the error.
aoqi@0 289 * @param d The diagnostic for which the error line should be printed
aoqi@0 290 */
aoqi@0 291 protected String formatSourceLine(JCDiagnostic d, int nSpaces) {
aoqi@0 292 StringBuilder buf = new StringBuilder();
aoqi@0 293 DiagnosticSource source = d.getDiagnosticSource();
aoqi@0 294 int pos = d.getIntPosition();
aoqi@0 295 if (d.getIntPosition() == Position.NOPOS)
aoqi@0 296 throw new AssertionError();
aoqi@0 297 String line = (source == null ? null : source.getLine(pos));
aoqi@0 298 if (line == null)
aoqi@0 299 return "";
aoqi@0 300 buf.append(indent(line, nSpaces));
aoqi@0 301 int col = source.getColumnNumber(pos, false);
aoqi@0 302 if (config.isCaretEnabled()) {
aoqi@0 303 buf.append("\n");
aoqi@0 304 for (int i = 0; i < col - 1; i++) {
aoqi@0 305 buf.append((line.charAt(i) == '\t') ? "\t" : " ");
aoqi@0 306 }
aoqi@0 307 buf.append(indent("^", nSpaces));
aoqi@0 308 }
aoqi@0 309 return buf.toString();
aoqi@0 310 }
aoqi@0 311
aoqi@0 312 protected String formatLintCategory(JCDiagnostic d, Locale l) {
aoqi@0 313 LintCategory lc = d.getLintCategory();
aoqi@0 314 if (lc == null)
aoqi@0 315 return "";
aoqi@0 316 return localize(l, "compiler.warn.lintOption", lc.option);
aoqi@0 317 }
aoqi@0 318
aoqi@0 319 /**
aoqi@0 320 * Converts a String into a locale-dependent representation accordingly to a given locale.
aoqi@0 321 *
aoqi@0 322 * @param l locale object to be used for i18n
aoqi@0 323 * @param key locale-independent key used for looking up in a resource file
aoqi@0 324 * @param args localization arguments
aoqi@0 325 * @return a locale-dependent string
aoqi@0 326 */
aoqi@0 327 protected String localize(Locale l, String key, Object... args) {
aoqi@0 328 return messages.getLocalizedString(l, key, args);
aoqi@0 329 }
aoqi@0 330
aoqi@0 331 public boolean displaySource(JCDiagnostic d) {
aoqi@0 332 return config.getVisible().contains(DiagnosticPart.SOURCE) &&
aoqi@0 333 d.getType() != FRAGMENT &&
aoqi@0 334 d.getIntPosition() != Position.NOPOS;
aoqi@0 335 }
aoqi@0 336
aoqi@0 337 public boolean isRaw() {
aoqi@0 338 return false;
aoqi@0 339 }
aoqi@0 340
aoqi@0 341 /**
aoqi@0 342 * Creates a string with a given amount of empty spaces. Useful for
aoqi@0 343 * indenting the text of a diagnostic message.
aoqi@0 344 *
aoqi@0 345 * @param nSpaces the amount of spaces to be added to the result string
aoqi@0 346 * @return the indentation string
aoqi@0 347 */
aoqi@0 348 protected String indentString(int nSpaces) {
aoqi@0 349 String spaces = " ";
aoqi@0 350 if (nSpaces <= spaces.length())
aoqi@0 351 return spaces.substring(0, nSpaces);
aoqi@0 352 else {
aoqi@0 353 StringBuilder buf = new StringBuilder();
aoqi@0 354 for (int i = 0 ; i < nSpaces ; i++)
aoqi@0 355 buf.append(" ");
aoqi@0 356 return buf.toString();
aoqi@0 357 }
aoqi@0 358 }
aoqi@0 359
aoqi@0 360 /**
aoqi@0 361 * Indent a string by prepending a given amount of empty spaces to each line
aoqi@0 362 * of the string.
aoqi@0 363 *
aoqi@0 364 * @param s the string to be indented
aoqi@0 365 * @param nSpaces the amount of spaces that should be prepended to each line
aoqi@0 366 * of the string
aoqi@0 367 * @return an indented string
aoqi@0 368 */
aoqi@0 369 protected String indent(String s, int nSpaces) {
aoqi@0 370 String indent = indentString(nSpaces);
aoqi@0 371 StringBuilder buf = new StringBuilder();
aoqi@0 372 String nl = "";
aoqi@0 373 for (String line : s.split("\n")) {
aoqi@0 374 buf.append(nl);
aoqi@0 375 buf.append(indent + line);
aoqi@0 376 nl = "\n";
aoqi@0 377 }
aoqi@0 378 return buf.toString();
aoqi@0 379 }
aoqi@0 380
aoqi@0 381 public SimpleConfiguration getConfiguration() {
aoqi@0 382 return config;
aoqi@0 383 }
aoqi@0 384
aoqi@0 385 static public class SimpleConfiguration implements Configuration {
aoqi@0 386
aoqi@0 387 protected Map<MultilineLimit, Integer> multilineLimits;
aoqi@0 388 protected EnumSet<DiagnosticPart> visibleParts;
aoqi@0 389 protected boolean caretEnabled;
aoqi@0 390
aoqi@0 391 public SimpleConfiguration(Set<DiagnosticPart> parts) {
aoqi@0 392 multilineLimits = new HashMap<MultilineLimit, Integer>();
aoqi@0 393 setVisible(parts);
aoqi@0 394 setMultilineLimit(MultilineLimit.DEPTH, -1);
aoqi@0 395 setMultilineLimit(MultilineLimit.LENGTH, -1);
aoqi@0 396 setCaretEnabled(true);
aoqi@0 397 }
aoqi@0 398
aoqi@0 399 @SuppressWarnings("fallthrough")
aoqi@0 400 public SimpleConfiguration(Options options, Set<DiagnosticPart> parts) {
aoqi@0 401 this(parts);
aoqi@0 402 String showSource = null;
aoqi@0 403 if ((showSource = options.get("showSource")) != null) {
aoqi@0 404 if (showSource.equals("true"))
aoqi@0 405 setVisiblePart(DiagnosticPart.SOURCE, true);
aoqi@0 406 else if (showSource.equals("false"))
aoqi@0 407 setVisiblePart(DiagnosticPart.SOURCE, false);
aoqi@0 408 }
aoqi@0 409 String diagOpts = options.get("diags");
aoqi@0 410 if (diagOpts != null) {//override -XDshowSource
aoqi@0 411 Collection<String> args = Arrays.asList(diagOpts.split(","));
aoqi@0 412 if (args.contains("short")) {
aoqi@0 413 setVisiblePart(DiagnosticPart.DETAILS, false);
aoqi@0 414 setVisiblePart(DiagnosticPart.SUBDIAGNOSTICS, false);
aoqi@0 415 }
aoqi@0 416 if (args.contains("source"))
aoqi@0 417 setVisiblePart(DiagnosticPart.SOURCE, true);
aoqi@0 418 if (args.contains("-source"))
aoqi@0 419 setVisiblePart(DiagnosticPart.SOURCE, false);
aoqi@0 420 }
aoqi@0 421 String multiPolicy = null;
aoqi@0 422 if ((multiPolicy = options.get("multilinePolicy")) != null) {
aoqi@0 423 if (multiPolicy.equals("disabled"))
aoqi@0 424 setVisiblePart(DiagnosticPart.SUBDIAGNOSTICS, false);
aoqi@0 425 else if (multiPolicy.startsWith("limit:")) {
aoqi@0 426 String limitString = multiPolicy.substring("limit:".length());
aoqi@0 427 String[] limits = limitString.split(":");
aoqi@0 428 try {
aoqi@0 429 switch (limits.length) {
aoqi@0 430 case 2: {
aoqi@0 431 if (!limits[1].equals("*"))
aoqi@0 432 setMultilineLimit(MultilineLimit.DEPTH, Integer.parseInt(limits[1]));
aoqi@0 433 }
aoqi@0 434 case 1: {
aoqi@0 435 if (!limits[0].equals("*"))
aoqi@0 436 setMultilineLimit(MultilineLimit.LENGTH, Integer.parseInt(limits[0]));
aoqi@0 437 }
aoqi@0 438 }
aoqi@0 439 }
aoqi@0 440 catch(NumberFormatException ex) {
aoqi@0 441 setMultilineLimit(MultilineLimit.DEPTH, -1);
aoqi@0 442 setMultilineLimit(MultilineLimit.LENGTH, -1);
aoqi@0 443 }
aoqi@0 444 }
aoqi@0 445 }
aoqi@0 446 String showCaret = null;
aoqi@0 447 if (((showCaret = options.get("showCaret")) != null) &&
aoqi@0 448 showCaret.equals("false"))
aoqi@0 449 setCaretEnabled(false);
aoqi@0 450 else
aoqi@0 451 setCaretEnabled(true);
aoqi@0 452 }
aoqi@0 453
aoqi@0 454 public int getMultilineLimit(MultilineLimit limit) {
aoqi@0 455 return multilineLimits.get(limit);
aoqi@0 456 }
aoqi@0 457
aoqi@0 458 public EnumSet<DiagnosticPart> getVisible() {
aoqi@0 459 return EnumSet.copyOf(visibleParts);
aoqi@0 460 }
aoqi@0 461
aoqi@0 462 public void setMultilineLimit(MultilineLimit limit, int value) {
aoqi@0 463 multilineLimits.put(limit, value < -1 ? -1 : value);
aoqi@0 464 }
aoqi@0 465
aoqi@0 466
aoqi@0 467 public void setVisible(Set<DiagnosticPart> diagParts) {
aoqi@0 468 visibleParts = EnumSet.copyOf(diagParts);
aoqi@0 469 }
aoqi@0 470
aoqi@0 471 public void setVisiblePart(DiagnosticPart diagParts, boolean enabled) {
aoqi@0 472 if (enabled)
aoqi@0 473 visibleParts.add(diagParts);
aoqi@0 474 else
aoqi@0 475 visibleParts.remove(diagParts);
aoqi@0 476 }
aoqi@0 477
aoqi@0 478 /**
aoqi@0 479 * Shows a '^' sign under the source line displayed by the formatter
aoqi@0 480 * (if applicable).
aoqi@0 481 *
aoqi@0 482 * @param caretEnabled if true enables caret
aoqi@0 483 */
aoqi@0 484 public void setCaretEnabled(boolean caretEnabled) {
aoqi@0 485 this.caretEnabled = caretEnabled;
aoqi@0 486 }
aoqi@0 487
aoqi@0 488 /**
aoqi@0 489 * Tells whether the caret display is active or not.
aoqi@0 490 *
aoqi@0 491 * @return true if the caret is enabled
aoqi@0 492 */
aoqi@0 493 public boolean isCaretEnabled() {
aoqi@0 494 return caretEnabled;
aoqi@0 495 }
aoqi@0 496 }
aoqi@0 497
aoqi@0 498 public Printer getPrinter() {
aoqi@0 499 return printer;
aoqi@0 500 }
aoqi@0 501
aoqi@0 502 public void setPrinter(Printer printer) {
aoqi@0 503 this.printer = printer;
aoqi@0 504 }
aoqi@0 505
aoqi@0 506 /**
aoqi@0 507 * An enhanced printer for formatting types/symbols used by
aoqi@0 508 * AbstractDiagnosticFormatter. Provides alternate numbering of captured
aoqi@0 509 * types (they are numbered starting from 1 on each new diagnostic, instead
aoqi@0 510 * of relying on the underlying hashcode() method which generates unstable
aoqi@0 511 * output). Also detects cycles in wildcard messages (e.g. if the wildcard
aoqi@0 512 * type referred by a given captured type C contains C itself) which might
aoqi@0 513 * lead to infinite loops.
aoqi@0 514 */
aoqi@0 515 protected Printer printer = new Printer() {
aoqi@0 516
aoqi@0 517 @Override
aoqi@0 518 protected String localize(Locale locale, String key, Object... args) {
aoqi@0 519 return AbstractDiagnosticFormatter.this.localize(locale, key, args);
aoqi@0 520 }
aoqi@0 521 @Override
aoqi@0 522 protected String capturedVarId(CapturedType t, Locale locale) {
aoqi@0 523 return "" + (allCaptured.indexOf(t) + 1);
aoqi@0 524 }
aoqi@0 525 @Override
aoqi@0 526 public String visitCapturedType(CapturedType t, Locale locale) {
aoqi@0 527 if (!allCaptured.contains(t)) {
aoqi@0 528 allCaptured = allCaptured.append(t);
aoqi@0 529 }
aoqi@0 530 return super.visitCapturedType(t, locale);
aoqi@0 531 }
aoqi@0 532 };
aoqi@0 533 }

mercurial