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

Thu, 24 May 2018 16:48:51 +0800

author
aoqi
date
Thu, 24 May 2018 16:48:51 +0800
changeset 3295
859dc787b52b
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2009, 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.EnumMap;
aoqi@0 28 import java.util.EnumSet;
aoqi@0 29 import java.util.HashMap;
aoqi@0 30 import java.util.LinkedHashMap;
aoqi@0 31 import java.util.Locale;
aoqi@0 32 import java.util.Map;
aoqi@0 33
aoqi@0 34 import com.sun.tools.javac.code.Kinds;
aoqi@0 35 import com.sun.tools.javac.code.Printer;
aoqi@0 36 import com.sun.tools.javac.code.Symbol;
aoqi@0 37 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 38 import com.sun.tools.javac.code.Symtab;
aoqi@0 39 import com.sun.tools.javac.code.Type;
aoqi@0 40 import com.sun.tools.javac.code.Type.*;
aoqi@0 41 import com.sun.tools.javac.code.Types;
aoqi@0 42
aoqi@0 43 import static com.sun.tools.javac.code.TypeTag.*;
aoqi@0 44 import static com.sun.tools.javac.code.Flags.*;
aoqi@0 45 import static com.sun.tools.javac.util.LayoutCharacters.*;
aoqi@0 46 import static com.sun.tools.javac.util.RichDiagnosticFormatter.RichConfiguration.*;
aoqi@0 47
aoqi@0 48 /**
aoqi@0 49 * A rich diagnostic formatter is a formatter that provides better integration
aoqi@0 50 * with javac's type system. A diagostic is first preprocessed in order to keep
aoqi@0 51 * track of each types/symbols in it; after these informations are collected,
aoqi@0 52 * the diagnostic is rendered using a standard formatter, whose type/symbol printer
aoqi@0 53 * has been replaced by a more refined version provided by this rich formatter.
aoqi@0 54 * The rich formatter currently enables three different features: (i) simple class
aoqi@0 55 * names - that is class names are displayed used a non qualified name (thus
aoqi@0 56 * omitting package info) whenever possible - (ii) where clause list - a list of
aoqi@0 57 * additional subdiagnostics that provide specific info about type-variables,
aoqi@0 58 * captured types, intersection types that occur in the diagnostic that is to be
aoqi@0 59 * formatted and (iii) type-variable disambiguation - when the diagnostic refers
aoqi@0 60 * to two different type-variables with the same name, their representation is
aoqi@0 61 * disambiguated by appending an index to the type variable name.
aoqi@0 62 *
aoqi@0 63 * <p><b>This is NOT part of any supported API.
aoqi@0 64 * If you write code that depends on this, you do so at your own risk.
aoqi@0 65 * This code and its internal interfaces are subject to change or
aoqi@0 66 * deletion without notice.</b>
aoqi@0 67 */
aoqi@0 68 public class RichDiagnosticFormatter extends
aoqi@0 69 ForwardingDiagnosticFormatter<JCDiagnostic, AbstractDiagnosticFormatter> {
aoqi@0 70
aoqi@0 71 final Symtab syms;
aoqi@0 72 final Types types;
aoqi@0 73 final JCDiagnostic.Factory diags;
aoqi@0 74 final JavacMessages messages;
aoqi@0 75
aoqi@0 76 /* name simplifier used by this formatter */
aoqi@0 77 protected ClassNameSimplifier nameSimplifier;
aoqi@0 78
aoqi@0 79 /* type/symbol printer used by this formatter */
aoqi@0 80 private RichPrinter printer;
aoqi@0 81
aoqi@0 82 /* map for keeping track of a where clause associated to a given type */
aoqi@0 83 Map<WhereClauseKind, Map<Type, JCDiagnostic>> whereClauses;
aoqi@0 84
aoqi@0 85 /** Get the DiagnosticFormatter instance for this context. */
aoqi@0 86 public static RichDiagnosticFormatter instance(Context context) {
aoqi@0 87 RichDiagnosticFormatter instance = context.get(RichDiagnosticFormatter.class);
aoqi@0 88 if (instance == null)
aoqi@0 89 instance = new RichDiagnosticFormatter(context);
aoqi@0 90 return instance;
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 protected RichDiagnosticFormatter(Context context) {
aoqi@0 94 super((AbstractDiagnosticFormatter)Log.instance(context).getDiagnosticFormatter());
aoqi@0 95 setRichPrinter(new RichPrinter());
aoqi@0 96 this.syms = Symtab.instance(context);
aoqi@0 97 this.diags = JCDiagnostic.Factory.instance(context);
aoqi@0 98 this.types = Types.instance(context);
aoqi@0 99 this.messages = JavacMessages.instance(context);
aoqi@0 100 whereClauses = new EnumMap<WhereClauseKind, Map<Type, JCDiagnostic>>(WhereClauseKind.class);
aoqi@0 101 configuration = new RichConfiguration(Options.instance(context), formatter);
aoqi@0 102 for (WhereClauseKind kind : WhereClauseKind.values())
aoqi@0 103 whereClauses.put(kind, new LinkedHashMap<Type, JCDiagnostic>());
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 @Override
aoqi@0 107 public String format(JCDiagnostic diag, Locale l) {
aoqi@0 108 StringBuilder sb = new StringBuilder();
aoqi@0 109 nameSimplifier = new ClassNameSimplifier();
aoqi@0 110 for (WhereClauseKind kind : WhereClauseKind.values())
aoqi@0 111 whereClauses.get(kind).clear();
aoqi@0 112 preprocessDiagnostic(diag);
aoqi@0 113 sb.append(formatter.format(diag, l));
aoqi@0 114 if (getConfiguration().isEnabled(RichFormatterFeature.WHERE_CLAUSES)) {
aoqi@0 115 List<JCDiagnostic> clauses = getWhereClauses();
aoqi@0 116 String indent = formatter.isRaw() ? "" :
aoqi@0 117 formatter.indentString(DetailsInc);
aoqi@0 118 for (JCDiagnostic d : clauses) {
aoqi@0 119 String whereClause = formatter.format(d, l);
aoqi@0 120 if (whereClause.length() > 0) {
aoqi@0 121 sb.append('\n' + indent + whereClause);
aoqi@0 122 }
aoqi@0 123 }
aoqi@0 124 }
aoqi@0 125 return sb.toString();
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 @Override
aoqi@0 129 public String formatMessage(JCDiagnostic diag, Locale l) {
aoqi@0 130 nameSimplifier = new ClassNameSimplifier();
aoqi@0 131 preprocessDiagnostic(diag);
aoqi@0 132 return super.formatMessage(diag, l);
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 /**
aoqi@0 136 * Sets the type/symbol printer used by this formatter.
aoqi@0 137 * @param printer the rich printer to be set
aoqi@0 138 */
aoqi@0 139 protected void setRichPrinter(RichPrinter printer) {
aoqi@0 140 this.printer = printer;
aoqi@0 141 formatter.setPrinter(printer);
aoqi@0 142 }
aoqi@0 143
aoqi@0 144 /**
aoqi@0 145 * Gets the type/symbol printer used by this formatter.
aoqi@0 146 * @return type/symbol rich printer
aoqi@0 147 */
aoqi@0 148 protected RichPrinter getRichPrinter() {
aoqi@0 149 return printer;
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 /**
aoqi@0 153 * Preprocess a given diagnostic by looking both into its arguments and into
aoqi@0 154 * its subdiagnostics (if any). This preprocessing is responsible for
aoqi@0 155 * generating info corresponding to features like where clauses, name
aoqi@0 156 * simplification, etc.
aoqi@0 157 *
aoqi@0 158 * @param diag the diagnostic to be preprocessed
aoqi@0 159 */
aoqi@0 160 protected void preprocessDiagnostic(JCDiagnostic diag) {
aoqi@0 161 for (Object o : diag.getArgs()) {
aoqi@0 162 if (o != null) {
aoqi@0 163 preprocessArgument(o);
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166 if (diag.isMultiline()) {
aoqi@0 167 for (JCDiagnostic d : diag.getSubdiagnostics())
aoqi@0 168 preprocessDiagnostic(d);
aoqi@0 169 }
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 /**
aoqi@0 173 * Preprocess a diagnostic argument. A type/symbol argument is
aoqi@0 174 * preprocessed by specialized type/symbol preprocessors.
aoqi@0 175 *
aoqi@0 176 * @param arg the argument to be translated
aoqi@0 177 */
aoqi@0 178 protected void preprocessArgument(Object arg) {
aoqi@0 179 if (arg instanceof Type) {
aoqi@0 180 preprocessType((Type)arg);
aoqi@0 181 }
aoqi@0 182 else if (arg instanceof Symbol) {
aoqi@0 183 preprocessSymbol((Symbol)arg);
aoqi@0 184 }
aoqi@0 185 else if (arg instanceof JCDiagnostic) {
aoqi@0 186 preprocessDiagnostic((JCDiagnostic)arg);
aoqi@0 187 }
aoqi@0 188 else if (arg instanceof Iterable<?>) {
aoqi@0 189 for (Object o : (Iterable<?>)arg) {
aoqi@0 190 preprocessArgument(o);
aoqi@0 191 }
aoqi@0 192 }
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 /**
aoqi@0 196 * Build a list of multiline diagnostics containing detailed info about
aoqi@0 197 * type-variables, captured types, and intersection types
aoqi@0 198 *
aoqi@0 199 * @return where clause list
aoqi@0 200 */
aoqi@0 201 protected List<JCDiagnostic> getWhereClauses() {
aoqi@0 202 List<JCDiagnostic> clauses = List.nil();
aoqi@0 203 for (WhereClauseKind kind : WhereClauseKind.values()) {
aoqi@0 204 List<JCDiagnostic> lines = List.nil();
aoqi@0 205 for (Map.Entry<Type, JCDiagnostic> entry : whereClauses.get(kind).entrySet()) {
aoqi@0 206 lines = lines.prepend(entry.getValue());
aoqi@0 207 }
aoqi@0 208 if (!lines.isEmpty()) {
aoqi@0 209 String key = kind.key();
aoqi@0 210 if (lines.size() > 1)
aoqi@0 211 key += ".1";
aoqi@0 212 JCDiagnostic d = diags.fragment(key, whereClauses.get(kind).keySet());
aoqi@0 213 d = new JCDiagnostic.MultilineDiagnostic(d, lines.reverse());
aoqi@0 214 clauses = clauses.prepend(d);
aoqi@0 215 }
aoqi@0 216 }
aoqi@0 217 return clauses.reverse();
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 private int indexOf(Type type, WhereClauseKind kind) {
aoqi@0 221 int index = 1;
aoqi@0 222 for (Type t : whereClauses.get(kind).keySet()) {
aoqi@0 223 if (t.tsym == type.tsym) {
aoqi@0 224 return index;
aoqi@0 225 }
aoqi@0 226 if (kind != WhereClauseKind.TYPEVAR ||
aoqi@0 227 t.toString().equals(type.toString())) {
aoqi@0 228 index++;
aoqi@0 229 }
aoqi@0 230 }
aoqi@0 231 return -1;
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 private boolean unique(TypeVar typevar) {
aoqi@0 235 int found = 0;
aoqi@0 236 for (Type t : whereClauses.get(WhereClauseKind.TYPEVAR).keySet()) {
aoqi@0 237 if (t.toString().equals(typevar.toString())) {
aoqi@0 238 found++;
aoqi@0 239 }
aoqi@0 240 }
aoqi@0 241 if (found < 1)
aoqi@0 242 throw new AssertionError("Missing type variable in where clause " + typevar);
aoqi@0 243 return found == 1;
aoqi@0 244 }
aoqi@0 245 //where
aoqi@0 246 /**
aoqi@0 247 * This enum defines all posssible kinds of where clauses that can be
aoqi@0 248 * attached by a rich diagnostic formatter to a given diagnostic
aoqi@0 249 */
aoqi@0 250 enum WhereClauseKind {
aoqi@0 251
aoqi@0 252 /** where clause regarding a type variable */
aoqi@0 253 TYPEVAR("where.description.typevar"),
aoqi@0 254 /** where clause regarding a captured type */
aoqi@0 255 CAPTURED("where.description.captured"),
aoqi@0 256 /** where clause regarding an intersection type */
aoqi@0 257 INTERSECTION("where.description.intersection");
aoqi@0 258
aoqi@0 259 /** resource key for this where clause kind */
aoqi@0 260 private final String key;
aoqi@0 261
aoqi@0 262 WhereClauseKind(String key) {
aoqi@0 263 this.key = key;
aoqi@0 264 }
aoqi@0 265
aoqi@0 266 String key() {
aoqi@0 267 return key;
aoqi@0 268 }
aoqi@0 269 }
aoqi@0 270
aoqi@0 271 // <editor-fold defaultstate="collapsed" desc="name simplifier">
aoqi@0 272 /**
aoqi@0 273 * A name simplifier keeps track of class names usages in order to determine
aoqi@0 274 * whether a class name can be compacted or not. Short names are not used
aoqi@0 275 * if a conflict is detected, e.g. when two classes with the same simple
aoqi@0 276 * name belong to different packages - in this case the formatter reverts
aoqi@0 277 * to fullnames as compact names might lead to a confusing diagnostic.
aoqi@0 278 */
aoqi@0 279 protected class ClassNameSimplifier {
aoqi@0 280
aoqi@0 281 /* table for keeping track of all short name usages */
aoqi@0 282 Map<Name, List<Symbol>> nameClashes = new HashMap<Name, List<Symbol>>();
aoqi@0 283
aoqi@0 284 /**
aoqi@0 285 * Add a name usage to the simplifier's internal cache
aoqi@0 286 */
aoqi@0 287 protected void addUsage(Symbol sym) {
aoqi@0 288 Name n = sym.getSimpleName();
aoqi@0 289 List<Symbol> conflicts = nameClashes.get(n);
aoqi@0 290 if (conflicts == null) {
aoqi@0 291 conflicts = List.nil();
aoqi@0 292 }
aoqi@0 293 if (!conflicts.contains(sym))
aoqi@0 294 nameClashes.put(n, conflicts.append(sym));
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 public String simplify(Symbol s) {
aoqi@0 298 String name = s.getQualifiedName().toString();
aoqi@0 299 if (!s.type.isCompound() && !s.type.isPrimitive()) {
aoqi@0 300 List<Symbol> conflicts = nameClashes.get(s.getSimpleName());
aoqi@0 301 if (conflicts == null ||
aoqi@0 302 (conflicts.size() == 1 &&
aoqi@0 303 conflicts.contains(s))) {
aoqi@0 304 List<Name> l = List.nil();
aoqi@0 305 Symbol s2 = s;
aoqi@0 306 while (s2.type.hasTag(CLASS) &&
aoqi@0 307 s2.type.getEnclosingType().hasTag(CLASS) &&
aoqi@0 308 s2.owner.kind == Kinds.TYP) {
aoqi@0 309 l = l.prepend(s2.getSimpleName());
aoqi@0 310 s2 = s2.owner;
aoqi@0 311 }
aoqi@0 312 l = l.prepend(s2.getSimpleName());
aoqi@0 313 StringBuilder buf = new StringBuilder();
aoqi@0 314 String sep = "";
aoqi@0 315 for (Name n2 : l) {
aoqi@0 316 buf.append(sep);
aoqi@0 317 buf.append(n2);
aoqi@0 318 sep = ".";
aoqi@0 319 }
aoqi@0 320 name = buf.toString();
aoqi@0 321 }
aoqi@0 322 }
aoqi@0 323 return name;
aoqi@0 324 }
aoqi@0 325 };
aoqi@0 326 // </editor-fold>
aoqi@0 327
aoqi@0 328 // <editor-fold defaultstate="collapsed" desc="rich printer">
aoqi@0 329 /**
aoqi@0 330 * Enhanced type/symbol printer that provides support for features like simple names
aoqi@0 331 * and type variable disambiguation. This enriched printer exploits the info
aoqi@0 332 * discovered during type/symbol preprocessing. This printer is set on the delegate
aoqi@0 333 * formatter so that rich type/symbol info can be properly rendered.
aoqi@0 334 */
aoqi@0 335 protected class RichPrinter extends Printer {
aoqi@0 336
aoqi@0 337 @Override
aoqi@0 338 public String localize(Locale locale, String key, Object... args) {
aoqi@0 339 return formatter.localize(locale, key, args);
aoqi@0 340 }
aoqi@0 341
aoqi@0 342 @Override
aoqi@0 343 public String capturedVarId(CapturedType t, Locale locale) {
aoqi@0 344 return indexOf(t, WhereClauseKind.CAPTURED) + "";
aoqi@0 345 }
aoqi@0 346
aoqi@0 347 @Override
aoqi@0 348 public String visitType(Type t, Locale locale) {
aoqi@0 349 String s = super.visitType(t, locale);
aoqi@0 350 if (t == syms.botType)
aoqi@0 351 s = localize(locale, "compiler.misc.type.null");
aoqi@0 352 return s;
aoqi@0 353 }
aoqi@0 354
aoqi@0 355 @Override
aoqi@0 356 public String visitCapturedType(CapturedType t, Locale locale) {
aoqi@0 357 if (getConfiguration().isEnabled(RichFormatterFeature.WHERE_CLAUSES)) {
aoqi@0 358 return localize(locale,
aoqi@0 359 "compiler.misc.captured.type",
aoqi@0 360 indexOf(t, WhereClauseKind.CAPTURED));
aoqi@0 361 }
aoqi@0 362 else
aoqi@0 363 return super.visitCapturedType(t, locale);
aoqi@0 364 }
aoqi@0 365
aoqi@0 366 @Override
aoqi@0 367 public String visitClassType(ClassType t, Locale locale) {
aoqi@0 368 if (t.isCompound() &&
aoqi@0 369 getConfiguration().isEnabled(RichFormatterFeature.WHERE_CLAUSES)) {
aoqi@0 370 return localize(locale,
aoqi@0 371 "compiler.misc.intersection.type",
aoqi@0 372 indexOf(t, WhereClauseKind.INTERSECTION));
aoqi@0 373 }
aoqi@0 374 else
aoqi@0 375 return super.visitClassType(t, locale);
aoqi@0 376 }
aoqi@0 377
aoqi@0 378 @Override
aoqi@0 379 protected String className(ClassType t, boolean longform, Locale locale) {
aoqi@0 380 Symbol sym = t.tsym;
aoqi@0 381 if (sym.name.length() == 0 ||
aoqi@0 382 !getConfiguration().isEnabled(RichFormatterFeature.SIMPLE_NAMES)) {
aoqi@0 383 return super.className(t, longform, locale);
aoqi@0 384 }
aoqi@0 385 else if (longform)
aoqi@0 386 return nameSimplifier.simplify(sym).toString();
aoqi@0 387 else
aoqi@0 388 return sym.name.toString();
aoqi@0 389 }
aoqi@0 390
aoqi@0 391 @Override
aoqi@0 392 public String visitTypeVar(TypeVar t, Locale locale) {
aoqi@0 393 if (unique(t) ||
aoqi@0 394 !getConfiguration().isEnabled(RichFormatterFeature.UNIQUE_TYPEVAR_NAMES)) {
aoqi@0 395 return t.toString();
aoqi@0 396 }
aoqi@0 397 else {
aoqi@0 398 return localize(locale,
aoqi@0 399 "compiler.misc.type.var",
aoqi@0 400 t.toString(), indexOf(t, WhereClauseKind.TYPEVAR));
aoqi@0 401 }
aoqi@0 402 }
aoqi@0 403
aoqi@0 404 @Override
aoqi@0 405 public String visitClassSymbol(ClassSymbol s, Locale locale) {
aoqi@0 406 if (s.type.isCompound()) {
aoqi@0 407 return visit(s.type, locale);
aoqi@0 408 }
aoqi@0 409 String name = nameSimplifier.simplify(s);
aoqi@0 410 if (name.length() == 0 ||
aoqi@0 411 !getConfiguration().isEnabled(RichFormatterFeature.SIMPLE_NAMES)) {
aoqi@0 412 return super.visitClassSymbol(s, locale);
aoqi@0 413 }
aoqi@0 414 else {
aoqi@0 415 return name;
aoqi@0 416 }
aoqi@0 417 }
aoqi@0 418
aoqi@0 419 @Override
aoqi@0 420 public String visitMethodSymbol(MethodSymbol s, Locale locale) {
aoqi@0 421 String ownerName = visit(s.owner, locale);
aoqi@0 422 if (s.isStaticOrInstanceInit()) {
aoqi@0 423 return ownerName;
aoqi@0 424 } else {
aoqi@0 425 String ms = (s.name == s.name.table.names.init)
aoqi@0 426 ? ownerName
aoqi@0 427 : s.name.toString();
aoqi@0 428 if (s.type != null) {
aoqi@0 429 if (s.type.hasTag(FORALL)) {
aoqi@0 430 ms = "<" + visitTypes(s.type.getTypeArguments(), locale) + ">" + ms;
aoqi@0 431 }
aoqi@0 432 ms += "(" + printMethodArgs(
aoqi@0 433 s.type.getParameterTypes(),
aoqi@0 434 (s.flags() & VARARGS) != 0,
aoqi@0 435 locale) + ")";
aoqi@0 436 }
aoqi@0 437 return ms;
aoqi@0 438 }
aoqi@0 439 }
aoqi@0 440 };
aoqi@0 441 // </editor-fold>
aoqi@0 442
aoqi@0 443 // <editor-fold defaultstate="collapsed" desc="type scanner">
aoqi@0 444 /**
aoqi@0 445 * Preprocess a given type looking for (i) additional info (where clauses) to be
aoqi@0 446 * added to the main diagnostic (ii) names to be compacted.
aoqi@0 447 */
aoqi@0 448 protected void preprocessType(Type t) {
aoqi@0 449 typePreprocessor.visit(t);
aoqi@0 450 }
aoqi@0 451 //where
aoqi@0 452 protected Types.UnaryVisitor<Void> typePreprocessor =
aoqi@0 453 new Types.UnaryVisitor<Void>() {
aoqi@0 454
aoqi@0 455 public Void visit(List<Type> ts) {
aoqi@0 456 for (Type t : ts)
aoqi@0 457 visit(t);
aoqi@0 458 return null;
aoqi@0 459 }
aoqi@0 460
aoqi@0 461 @Override
aoqi@0 462 public Void visitForAll(ForAll t, Void ignored) {
aoqi@0 463 visit(t.tvars);
aoqi@0 464 visit(t.qtype);
aoqi@0 465 return null;
aoqi@0 466 }
aoqi@0 467
aoqi@0 468 @Override
aoqi@0 469 public Void visitMethodType(MethodType t, Void ignored) {
aoqi@0 470 visit(t.argtypes);
aoqi@0 471 visit(t.restype);
aoqi@0 472 return null;
aoqi@0 473 }
aoqi@0 474
aoqi@0 475 @Override
aoqi@0 476 public Void visitErrorType(ErrorType t, Void ignored) {
aoqi@0 477 Type ot = t.getOriginalType();
aoqi@0 478 if (ot != null)
aoqi@0 479 visit(ot);
aoqi@0 480 return null;
aoqi@0 481 }
aoqi@0 482
aoqi@0 483 @Override
aoqi@0 484 public Void visitArrayType(ArrayType t, Void ignored) {
aoqi@0 485 visit(t.elemtype);
aoqi@0 486 return null;
aoqi@0 487 }
aoqi@0 488
aoqi@0 489 @Override
aoqi@0 490 public Void visitWildcardType(WildcardType t, Void ignored) {
aoqi@0 491 visit(t.type);
aoqi@0 492 return null;
aoqi@0 493 }
aoqi@0 494
aoqi@0 495 public Void visitType(Type t, Void ignored) {
aoqi@0 496 return null;
aoqi@0 497 }
aoqi@0 498
aoqi@0 499 @Override
aoqi@0 500 public Void visitCapturedType(CapturedType t, Void ignored) {
aoqi@0 501 if (indexOf(t, WhereClauseKind.CAPTURED) == -1) {
aoqi@0 502 String suffix = t.lower == syms.botType ? ".1" : "";
aoqi@0 503 JCDiagnostic d = diags.fragment("where.captured"+ suffix, t, t.bound, t.lower, t.wildcard);
aoqi@0 504 whereClauses.get(WhereClauseKind.CAPTURED).put(t, d);
aoqi@0 505 visit(t.wildcard);
aoqi@0 506 visit(t.lower);
aoqi@0 507 visit(t.bound);
aoqi@0 508 }
aoqi@0 509 return null;
aoqi@0 510 }
aoqi@0 511
aoqi@0 512 @Override
aoqi@0 513 public Void visitClassType(ClassType t, Void ignored) {
aoqi@0 514 if (t.isCompound()) {
aoqi@0 515 if (indexOf(t, WhereClauseKind.INTERSECTION) == -1) {
aoqi@0 516 Type supertype = types.supertype(t);
aoqi@0 517 List<Type> interfaces = types.interfaces(t);
aoqi@0 518 JCDiagnostic d = diags.fragment("where.intersection", t, interfaces.prepend(supertype));
aoqi@0 519 whereClauses.get(WhereClauseKind.INTERSECTION).put(t, d);
aoqi@0 520 visit(supertype);
aoqi@0 521 visit(interfaces);
aoqi@0 522 }
aoqi@0 523 } else if (t.tsym.name.isEmpty()) {
aoqi@0 524 //anon class
aoqi@0 525 ClassType norm = (ClassType) t.tsym.type;
aoqi@0 526 if (norm != null) {
aoqi@0 527 if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
aoqi@0 528 visit(norm.interfaces_field.head);
aoqi@0 529 } else {
aoqi@0 530 visit(norm.supertype_field);
aoqi@0 531 }
aoqi@0 532 }
aoqi@0 533 }
aoqi@0 534 nameSimplifier.addUsage(t.tsym);
aoqi@0 535 visit(t.getTypeArguments());
aoqi@0 536 if (t.getEnclosingType() != Type.noType)
aoqi@0 537 visit(t.getEnclosingType());
aoqi@0 538 return null;
aoqi@0 539 }
aoqi@0 540
aoqi@0 541 @Override
aoqi@0 542 public Void visitTypeVar(TypeVar t, Void ignored) {
aoqi@0 543 if (indexOf(t, WhereClauseKind.TYPEVAR) == -1) {
aoqi@0 544 //access the bound type and skip error types
aoqi@0 545 Type bound = t.bound;
aoqi@0 546 while ((bound instanceof ErrorType))
aoqi@0 547 bound = ((ErrorType)bound).getOriginalType();
aoqi@0 548 //retrieve the bound list - if the type variable
aoqi@0 549 //has not been attributed the bound is not set
aoqi@0 550 List<Type> bounds = (bound != null) &&
aoqi@0 551 (bound.hasTag(CLASS) || bound.hasTag(TYPEVAR)) ?
aoqi@0 552 types.getBounds(t) :
aoqi@0 553 List.<Type>nil();
aoqi@0 554
aoqi@0 555 nameSimplifier.addUsage(t.tsym);
aoqi@0 556
aoqi@0 557 boolean boundErroneous = bounds.head == null ||
aoqi@0 558 bounds.head.hasTag(NONE) ||
aoqi@0 559 bounds.head.hasTag(ERROR);
aoqi@0 560
aoqi@0 561 if ((t.tsym.flags() & SYNTHETIC) == 0) {
aoqi@0 562 //this is a true typevar
aoqi@0 563 JCDiagnostic d = diags.fragment("where.typevar" +
aoqi@0 564 (boundErroneous ? ".1" : ""), t, bounds,
aoqi@0 565 Kinds.kindName(t.tsym.location()), t.tsym.location());
aoqi@0 566 whereClauses.get(WhereClauseKind.TYPEVAR).put(t, d);
aoqi@0 567 symbolPreprocessor.visit(t.tsym.location(), null);
aoqi@0 568 visit(bounds);
aoqi@0 569 } else {
aoqi@0 570 Assert.check(!boundErroneous);
aoqi@0 571 //this is a fresh (synthetic) tvar
aoqi@0 572 JCDiagnostic d = diags.fragment("where.fresh.typevar", t, bounds);
aoqi@0 573 whereClauses.get(WhereClauseKind.TYPEVAR).put(t, d);
aoqi@0 574 visit(bounds);
aoqi@0 575 }
aoqi@0 576
aoqi@0 577 }
aoqi@0 578 return null;
aoqi@0 579 }
aoqi@0 580 };
aoqi@0 581 // </editor-fold>
aoqi@0 582
aoqi@0 583 // <editor-fold defaultstate="collapsed" desc="symbol scanner">
aoqi@0 584 /**
aoqi@0 585 * Preprocess a given symbol looking for (i) additional info (where clauses) to be
aoqi@0 586 * added to the main diagnostic (ii) names to be compacted
aoqi@0 587 */
aoqi@0 588 protected void preprocessSymbol(Symbol s) {
aoqi@0 589 symbolPreprocessor.visit(s, null);
aoqi@0 590 }
aoqi@0 591 //where
aoqi@0 592 protected Types.DefaultSymbolVisitor<Void, Void> symbolPreprocessor =
aoqi@0 593 new Types.DefaultSymbolVisitor<Void, Void>() {
aoqi@0 594
aoqi@0 595 @Override
aoqi@0 596 public Void visitClassSymbol(ClassSymbol s, Void ignored) {
aoqi@0 597 if (s.type.isCompound()) {
aoqi@0 598 typePreprocessor.visit(s.type);
aoqi@0 599 } else {
aoqi@0 600 nameSimplifier.addUsage(s);
aoqi@0 601 }
aoqi@0 602 return null;
aoqi@0 603 }
aoqi@0 604
aoqi@0 605 @Override
aoqi@0 606 public Void visitSymbol(Symbol s, Void ignored) {
aoqi@0 607 return null;
aoqi@0 608 }
aoqi@0 609
aoqi@0 610 @Override
aoqi@0 611 public Void visitMethodSymbol(MethodSymbol s, Void ignored) {
aoqi@0 612 visit(s.owner, null);
aoqi@0 613 if (s.type != null)
aoqi@0 614 typePreprocessor.visit(s.type);
aoqi@0 615 return null;
aoqi@0 616 }
aoqi@0 617 };
aoqi@0 618 // </editor-fold>
aoqi@0 619
aoqi@0 620 @Override
aoqi@0 621 public RichConfiguration getConfiguration() {
aoqi@0 622 //the following cast is always safe - see init
aoqi@0 623 return (RichConfiguration)configuration;
aoqi@0 624 }
aoqi@0 625
aoqi@0 626 /**
aoqi@0 627 * Configuration object provided by the rich formatter.
aoqi@0 628 */
aoqi@0 629 public static class RichConfiguration extends ForwardingDiagnosticFormatter.ForwardingConfiguration {
aoqi@0 630
aoqi@0 631 /** set of enabled rich formatter's features */
aoqi@0 632 protected java.util.EnumSet<RichFormatterFeature> features;
aoqi@0 633
aoqi@0 634 @SuppressWarnings("fallthrough")
aoqi@0 635 public RichConfiguration(Options options, AbstractDiagnosticFormatter formatter) {
aoqi@0 636 super(formatter.getConfiguration());
aoqi@0 637 features = formatter.isRaw() ? EnumSet.noneOf(RichFormatterFeature.class) :
aoqi@0 638 EnumSet.of(RichFormatterFeature.SIMPLE_NAMES,
aoqi@0 639 RichFormatterFeature.WHERE_CLAUSES,
aoqi@0 640 RichFormatterFeature.UNIQUE_TYPEVAR_NAMES);
aoqi@0 641 String diagOpts = options.get("diags");
aoqi@0 642 if (diagOpts != null) {
aoqi@0 643 for (String args: diagOpts.split(",")) {
aoqi@0 644 if (args.equals("-where")) {
aoqi@0 645 features.remove(RichFormatterFeature.WHERE_CLAUSES);
aoqi@0 646 }
aoqi@0 647 else if (args.equals("where")) {
aoqi@0 648 features.add(RichFormatterFeature.WHERE_CLAUSES);
aoqi@0 649 }
aoqi@0 650 if (args.equals("-simpleNames")) {
aoqi@0 651 features.remove(RichFormatterFeature.SIMPLE_NAMES);
aoqi@0 652 }
aoqi@0 653 else if (args.equals("simpleNames")) {
aoqi@0 654 features.add(RichFormatterFeature.SIMPLE_NAMES);
aoqi@0 655 }
aoqi@0 656 if (args.equals("-disambiguateTvars")) {
aoqi@0 657 features.remove(RichFormatterFeature.UNIQUE_TYPEVAR_NAMES);
aoqi@0 658 }
aoqi@0 659 else if (args.equals("disambiguateTvars")) {
aoqi@0 660 features.add(RichFormatterFeature.UNIQUE_TYPEVAR_NAMES);
aoqi@0 661 }
aoqi@0 662 }
aoqi@0 663 }
aoqi@0 664 }
aoqi@0 665
aoqi@0 666 /**
aoqi@0 667 * Returns a list of all the features supported by the rich formatter.
aoqi@0 668 * @return list of supported features
aoqi@0 669 */
aoqi@0 670 public RichFormatterFeature[] getAvailableFeatures() {
aoqi@0 671 return RichFormatterFeature.values();
aoqi@0 672 }
aoqi@0 673
aoqi@0 674 /**
aoqi@0 675 * Enable a specific feature on this rich formatter.
aoqi@0 676 * @param feature feature to be enabled
aoqi@0 677 */
aoqi@0 678 public void enable(RichFormatterFeature feature) {
aoqi@0 679 features.add(feature);
aoqi@0 680 }
aoqi@0 681
aoqi@0 682 /**
aoqi@0 683 * Disable a specific feature on this rich formatter.
aoqi@0 684 * @param feature feature to be disabled
aoqi@0 685 */
aoqi@0 686 public void disable(RichFormatterFeature feature) {
aoqi@0 687 features.remove(feature);
aoqi@0 688 }
aoqi@0 689
aoqi@0 690 /**
aoqi@0 691 * Is a given feature enabled on this formatter?
aoqi@0 692 * @param feature feature to be tested
aoqi@0 693 */
aoqi@0 694 public boolean isEnabled(RichFormatterFeature feature) {
aoqi@0 695 return features.contains(feature);
aoqi@0 696 }
aoqi@0 697
aoqi@0 698 /**
aoqi@0 699 * The advanced formatting features provided by the rich formatter
aoqi@0 700 */
aoqi@0 701 public enum RichFormatterFeature {
aoqi@0 702 /** a list of additional info regarding a given type/symbol */
aoqi@0 703 WHERE_CLAUSES,
aoqi@0 704 /** full class names simplification (where possible) */
aoqi@0 705 SIMPLE_NAMES,
aoqi@0 706 /** type-variable names disambiguation */
aoqi@0 707 UNIQUE_TYPEVAR_NAMES;
aoqi@0 708 }
aoqi@0 709 }
aoqi@0 710 }

mercurial