test/tools/javac/lib/DPrinter.java

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

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

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 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.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 import java.io.File;
aoqi@0 25 import java.io.IOException;
aoqi@0 26 import java.io.PrintWriter;
aoqi@0 27 import java.lang.reflect.Field;
aoqi@0 28 import java.util.ArrayList;
aoqi@0 29 import java.util.Arrays;
aoqi@0 30 import java.util.Collection;
aoqi@0 31 import java.util.EnumSet;
aoqi@0 32 import java.util.HashMap;
aoqi@0 33 import java.util.List;
aoqi@0 34 import java.util.Locale;
aoqi@0 35 import java.util.Map;
aoqi@0 36 import java.util.Set;
aoqi@0 37
aoqi@0 38 import javax.lang.model.element.Name;
aoqi@0 39 import javax.lang.model.element.TypeElement;
aoqi@0 40 import javax.tools.FileObject;
aoqi@0 41 import javax.tools.JavaCompiler;
aoqi@0 42 import javax.tools.JavaFileObject;
aoqi@0 43 import javax.tools.StandardJavaFileManager;
aoqi@0 44 import javax.tools.StandardLocation;
aoqi@0 45 import javax.tools.ToolProvider;
aoqi@0 46
aoqi@0 47 import com.sun.source.util.JavacTask;
aoqi@0 48 import com.sun.source.util.TaskEvent;
aoqi@0 49 import com.sun.source.util.TaskListener;
aoqi@0 50 import com.sun.source.util.Trees;
aoqi@0 51 import com.sun.tools.javac.api.JavacTrees;
aoqi@0 52 import com.sun.tools.javac.code.SymbolMetadata;
aoqi@0 53 import com.sun.tools.javac.code.Attribute;
aoqi@0 54 import com.sun.tools.javac.code.Flags;
aoqi@0 55 import com.sun.tools.javac.code.Kinds;
aoqi@0 56 import com.sun.tools.javac.code.Printer;
aoqi@0 57 import com.sun.tools.javac.code.Scope;
aoqi@0 58 import com.sun.tools.javac.code.Scope.CompoundScope;
aoqi@0 59 import com.sun.tools.javac.code.Symbol;
aoqi@0 60 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 61 import com.sun.tools.javac.code.Type;
aoqi@0 62 import com.sun.tools.javac.code.Type.*;
aoqi@0 63 import com.sun.tools.javac.code.TypeTag;
aoqi@0 64 import com.sun.tools.javac.tree.JCTree;
aoqi@0 65 import com.sun.tools.javac.tree.JCTree.*;
aoqi@0 66 import com.sun.tools.javac.tree.Pretty;
aoqi@0 67 import com.sun.tools.javac.tree.TreeInfo;
aoqi@0 68 import com.sun.tools.javac.tree.TreeScanner;
aoqi@0 69 import com.sun.tools.javac.util.Assert;
aoqi@0 70 import com.sun.tools.javac.util.Context;
aoqi@0 71 import com.sun.tools.javac.util.Log;
aoqi@0 72
aoqi@0 73
aoqi@0 74 /**
aoqi@0 75 * Debug printer for javac internals, for when toString() just isn't enough.
aoqi@0 76 *
aoqi@0 77 * <p>
aoqi@0 78 * The printer provides an API to generate structured views of javac objects,
aoqi@0 79 * such as AST nodes, symbol, types and annotations. Various aspects of the
aoqi@0 80 * output can be configured, such as whether to show nulls, empty lists, or
aoqi@0 81 * a compressed representation of the source code. Visitors are used to walk
aoqi@0 82 * object hierarchies, and can be replaced with custom visitors if the default
aoqi@0 83 * visitors are not flexible enough.
aoqi@0 84 *
aoqi@0 85 * <p>
aoqi@0 86 * In general, nodes are printed with an initial line identifying the node
aoqi@0 87 * followed by indented lines for the child nodes. Currently, graphs are
aoqi@0 88 * represented by printing a spanning subtree.
aoqi@0 89 *
aoqi@0 90 * <p>
aoqi@0 91 * The printer can be accessed via a simple command-line utility,
aoqi@0 92 * which makes it easy to see the internal representation of source code,
aoqi@0 93 * such as simple test programs, during the compilation pipeline.
aoqi@0 94 *
aoqi@0 95 * <p><b>This is NOT part of any supported API.
aoqi@0 96 * If you write code that depends on this, you do so at your own risk.
aoqi@0 97 * This code and its internal interfaces are subject to change or
aoqi@0 98 * deletion without notice.</b>
aoqi@0 99 */
aoqi@0 100
aoqi@0 101 public class DPrinter {
aoqi@0 102 protected final PrintWriter out;
aoqi@0 103 protected final Trees trees;
aoqi@0 104 protected Printer printer;
aoqi@0 105 protected boolean showEmptyItems = true;
aoqi@0 106 protected boolean showNulls = true;
aoqi@0 107 protected boolean showPositions = false;
aoqi@0 108 protected boolean showSrc;
aoqi@0 109 protected boolean showTreeSymbols;
aoqi@0 110 protected boolean showTreeTypes;
aoqi@0 111 protected int maxSrcLength = 32;
aoqi@0 112 protected Locale locale = Locale.getDefault();
aoqi@0 113 protected static final String NULL = "#null";
aoqi@0 114
aoqi@0 115 // <editor-fold defaultstate="collapsed" desc="Configuration">
aoqi@0 116
aoqi@0 117 public static DPrinter instance(Context context) {
aoqi@0 118 DPrinter dp = context.get(DPrinter.class);
aoqi@0 119 if (dp == null) {
aoqi@0 120 dp = new DPrinter(context);
aoqi@0 121 }
aoqi@0 122 return dp;
aoqi@0 123
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 protected DPrinter(Context context) {
aoqi@0 127 context.put(DPrinter.class, this);
aoqi@0 128 out = context.get(Log.outKey);
aoqi@0 129 trees = JavacTrees.instance(context);
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 public DPrinter(PrintWriter out, Trees trees) {
aoqi@0 133 this.out = out;
aoqi@0 134 this.trees = trees;
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 public DPrinter emptyItems(boolean showEmptyItems) {
aoqi@0 138 this.showEmptyItems = showEmptyItems;
aoqi@0 139 return this;
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 public DPrinter nulls(boolean showNulls) {
aoqi@0 143 this.showNulls = showNulls;
aoqi@0 144 return this;
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 public DPrinter positions(boolean showPositions) {
aoqi@0 148 this.showPositions = showPositions;
aoqi@0 149 return this;
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 public DPrinter source(boolean showSrc) {
aoqi@0 153 this.showSrc = showSrc;
aoqi@0 154 return this;
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 public DPrinter source(int maxSrcLength) {
aoqi@0 158 this.showSrc = true;
aoqi@0 159 this.maxSrcLength = maxSrcLength;
aoqi@0 160 return this;
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 public DPrinter treeSymbols(boolean showTreeSymbols) {
aoqi@0 164 this.showTreeSymbols = showTreeSymbols;
aoqi@0 165 return this;
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 public DPrinter treeTypes(boolean showTreeTypes) {
aoqi@0 169 this.showTreeTypes = showTreeTypes;
aoqi@0 170 return this;
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 public DPrinter typeSymbolPrinter(Printer p) {
aoqi@0 174 printer = p;
aoqi@0 175 return this;
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 // </editor-fold>
aoqi@0 179
aoqi@0 180 // <editor-fold defaultstate="collapsed" desc="Printing">
aoqi@0 181
aoqi@0 182 protected enum Details {
aoqi@0 183 /** A one-line non-recursive summary */
aoqi@0 184 SUMMARY,
aoqi@0 185 /** Multi-line, possibly recursive. */
aoqi@0 186 FULL
aoqi@0 187 };
aoqi@0 188
aoqi@0 189 public void printAnnotations(String label, SymbolMetadata annotations) {
aoqi@0 190 printAnnotations(label, annotations, Details.FULL);
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 protected void printAnnotations(String label, SymbolMetadata annotations, Details details) {
aoqi@0 194 if (annotations == null) {
aoqi@0 195 printNull(label);
aoqi@0 196 } else {
aoqi@0 197 // no SUMMARY format currently available to use
aoqi@0 198
aoqi@0 199 // use reflection to get at private fields
aoqi@0 200 Object DECL_NOT_STARTED = getField(null, SymbolMetadata.class, "DECL_NOT_STARTED");
aoqi@0 201 Object DECL_IN_PROGRESS = getField(null, SymbolMetadata.class, "DECL_IN_PROGRESS");
aoqi@0 202 Object attributes = getField(annotations, SymbolMetadata.class, "attributes");
aoqi@0 203 Object type_attributes = getField(annotations, SymbolMetadata.class, "type_attributes");
aoqi@0 204
aoqi@0 205 if (!showEmptyItems) {
aoqi@0 206 if (attributes instanceof List && ((List) attributes).isEmpty()
aoqi@0 207 && attributes != DECL_NOT_STARTED
aoqi@0 208 && attributes != DECL_IN_PROGRESS
aoqi@0 209 && type_attributes instanceof List && ((List) type_attributes).isEmpty())
aoqi@0 210 return;
aoqi@0 211 }
aoqi@0 212
aoqi@0 213 printString(label, hashString(annotations));
aoqi@0 214
aoqi@0 215 indent(+1);
aoqi@0 216 if (attributes == DECL_NOT_STARTED)
aoqi@0 217 printString("attributes", "DECL_NOT_STARTED");
aoqi@0 218 else if (attributes == DECL_IN_PROGRESS)
aoqi@0 219 printString("attributes", "DECL_IN_PROGRESS");
aoqi@0 220 else if (attributes instanceof List)
aoqi@0 221 printList("attributes", (List) attributes);
aoqi@0 222 else
aoqi@0 223 printObject("attributes", attributes, Details.SUMMARY);
aoqi@0 224
aoqi@0 225 if (attributes instanceof List)
aoqi@0 226 printList("type_attributes", (List) type_attributes);
aoqi@0 227 else
aoqi@0 228 printObject("type_attributes", type_attributes, Details.SUMMARY);
aoqi@0 229 indent(-1);
aoqi@0 230 }
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 public void printAttribute(String label, Attribute attr) {
aoqi@0 234 if (attr == null) {
aoqi@0 235 printNull(label);
aoqi@0 236 } else {
aoqi@0 237 printString(label, attr.getClass().getSimpleName());
aoqi@0 238
aoqi@0 239 indent(+1);
aoqi@0 240 attr.accept(attrVisitor);
aoqi@0 241 indent(-1);
aoqi@0 242 }
aoqi@0 243 }
aoqi@0 244
aoqi@0 245 public void printFileObject(String label, FileObject fo) {
aoqi@0 246 if (fo == null) {
aoqi@0 247 printNull(label);
aoqi@0 248 } else {
aoqi@0 249 printString(label, fo.getName());
aoqi@0 250 }
aoqi@0 251 }
aoqi@0 252
aoqi@0 253 protected <T> void printImplClass(T item, Class<? extends T> stdImplClass) {
aoqi@0 254 if (item.getClass() != stdImplClass)
aoqi@0 255 printString("impl", item.getClass().getName());
aoqi@0 256 }
aoqi@0 257
aoqi@0 258 public void printInt(String label, int i) {
aoqi@0 259 printString(label, String.valueOf(i));
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 public void printList(String label, List<?> list) {
aoqi@0 263 if (list == null) {
aoqi@0 264 printNull(label);
aoqi@0 265 } else if (!list.isEmpty() || showEmptyItems) {
aoqi@0 266 printString(label, "[" + list.size() + "]");
aoqi@0 267
aoqi@0 268 indent(+1);
aoqi@0 269 int i = 0;
aoqi@0 270 for (Object item: list) {
aoqi@0 271 printObject(String.valueOf(i++), item, Details.FULL);
aoqi@0 272 }
aoqi@0 273 indent(-1);
aoqi@0 274 }
aoqi@0 275 }
aoqi@0 276
aoqi@0 277 public void printName(String label, Name name) {
aoqi@0 278 if (name == null) {
aoqi@0 279 printNull(label);
aoqi@0 280 } else {
aoqi@0 281 printString(label, name.toString());
aoqi@0 282 }
aoqi@0 283 }
aoqi@0 284
aoqi@0 285 public void printNull(String label) {
aoqi@0 286 if (showNulls)
aoqi@0 287 printString(label, NULL);
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 protected void printObject(String label, Object item, Details details) {
aoqi@0 291 if (item == null) {
aoqi@0 292 printNull(label);
aoqi@0 293 } else if (item instanceof Attribute) {
aoqi@0 294 printAttribute(label, (Attribute) item);
aoqi@0 295 } else if (item instanceof Symbol) {
aoqi@0 296 printSymbol(label, (Symbol) item, details);
aoqi@0 297 } else if (item instanceof Type) {
aoqi@0 298 printType(label, (Type) item, details);
aoqi@0 299 } else if (item instanceof JCTree) {
aoqi@0 300 printTree(label, (JCTree) item);
aoqi@0 301 } else if (item instanceof List) {
aoqi@0 302 printList(label, (List) item);
aoqi@0 303 } else if (item instanceof Name) {
aoqi@0 304 printName(label, (Name) item);
aoqi@0 305 } else {
aoqi@0 306 printString(label, String.valueOf(item));
aoqi@0 307 }
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 public void printScope(String label, Scope scope) {
aoqi@0 311 printScope(label, scope, Details.FULL);
aoqi@0 312 }
aoqi@0 313
aoqi@0 314 public void printScope(String label, Scope scope, Details details) {
aoqi@0 315 if (scope == null) {
aoqi@0 316 printNull(label);
aoqi@0 317 } else {
aoqi@0 318 switch (details) {
aoqi@0 319 case SUMMARY: {
aoqi@0 320 indent();
aoqi@0 321 out.print(label);
aoqi@0 322 out.print(": [");
aoqi@0 323 String sep = "";
aoqi@0 324 for (Symbol sym: scope.getElements()) {
aoqi@0 325 out.print(sep);
aoqi@0 326 out.print(sym.name);
aoqi@0 327 sep = ",";
aoqi@0 328 }
aoqi@0 329 out.println("]");
aoqi@0 330 break;
aoqi@0 331 }
aoqi@0 332
aoqi@0 333 case FULL: {
aoqi@0 334 indent();
aoqi@0 335 out.println(label);
aoqi@0 336
aoqi@0 337 indent(+1);
aoqi@0 338 printImplClass(scope, Scope.class);
aoqi@0 339 printSymbol("owner", scope.owner, Details.SUMMARY);
aoqi@0 340 printScope("next", scope.next, Details.SUMMARY);
aoqi@0 341 printObject("shared", getField(scope, Scope.class, "shared"), Details.SUMMARY);
aoqi@0 342 if (scope instanceof CompoundScope) {
aoqi@0 343 printObject("subScopes",
aoqi@0 344 getField(scope, CompoundScope.class, "subScopes"),
aoqi@0 345 Details.FULL);
aoqi@0 346 } else {
aoqi@0 347 for (Symbol sym : scope.getElements()) {
aoqi@0 348 printSymbol(sym.name.toString(), sym, Details.SUMMARY);
aoqi@0 349 }
aoqi@0 350 }
aoqi@0 351 indent(-1);
aoqi@0 352 break;
aoqi@0 353 }
aoqi@0 354 }
aoqi@0 355 }
aoqi@0 356 }
aoqi@0 357
aoqi@0 358 public void printSource(String label, JCTree tree) {
aoqi@0 359 printString(label, Pretty.toSimpleString(tree, maxSrcLength));
aoqi@0 360 }
aoqi@0 361
aoqi@0 362 public void printString(String label, String text) {
aoqi@0 363 indent();
aoqi@0 364 out.print(label);
aoqi@0 365 out.print(": ");
aoqi@0 366 out.print(text);
aoqi@0 367 out.println();
aoqi@0 368 }
aoqi@0 369
aoqi@0 370 public void printSymbol(String label, Symbol symbol) {
aoqi@0 371 printSymbol(label, symbol, Details.FULL);
aoqi@0 372 }
aoqi@0 373
aoqi@0 374 protected void printSymbol(String label, Symbol sym, Details details) {
aoqi@0 375 if (sym == null) {
aoqi@0 376 printNull(label);
aoqi@0 377 } else {
aoqi@0 378 switch (details) {
aoqi@0 379 case SUMMARY:
aoqi@0 380 printString(label, toString(sym));
aoqi@0 381 break;
aoqi@0 382
aoqi@0 383 case FULL:
aoqi@0 384 indent();
aoqi@0 385 out.print(label);
aoqi@0 386 out.println(": " +
aoqi@0 387 info(sym.getClass(),
aoqi@0 388 String.format("0x%x--%s", sym.kind, Kinds.kindName(sym)),
aoqi@0 389 sym.getKind())
aoqi@0 390 + " " + sym.name
aoqi@0 391 + " " + hashString(sym));
aoqi@0 392
aoqi@0 393 indent(+1);
aoqi@0 394 if (showSrc) {
aoqi@0 395 JCTree tree = (JCTree) trees.getTree(sym);
aoqi@0 396 if (tree != null)
aoqi@0 397 printSource("src", tree);
aoqi@0 398 }
aoqi@0 399 printString("flags", String.format("0x%x--%s",
aoqi@0 400 sym.flags_field, Flags.toString(sym.flags_field)));
aoqi@0 401 printObject("completer", sym.completer, Details.SUMMARY); // what if too long?
aoqi@0 402 printSymbol("owner", sym.owner, Details.SUMMARY);
aoqi@0 403 printType("type", sym.type, Details.SUMMARY);
aoqi@0 404 printType("erasure", sym.erasure_field, Details.SUMMARY);
aoqi@0 405 sym.accept(symVisitor, null);
aoqi@0 406 printAnnotations("annotations", sym.getAnnotations(), Details.SUMMARY);
aoqi@0 407 indent(-1);
aoqi@0 408 }
aoqi@0 409 }
aoqi@0 410 }
aoqi@0 411
aoqi@0 412 protected String toString(Symbol sym) {
aoqi@0 413 return (printer != null) ? printer.visit(sym, locale) : String.valueOf(sym);
aoqi@0 414 }
aoqi@0 415
aoqi@0 416 protected void printTree(String label, JCTree tree) {
aoqi@0 417 if (tree == null) {
aoqi@0 418 printNull(label);
aoqi@0 419 } else {
aoqi@0 420 indent();
aoqi@0 421 String ext;
aoqi@0 422 try {
aoqi@0 423 ext = tree.getKind().name();
aoqi@0 424 } catch (Throwable t) {
aoqi@0 425 ext = "n/a";
aoqi@0 426 }
aoqi@0 427 out.print(label + ": " + info(tree.getClass(), tree.getTag(), ext));
aoqi@0 428 if (showPositions) {
aoqi@0 429 // We can always get start position, but to get end position
aoqi@0 430 // and/or line+offset, we would need a JCCompilationUnit
aoqi@0 431 out.print(" pos:" + tree.pos);
aoqi@0 432 }
aoqi@0 433 if (showTreeTypes && tree.type != null)
aoqi@0 434 out.print(" type:" + toString(tree.type));
aoqi@0 435 Symbol sym;
aoqi@0 436 if (showTreeSymbols && (sym = TreeInfo.symbolFor(tree)) != null)
aoqi@0 437 out.print(" sym:" + toString(sym));
aoqi@0 438 out.println();
aoqi@0 439
aoqi@0 440 indent(+1);
aoqi@0 441 if (showSrc) {
aoqi@0 442 indent();
aoqi@0 443 out.println("src: " + Pretty.toSimpleString(tree, maxSrcLength));
aoqi@0 444 }
aoqi@0 445 tree.accept(treeVisitor);
aoqi@0 446 indent(-1);
aoqi@0 447 }
aoqi@0 448 }
aoqi@0 449
aoqi@0 450 public void printType(String label, Type type) {
aoqi@0 451 printType(label, type, Details.FULL);
aoqi@0 452 }
aoqi@0 453
aoqi@0 454 protected void printType(String label, Type type, Details details) {
aoqi@0 455 if (type == null)
aoqi@0 456 printNull(label);
aoqi@0 457 else {
aoqi@0 458 switch (details) {
aoqi@0 459 case SUMMARY:
aoqi@0 460 printString(label, toString(type));
aoqi@0 461 break;
aoqi@0 462
aoqi@0 463 case FULL:
aoqi@0 464 indent();
aoqi@0 465 out.print(label);
aoqi@0 466 out.println(": " + info(type.getClass(), type.getTag(), type.getKind())
aoqi@0 467 + " " + hashString(type));
aoqi@0 468
aoqi@0 469 indent(+1);
aoqi@0 470 printSymbol("tsym", type.tsym, Details.SUMMARY);
aoqi@0 471 printObject("constValue", type.constValue(), Details.SUMMARY);
aoqi@0 472 printObject("annotations", type.getAnnotationMirrors(), Details.SUMMARY);
aoqi@0 473 type.accept(typeVisitor, null);
aoqi@0 474 indent(-1);
aoqi@0 475 }
aoqi@0 476 }
aoqi@0 477 }
aoqi@0 478
aoqi@0 479 protected String toString(Type type) {
aoqi@0 480 return (printer != null) ? printer.visit(type, locale) : String.valueOf(type);
aoqi@0 481 }
aoqi@0 482
aoqi@0 483 protected String hashString(Object obj) {
aoqi@0 484 return String.format("#%x", obj.hashCode());
aoqi@0 485 }
aoqi@0 486
aoqi@0 487 protected String info(Class<?> clazz, Object internal, Object external) {
aoqi@0 488 return String.format("%s,%s,%s", clazz.getSimpleName(), internal, external);
aoqi@0 489 }
aoqi@0 490
aoqi@0 491 private int indent = 0;
aoqi@0 492
aoqi@0 493 protected void indent() {
aoqi@0 494 for (int i = 0; i < indent; i++) {
aoqi@0 495 out.print(" ");
aoqi@0 496 }
aoqi@0 497 }
aoqi@0 498
aoqi@0 499 protected void indent(int n) {
aoqi@0 500 indent += n;
aoqi@0 501 }
aoqi@0 502
aoqi@0 503 protected Object getField(Object o, Class<?> clazz, String name) {
aoqi@0 504 try {
aoqi@0 505 Field f = clazz.getDeclaredField(name);
aoqi@0 506 boolean prev = f.isAccessible();
aoqi@0 507 f.setAccessible(true);
aoqi@0 508 try {
aoqi@0 509 return f.get(o);
aoqi@0 510 } finally {
aoqi@0 511 f.setAccessible(prev);
aoqi@0 512 }
aoqi@0 513 } catch (ReflectiveOperationException e) {
aoqi@0 514 return e;
aoqi@0 515 } catch (SecurityException e) {
aoqi@0 516 return e;
aoqi@0 517 }
aoqi@0 518 }
aoqi@0 519
aoqi@0 520 // </editor-fold>
aoqi@0 521
aoqi@0 522 // <editor-fold defaultstate="collapsed" desc="JCTree visitor methods">
aoqi@0 523
aoqi@0 524 protected JCTree.Visitor treeVisitor = new TreeVisitor();
aoqi@0 525
aoqi@0 526 /**
aoqi@0 527 * Default visitor class for JCTree (AST) objects.
aoqi@0 528 */
aoqi@0 529 public class TreeVisitor extends JCTree.Visitor {
aoqi@0 530 @Override
aoqi@0 531 public void visitTopLevel(JCCompilationUnit tree) {
aoqi@0 532 printList("packageAnnotations", tree.packageAnnotations);
aoqi@0 533 printTree("pid", tree.pid);
aoqi@0 534 printList("defs", tree.defs);
aoqi@0 535 }
aoqi@0 536
aoqi@0 537 @Override
aoqi@0 538 public void visitImport(JCImport tree) {
aoqi@0 539 printTree("qualid", tree.qualid);
aoqi@0 540 }
aoqi@0 541
aoqi@0 542 @Override
aoqi@0 543 public void visitClassDef(JCClassDecl tree) {
aoqi@0 544 printName("name", tree.name);
aoqi@0 545 printTree("mods", tree.mods);
aoqi@0 546 printList("typarams", tree.typarams);
aoqi@0 547 printTree("extending", tree.extending);
aoqi@0 548 printList("implementing", tree.implementing);
aoqi@0 549 printList("defs", tree.defs);
aoqi@0 550 }
aoqi@0 551
aoqi@0 552 @Override
aoqi@0 553 public void visitMethodDef(JCMethodDecl tree) {
aoqi@0 554 printName("name", tree.name);
aoqi@0 555 printTree("mods", tree.mods);
aoqi@0 556 printTree("restype", tree.restype);
aoqi@0 557 printList("typarams", tree.typarams);
aoqi@0 558 printTree("recvparam", tree.recvparam);
aoqi@0 559 printList("params", tree.params);
aoqi@0 560 printList("thrown", tree.thrown);
aoqi@0 561 printTree("defaultValue", tree.defaultValue);
aoqi@0 562 printTree("body", tree.body);
aoqi@0 563 }
aoqi@0 564
aoqi@0 565 @Override
aoqi@0 566 public void visitVarDef(JCVariableDecl tree) {
aoqi@0 567 printName("name", tree.name);
aoqi@0 568 printTree("mods", tree.mods);
aoqi@0 569 printTree("vartype", tree.vartype);
aoqi@0 570 printTree("init", tree.init);
aoqi@0 571 }
aoqi@0 572
aoqi@0 573 @Override
aoqi@0 574 public void visitSkip(JCSkip tree) {
aoqi@0 575 }
aoqi@0 576
aoqi@0 577 @Override
aoqi@0 578 public void visitBlock(JCBlock tree) {
aoqi@0 579 printList("stats", tree.stats);
aoqi@0 580 }
aoqi@0 581
aoqi@0 582 @Override
aoqi@0 583 public void visitDoLoop(JCDoWhileLoop tree) {
aoqi@0 584 printTree("body", tree.body);
aoqi@0 585 printTree("cond", tree.cond);
aoqi@0 586 }
aoqi@0 587
aoqi@0 588 @Override
aoqi@0 589 public void visitWhileLoop(JCWhileLoop tree) {
aoqi@0 590 printTree("cond", tree.cond);
aoqi@0 591 printTree("body", tree.body);
aoqi@0 592 }
aoqi@0 593
aoqi@0 594 @Override
aoqi@0 595 public void visitForLoop(JCForLoop tree) {
aoqi@0 596 printList("init", tree.init);
aoqi@0 597 printTree("cond", tree.cond);
aoqi@0 598 printList("step", tree.step);
aoqi@0 599 printTree("body", tree.body);
aoqi@0 600 }
aoqi@0 601
aoqi@0 602 @Override
aoqi@0 603 public void visitForeachLoop(JCEnhancedForLoop tree) {
aoqi@0 604 printTree("var", tree.var);
aoqi@0 605 printTree("expr", tree.expr);
aoqi@0 606 printTree("body", tree.body);
aoqi@0 607 }
aoqi@0 608
aoqi@0 609 @Override
aoqi@0 610 public void visitLabelled(JCLabeledStatement tree) {
aoqi@0 611 printTree("body", tree.body);
aoqi@0 612 }
aoqi@0 613
aoqi@0 614 @Override
aoqi@0 615 public void visitSwitch(JCSwitch tree) {
aoqi@0 616 printTree("selector", tree.selector);
aoqi@0 617 printList("cases", tree.cases);
aoqi@0 618 }
aoqi@0 619
aoqi@0 620 @Override
aoqi@0 621 public void visitCase(JCCase tree) {
aoqi@0 622 printTree("pat", tree.pat);
aoqi@0 623 printList("stats", tree.stats);
aoqi@0 624 }
aoqi@0 625
aoqi@0 626 @Override
aoqi@0 627 public void visitSynchronized(JCSynchronized tree) {
aoqi@0 628 printTree("lock", tree.lock);
aoqi@0 629 printTree("body", tree.body);
aoqi@0 630 }
aoqi@0 631
aoqi@0 632 @Override
aoqi@0 633 public void visitTry(JCTry tree) {
aoqi@0 634 printList("resources", tree.resources);
aoqi@0 635 printTree("body", tree.body);
aoqi@0 636 printList("catchers", tree.catchers);
aoqi@0 637 printTree("finalizer", tree.finalizer);
aoqi@0 638 }
aoqi@0 639
aoqi@0 640 @Override
aoqi@0 641 public void visitCatch(JCCatch tree) {
aoqi@0 642 printTree("param", tree.param);
aoqi@0 643 printTree("body", tree.body);
aoqi@0 644 }
aoqi@0 645
aoqi@0 646 @Override
aoqi@0 647 public void visitConditional(JCConditional tree) {
aoqi@0 648 printTree("cond", tree.cond);
aoqi@0 649 printTree("truepart", tree.truepart);
aoqi@0 650 printTree("falsepart", tree.falsepart);
aoqi@0 651 }
aoqi@0 652
aoqi@0 653 @Override
aoqi@0 654 public void visitIf(JCIf tree) {
aoqi@0 655 printTree("cond", tree.cond);
aoqi@0 656 printTree("thenpart", tree.thenpart);
aoqi@0 657 printTree("elsepart", tree.elsepart);
aoqi@0 658 }
aoqi@0 659
aoqi@0 660 @Override
aoqi@0 661 public void visitExec(JCExpressionStatement tree) {
aoqi@0 662 printTree("expr", tree.expr);
aoqi@0 663 }
aoqi@0 664
aoqi@0 665 @Override
aoqi@0 666 public void visitBreak(JCBreak tree) {
aoqi@0 667 printName("label", tree.label);
aoqi@0 668 }
aoqi@0 669
aoqi@0 670 @Override
aoqi@0 671 public void visitContinue(JCContinue tree) {
aoqi@0 672 printName("label", tree.label);
aoqi@0 673 }
aoqi@0 674
aoqi@0 675 @Override
aoqi@0 676 public void visitReturn(JCReturn tree) {
aoqi@0 677 printTree("expr", tree.expr);
aoqi@0 678 }
aoqi@0 679
aoqi@0 680 @Override
aoqi@0 681 public void visitThrow(JCThrow tree) {
aoqi@0 682 printTree("expr", tree.expr);
aoqi@0 683 }
aoqi@0 684
aoqi@0 685 @Override
aoqi@0 686 public void visitAssert(JCAssert tree) {
aoqi@0 687 printTree("cond", tree.cond);
aoqi@0 688 printTree("detail", tree.detail);
aoqi@0 689 }
aoqi@0 690
aoqi@0 691 @Override
aoqi@0 692 public void visitApply(JCMethodInvocation tree) {
aoqi@0 693 printList("typeargs", tree.typeargs);
aoqi@0 694 printTree("meth", tree.meth);
aoqi@0 695 printList("args", tree.args);
aoqi@0 696 }
aoqi@0 697
aoqi@0 698 @Override
aoqi@0 699 public void visitNewClass(JCNewClass tree) {
aoqi@0 700 printTree("encl", tree.encl);
aoqi@0 701 printList("typeargs", tree.typeargs);
aoqi@0 702 printTree("clazz", tree.clazz);
aoqi@0 703 printList("args", tree.args);
aoqi@0 704 printTree("def", tree.def);
aoqi@0 705 }
aoqi@0 706
aoqi@0 707 @Override
aoqi@0 708 public void visitNewArray(JCNewArray tree) {
aoqi@0 709 printList("annotations", tree.annotations);
aoqi@0 710 printTree("elemtype", tree.elemtype);
aoqi@0 711 printList("dims", tree.dims);
aoqi@0 712 printList("dimAnnotations", tree.dimAnnotations);
aoqi@0 713 printList("elems", tree.elems);
aoqi@0 714 }
aoqi@0 715
aoqi@0 716 @Override
aoqi@0 717 public void visitLambda(JCLambda tree) {
aoqi@0 718 printTree("body", tree.body);
aoqi@0 719 printList("params", tree.params);
aoqi@0 720 }
aoqi@0 721
aoqi@0 722 @Override
aoqi@0 723 public void visitParens(JCParens tree) {
aoqi@0 724 printTree("expr", tree.expr);
aoqi@0 725 }
aoqi@0 726
aoqi@0 727 @Override
aoqi@0 728 public void visitAssign(JCAssign tree) {
aoqi@0 729 printTree("lhs", tree.lhs);
aoqi@0 730 printTree("rhs", tree.rhs);
aoqi@0 731 }
aoqi@0 732
aoqi@0 733 @Override
aoqi@0 734 public void visitAssignop(JCAssignOp tree) {
aoqi@0 735 printTree("lhs", tree.lhs);
aoqi@0 736 printTree("rhs", tree.rhs);
aoqi@0 737 }
aoqi@0 738
aoqi@0 739 @Override
aoqi@0 740 public void visitUnary(JCUnary tree) {
aoqi@0 741 printTree("arg", tree.arg);
aoqi@0 742 }
aoqi@0 743
aoqi@0 744 @Override
aoqi@0 745 public void visitBinary(JCBinary tree) {
aoqi@0 746 printTree("lhs", tree.lhs);
aoqi@0 747 printTree("rhs", tree.rhs);
aoqi@0 748 }
aoqi@0 749
aoqi@0 750 @Override
aoqi@0 751 public void visitTypeCast(JCTypeCast tree) {
aoqi@0 752 printTree("clazz", tree.clazz);
aoqi@0 753 printTree("expr", tree.expr);
aoqi@0 754 }
aoqi@0 755
aoqi@0 756 @Override
aoqi@0 757 public void visitTypeTest(JCInstanceOf tree) {
aoqi@0 758 printTree("expr", tree.expr);
aoqi@0 759 printTree("clazz", tree.clazz);
aoqi@0 760 }
aoqi@0 761
aoqi@0 762 @Override
aoqi@0 763 public void visitIndexed(JCArrayAccess tree) {
aoqi@0 764 printTree("indexed", tree.indexed);
aoqi@0 765 printTree("index", tree.index);
aoqi@0 766 }
aoqi@0 767
aoqi@0 768 @Override
aoqi@0 769 public void visitSelect(JCFieldAccess tree) {
aoqi@0 770 printTree("selected", tree.selected);
aoqi@0 771 }
aoqi@0 772
aoqi@0 773 @Override
aoqi@0 774 public void visitReference(JCMemberReference tree) {
aoqi@0 775 printTree("expr", tree.expr);
aoqi@0 776 printList("typeargs", tree.typeargs);
aoqi@0 777 }
aoqi@0 778
aoqi@0 779 @Override
aoqi@0 780 public void visitIdent(JCIdent tree) {
aoqi@0 781 printName("name", tree.name);
aoqi@0 782 }
aoqi@0 783
aoqi@0 784 @Override
aoqi@0 785 public void visitLiteral(JCLiteral tree) {
aoqi@0 786 printString("value", Pretty.toSimpleString(tree, 32));
aoqi@0 787 }
aoqi@0 788
aoqi@0 789 @Override
aoqi@0 790 public void visitTypeIdent(JCPrimitiveTypeTree tree) {
aoqi@0 791 printString("typetag", tree.typetag.name());
aoqi@0 792 }
aoqi@0 793
aoqi@0 794 @Override
aoqi@0 795 public void visitTypeArray(JCArrayTypeTree tree) {
aoqi@0 796 printTree("elemtype", tree.elemtype);
aoqi@0 797 }
aoqi@0 798
aoqi@0 799 @Override
aoqi@0 800 public void visitTypeApply(JCTypeApply tree) {
aoqi@0 801 printTree("clazz", tree.clazz);
aoqi@0 802 printList("arguments", tree.arguments);
aoqi@0 803 }
aoqi@0 804
aoqi@0 805 @Override
aoqi@0 806 public void visitTypeUnion(JCTypeUnion tree) {
aoqi@0 807 printList("alternatives", tree.alternatives);
aoqi@0 808 }
aoqi@0 809
aoqi@0 810 @Override
aoqi@0 811 public void visitTypeIntersection(JCTypeIntersection tree) {
aoqi@0 812 printList("bounds", tree.bounds);
aoqi@0 813 }
aoqi@0 814
aoqi@0 815 @Override
aoqi@0 816 public void visitTypeParameter(JCTypeParameter tree) {
aoqi@0 817 printName("name", tree.name);
aoqi@0 818 printList("annotations", tree.annotations);
aoqi@0 819 printList("bounds", tree.bounds);
aoqi@0 820 }
aoqi@0 821
aoqi@0 822 @Override
aoqi@0 823 public void visitWildcard(JCWildcard tree) {
aoqi@0 824 printTree("kind", tree.kind);
aoqi@0 825 printTree("inner", tree.inner);
aoqi@0 826 }
aoqi@0 827
aoqi@0 828 @Override
aoqi@0 829 public void visitTypeBoundKind(TypeBoundKind tree) {
aoqi@0 830 printString("kind", tree.kind.name());
aoqi@0 831 }
aoqi@0 832
aoqi@0 833 @Override
aoqi@0 834 public void visitModifiers(JCModifiers tree) {
aoqi@0 835 printList("annotations", tree.annotations);
aoqi@0 836 printString("flags", String.valueOf(Flags.asFlagSet(tree.flags)));
aoqi@0 837 }
aoqi@0 838
aoqi@0 839 @Override
aoqi@0 840 public void visitAnnotation(JCAnnotation tree) {
aoqi@0 841 printTree("annotationType", tree.annotationType);
aoqi@0 842 printList("args", tree.args);
aoqi@0 843 }
aoqi@0 844
aoqi@0 845 @Override
aoqi@0 846 public void visitAnnotatedType(JCAnnotatedType tree) {
aoqi@0 847 printList("annotations", tree.annotations);
aoqi@0 848 printTree("underlyingType", tree.underlyingType);
aoqi@0 849 }
aoqi@0 850
aoqi@0 851 @Override
aoqi@0 852 public void visitErroneous(JCErroneous tree) {
aoqi@0 853 printList("errs", tree.errs);
aoqi@0 854 }
aoqi@0 855
aoqi@0 856 @Override
aoqi@0 857 public void visitLetExpr(LetExpr tree) {
aoqi@0 858 printList("defs", tree.defs);
aoqi@0 859 printTree("expr", tree.expr);
aoqi@0 860 }
aoqi@0 861
aoqi@0 862 @Override
aoqi@0 863 public void visitTree(JCTree tree) {
aoqi@0 864 Assert.error();
aoqi@0 865 }
aoqi@0 866 }
aoqi@0 867
aoqi@0 868 // </editor-fold>
aoqi@0 869
aoqi@0 870 // <editor-fold defaultstate="collapsed" desc="Symbol visitor">
aoqi@0 871
aoqi@0 872 protected Symbol.Visitor<Void,Void> symVisitor = new SymbolVisitor();
aoqi@0 873
aoqi@0 874 /**
aoqi@0 875 * Default visitor class for Symbol objects.
aoqi@0 876 * Note: each visitXYZ method ends by calling the corresponding
aoqi@0 877 * visit method for its superclass.
aoqi@0 878 */
aoqi@0 879 class SymbolVisitor implements Symbol.Visitor<Void,Void> {
aoqi@0 880 @Override
aoqi@0 881 public Void visitClassSymbol(ClassSymbol sym, Void ignore) {
aoqi@0 882 printName("fullname", sym.fullname);
aoqi@0 883 printName("flatname", sym.flatname);
aoqi@0 884 printScope("members", sym.members_field);
aoqi@0 885 printFileObject("sourcefile", sym.sourcefile);
aoqi@0 886 printFileObject("classfile", sym.classfile);
aoqi@0 887 // trans-local?
aoqi@0 888 // pool?
aoqi@0 889 return visitTypeSymbol(sym, null);
aoqi@0 890 }
aoqi@0 891
aoqi@0 892 @Override
aoqi@0 893 public Void visitMethodSymbol(MethodSymbol sym, Void ignore) {
aoqi@0 894 // code
aoqi@0 895 printList("params", sym.params);
aoqi@0 896 printList("savedParameterNames", sym.savedParameterNames);
aoqi@0 897 return visitSymbol(sym, null);
aoqi@0 898 }
aoqi@0 899
aoqi@0 900 @Override
aoqi@0 901 public Void visitPackageSymbol(PackageSymbol sym, Void ignore) {
aoqi@0 902 printName("fullname", sym.fullname);
aoqi@0 903 printScope("members", sym.members_field);
aoqi@0 904 printSymbol("package-info", sym.package_info, Details.SUMMARY);
aoqi@0 905 return visitTypeSymbol(sym, null);
aoqi@0 906 }
aoqi@0 907
aoqi@0 908 @Override
aoqi@0 909 public Void visitOperatorSymbol(OperatorSymbol sym, Void ignore) {
aoqi@0 910 printInt("opcode", sym.opcode);
aoqi@0 911 return visitMethodSymbol(sym, null);
aoqi@0 912 }
aoqi@0 913
aoqi@0 914 @Override
aoqi@0 915 public Void visitVarSymbol(VarSymbol sym, Void ignore) {
aoqi@0 916 printInt("pos", sym.pos);
aoqi@0 917 printInt("adm", sym.adr);
aoqi@0 918 // data is a private field, and the standard accessors may
aoqi@0 919 // mutate it as part of lazy evaluation. Therefore, use
aoqi@0 920 // reflection to get the raw data.
aoqi@0 921 printObject("data", getField(sym, VarSymbol.class, "data"), Details.SUMMARY);
aoqi@0 922 return visitSymbol(sym, null);
aoqi@0 923 }
aoqi@0 924
aoqi@0 925 @Override
aoqi@0 926 public Void visitTypeSymbol(TypeSymbol sym, Void ignore) {
aoqi@0 927 return visitSymbol(sym, null);
aoqi@0 928 }
aoqi@0 929
aoqi@0 930 @Override
aoqi@0 931 public Void visitSymbol(Symbol sym, Void ignore) {
aoqi@0 932 return null;
aoqi@0 933 }
aoqi@0 934 }
aoqi@0 935
aoqi@0 936 // </editor-fold>
aoqi@0 937
aoqi@0 938 // <editor-fold defaultstate="collapsed" desc="Type visitor">
aoqi@0 939
aoqi@0 940 protected Type.Visitor<Void,Void> typeVisitor = new TypeVisitor();
aoqi@0 941
aoqi@0 942 /**
aoqi@0 943 * Default visitor class for Type objects.
aoqi@0 944 * Note: each visitXYZ method ends by calling the corresponding
aoqi@0 945 * visit method for its superclass.
aoqi@0 946 */
aoqi@0 947 public class TypeVisitor implements Type.Visitor<Void,Void> {
aoqi@0 948 public Void visitAnnotatedType(AnnotatedType type, Void ignore) {
aoqi@0 949 printList("typeAnnotations", type.getAnnotationMirrors());
aoqi@0 950 printType("underlyingType", type.unannotatedType(), Details.FULL);
aoqi@0 951 return visitType(type, null);
aoqi@0 952 }
aoqi@0 953
aoqi@0 954 public Void visitArrayType(ArrayType type, Void ignore) {
aoqi@0 955 printType("elemType", type.elemtype, Details.FULL);
aoqi@0 956 return visitType(type, null);
aoqi@0 957 }
aoqi@0 958
aoqi@0 959 public Void visitCapturedType(CapturedType type, Void ignore) {
aoqi@0 960 printType("wildcard", type.wildcard, Details.FULL);
aoqi@0 961 return visitTypeVar(type, null);
aoqi@0 962 }
aoqi@0 963
aoqi@0 964 public Void visitClassType(ClassType type, Void ignore) {
aoqi@0 965 printType("outer", type.getEnclosingType(), Details.SUMMARY);
aoqi@0 966 printList("typarams", type.typarams_field);
aoqi@0 967 printList("allparams", type.allparams_field);
aoqi@0 968 printType("supertype", type.supertype_field, Details.SUMMARY);
aoqi@0 969 printList("interfaces", type.interfaces_field);
aoqi@0 970 printList("allinterfaces", type.all_interfaces_field);
aoqi@0 971 return visitType(type, null);
aoqi@0 972 }
aoqi@0 973
aoqi@0 974 public Void visitErrorType(ErrorType type, Void ignore) {
aoqi@0 975 printType("originalType", type.getOriginalType(), Details.FULL);
aoqi@0 976 return visitClassType(type, null);
aoqi@0 977 }
aoqi@0 978
aoqi@0 979 public Void visitForAll(ForAll type, Void ignore) {
aoqi@0 980 printList("tvars", type.tvars);
aoqi@0 981 return visitDelegatedType(type);
aoqi@0 982 }
aoqi@0 983
aoqi@0 984 public Void visitMethodType(MethodType type, Void ignore) {
aoqi@0 985 printList("argtypes", type.argtypes);
aoqi@0 986 printType("restype", type.restype, Details.FULL);
aoqi@0 987 printList("thrown", type.thrown);
aoqi@0 988 return visitType(type, null);
aoqi@0 989 }
aoqi@0 990
aoqi@0 991 public Void visitPackageType(PackageType type, Void ignore) {
aoqi@0 992 return visitType(type, null);
aoqi@0 993 }
aoqi@0 994
aoqi@0 995 public Void visitTypeVar(TypeVar type, Void ignore) {
aoqi@0 996 // For TypeVars (and not subtypes), the bound should always be
aoqi@0 997 // null or bot. So, only print the bound for subtypes of TypeVar,
aoqi@0 998 // or if the bound is (erroneously) not null or bot.
aoqi@0 999 if (!type.hasTag(TypeTag.TYPEVAR)
aoqi@0 1000 || !(type.bound == null || type.bound.hasTag(TypeTag.BOT))) {
aoqi@0 1001 printType("bound", type.bound, Details.FULL);
aoqi@0 1002 }
aoqi@0 1003 printType("lower", type.lower, Details.FULL);
aoqi@0 1004 return visitType(type, null);
aoqi@0 1005 }
aoqi@0 1006
aoqi@0 1007 public Void visitUndetVar(UndetVar type, Void ignore) {
aoqi@0 1008 for (UndetVar.InferenceBound ib: UndetVar.InferenceBound.values())
aoqi@0 1009 printList("bounds." + ib, type.getBounds(ib));
aoqi@0 1010 printInt("declaredCount", type.declaredCount);
aoqi@0 1011 printType("inst", type.inst, Details.SUMMARY);
aoqi@0 1012 return visitDelegatedType(type);
aoqi@0 1013 }
aoqi@0 1014
aoqi@0 1015 public Void visitWildcardType(WildcardType type, Void ignore) {
aoqi@0 1016 printType("type", type.type, Details.SUMMARY);
aoqi@0 1017 printString("kind", type.kind.name());
aoqi@0 1018 printType("bound", type.bound, Details.SUMMARY);
aoqi@0 1019 return visitType(type, null);
aoqi@0 1020 }
aoqi@0 1021
aoqi@0 1022 protected Void visitDelegatedType(DelegatedType type) {
aoqi@0 1023 printType("qtype", type.qtype, Details.FULL);
aoqi@0 1024 return visitType(type, null);
aoqi@0 1025 }
aoqi@0 1026
aoqi@0 1027 public Void visitType(Type type, Void ignore) {
aoqi@0 1028 return null;
aoqi@0 1029 }
aoqi@0 1030 }
aoqi@0 1031
aoqi@0 1032 // </editor-fold>
aoqi@0 1033
aoqi@0 1034 // <editor-fold defaultstate="collapsed" desc="Attribute (annotations) visitor">
aoqi@0 1035
aoqi@0 1036 protected Attribute.Visitor attrVisitor = new AttributeVisitor();
aoqi@0 1037
aoqi@0 1038 /**
aoqi@0 1039 * Default visitor class for Attribute (annotation) objects.
aoqi@0 1040 */
aoqi@0 1041 public class AttributeVisitor implements Attribute.Visitor {
aoqi@0 1042
aoqi@0 1043 public void visitConstant(Attribute.Constant a) {
aoqi@0 1044 printObject("value", a.value, Details.SUMMARY);
aoqi@0 1045 visitAttribute(a);
aoqi@0 1046 }
aoqi@0 1047
aoqi@0 1048 public void visitClass(Attribute.Class a) {
aoqi@0 1049 printObject("classType", a.classType, Details.SUMMARY);
aoqi@0 1050 visitAttribute(a);
aoqi@0 1051 }
aoqi@0 1052
aoqi@0 1053 public void visitCompound(Attribute.Compound a) {
aoqi@0 1054 if (a instanceof Attribute.TypeCompound) {
aoqi@0 1055 Attribute.TypeCompound ta = (Attribute.TypeCompound) a;
aoqi@0 1056 // consider a custom printer?
aoqi@0 1057 printObject("position", ta.position, Details.SUMMARY);
aoqi@0 1058 }
aoqi@0 1059 printObject("synthesized", a.isSynthesized(), Details.SUMMARY);
aoqi@0 1060 printList("values", a.values);
aoqi@0 1061 visitAttribute(a);
aoqi@0 1062 }
aoqi@0 1063
aoqi@0 1064 public void visitArray(Attribute.Array a) {
aoqi@0 1065 printList("values", Arrays.asList(a.values));
aoqi@0 1066 visitAttribute(a);
aoqi@0 1067 }
aoqi@0 1068
aoqi@0 1069 public void visitEnum(Attribute.Enum a) {
aoqi@0 1070 printSymbol("value", a.value, Details.SUMMARY);
aoqi@0 1071 visitAttribute(a);
aoqi@0 1072 }
aoqi@0 1073
aoqi@0 1074 public void visitError(Attribute.Error a) {
aoqi@0 1075 visitAttribute(a);
aoqi@0 1076 }
aoqi@0 1077
aoqi@0 1078 public void visitAttribute(Attribute a) {
aoqi@0 1079 printType("type", a.type, Details.SUMMARY);
aoqi@0 1080 }
aoqi@0 1081
aoqi@0 1082 }
aoqi@0 1083 // </editor-fold>
aoqi@0 1084
aoqi@0 1085 // <editor-fold defaultstate="collapsed" desc="Utility front end">
aoqi@0 1086
aoqi@0 1087 /**
aoqi@0 1088 * Utility class to invoke DPrinter from the command line.
aoqi@0 1089 */
aoqi@0 1090 static class Main {
aoqi@0 1091 public static void main(String... args) throws IOException {
aoqi@0 1092 Main m = new Main();
aoqi@0 1093 PrintWriter out = new PrintWriter(System.out);
aoqi@0 1094 try {
aoqi@0 1095 if (args.length == 0)
aoqi@0 1096 m.usage(out);
aoqi@0 1097 else
aoqi@0 1098 m.run(out, args);
aoqi@0 1099 } finally {
aoqi@0 1100 out.flush();
aoqi@0 1101 }
aoqi@0 1102 }
aoqi@0 1103
aoqi@0 1104 void usage(PrintWriter out) {
aoqi@0 1105 out.println("Usage:");
aoqi@0 1106 out.println(" java " + Main.class.getName() + " mode [options] [javac-options]");
aoqi@0 1107 out.print("where mode is one of: ");
aoqi@0 1108 String sep = "";
aoqi@0 1109 for (Handler h: getHandlers().values()) {
aoqi@0 1110 out.print(sep);
aoqi@0 1111 out.print(h.name);
aoqi@0 1112 sep = ", ";
aoqi@0 1113 }
aoqi@0 1114 out.println();
aoqi@0 1115 out.println("and where options include:");
aoqi@0 1116 out.println(" -before PARSE|ENTER|ANALYZE|GENERATE|ANNOTATION_PROCESSING|ANNOTATION_PROCESSING_ROUND");
aoqi@0 1117 out.println(" -after PARSE|ENTER|ANALYZE|GENERATE|ANNOTATION_PROCESSING|ANNOTATION_PROCESSING_ROUND");
aoqi@0 1118 out.println(" -showPositions");
aoqi@0 1119 out.println(" -showSource");
aoqi@0 1120 out.println(" -showTreeSymbols");
aoqi@0 1121 out.println(" -showTreeTypes");
aoqi@0 1122 out.println(" -hideEmptyItems");
aoqi@0 1123 out.println(" -hideNulls");
aoqi@0 1124 }
aoqi@0 1125
aoqi@0 1126 void run(PrintWriter out, String... args) throws IOException {
aoqi@0 1127 JavaCompiler c = ToolProvider.getSystemJavaCompiler();
aoqi@0 1128 StandardJavaFileManager fm = c.getStandardFileManager(null, null, null);
aoqi@0 1129
aoqi@0 1130 // DPrinter options
aoqi@0 1131 final Set<TaskEvent.Kind> before = EnumSet.noneOf(TaskEvent.Kind.class);
aoqi@0 1132 final Set<TaskEvent.Kind> after = EnumSet.noneOf(TaskEvent.Kind.class);
aoqi@0 1133 boolean showPositions = false;
aoqi@0 1134 boolean showSource = false;
aoqi@0 1135 boolean showTreeSymbols = false;
aoqi@0 1136 boolean showTreeTypes = false;
aoqi@0 1137 boolean showEmptyItems = true;
aoqi@0 1138 boolean showNulls = true;
aoqi@0 1139
aoqi@0 1140 // javac options
aoqi@0 1141 Collection<String> options = new ArrayList<String>();
aoqi@0 1142 Collection<File> files = new ArrayList<File>();
aoqi@0 1143 String classpath = null;
aoqi@0 1144 String classoutdir = null;
aoqi@0 1145
aoqi@0 1146 final Handler h = getHandlers().get(args[0]);
aoqi@0 1147 if (h == null)
aoqi@0 1148 throw new IllegalArgumentException(args[0]);
aoqi@0 1149
aoqi@0 1150 for (int i = 1; i < args.length; i++) {
aoqi@0 1151 String arg = args[i];
aoqi@0 1152 if (arg.equals("-before") && i + 1 < args.length) {
aoqi@0 1153 before.add(getKind(args[++i]));
aoqi@0 1154 } else if (arg.equals("-after") && i + 1 < args.length) {
aoqi@0 1155 after.add(getKind(args[++i]));
aoqi@0 1156 } else if (arg.equals("-showPositions")) {
aoqi@0 1157 showPositions = true;
aoqi@0 1158 } else if (arg.equals("-showSource")) {
aoqi@0 1159 showSource = true;
aoqi@0 1160 } else if (arg.equals("-showTreeSymbols")) {
aoqi@0 1161 showTreeSymbols = true;
aoqi@0 1162 } else if (arg.equals("-showTreeTypes")) {
aoqi@0 1163 showTreeTypes = true;
aoqi@0 1164 } else if (arg.equals("-hideEmptyLists")) {
aoqi@0 1165 showEmptyItems = false;
aoqi@0 1166 } else if (arg.equals("-hideNulls")) {
aoqi@0 1167 showNulls = false;
aoqi@0 1168 } else if (arg.equals("-classpath") && i + 1 < args.length) {
aoqi@0 1169 classpath = args[++i];
aoqi@0 1170 } else if (arg.equals("-d") && i + 1 < args.length) {
aoqi@0 1171 classoutdir = args[++i];
aoqi@0 1172 } else if (arg.startsWith("-")) {
aoqi@0 1173 int n = c.isSupportedOption(arg);
aoqi@0 1174 if (n < 0) throw new IllegalArgumentException(arg);
aoqi@0 1175 options.add(arg);
aoqi@0 1176 while (n > 0) options.add(args[++i]);
aoqi@0 1177 } else if (arg.endsWith(".java")) {
aoqi@0 1178 files.add(new File(arg));
aoqi@0 1179 }
aoqi@0 1180 }
aoqi@0 1181
aoqi@0 1182 if (classoutdir != null) {
aoqi@0 1183 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(classoutdir)));
aoqi@0 1184 }
aoqi@0 1185
aoqi@0 1186 if (classpath != null) {
aoqi@0 1187 Collection<File> path = new ArrayList<File>();
aoqi@0 1188 for (String p: classpath.split(File.pathSeparator)) {
aoqi@0 1189 if (p.isEmpty()) continue;
aoqi@0 1190 File f = new File(p);
aoqi@0 1191 if (f.exists()) path.add(f);
aoqi@0 1192 }
aoqi@0 1193 fm.setLocation(StandardLocation.CLASS_PATH, path);
aoqi@0 1194 }
aoqi@0 1195 Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);
aoqi@0 1196
aoqi@0 1197 JavacTask task = (JavacTask) c.getTask(out, fm, null, options, null, fos);
aoqi@0 1198 final Trees trees = Trees.instance(task);
aoqi@0 1199
aoqi@0 1200 final DPrinter dprinter = new DPrinter(out, trees);
aoqi@0 1201 dprinter.source(showSource)
aoqi@0 1202 .emptyItems(showEmptyItems)
aoqi@0 1203 .nulls(showNulls)
aoqi@0 1204 .positions(showPositions)
aoqi@0 1205 .treeSymbols(showTreeSymbols)
aoqi@0 1206 .treeTypes(showTreeTypes);
aoqi@0 1207
aoqi@0 1208 if (before.isEmpty() && after.isEmpty()) {
aoqi@0 1209 if (h.name.equals("trees") && !showTreeSymbols && !showTreeTypes)
aoqi@0 1210 after.add(TaskEvent.Kind.PARSE);
aoqi@0 1211 else
aoqi@0 1212 after.add(TaskEvent.Kind.ANALYZE);
aoqi@0 1213 }
aoqi@0 1214
aoqi@0 1215 task.addTaskListener(new TaskListener() {
aoqi@0 1216 public void started(TaskEvent e) {
aoqi@0 1217 if (before.contains(e.getKind()))
aoqi@0 1218 handle(e);
aoqi@0 1219 }
aoqi@0 1220
aoqi@0 1221 public void finished(TaskEvent e) {
aoqi@0 1222 if (after.contains(e.getKind()))
aoqi@0 1223 handle(e);
aoqi@0 1224 }
aoqi@0 1225
aoqi@0 1226 private void handle(TaskEvent e) {
aoqi@0 1227 switch (e.getKind()) {
aoqi@0 1228 case PARSE:
aoqi@0 1229 case ENTER:
aoqi@0 1230 h.handle(e.getSourceFile().getName(),
aoqi@0 1231 (JCTree) e.getCompilationUnit(),
aoqi@0 1232 dprinter);
aoqi@0 1233 break;
aoqi@0 1234
aoqi@0 1235 default:
aoqi@0 1236 TypeElement elem = e.getTypeElement();
aoqi@0 1237 h.handle(elem.toString(),
aoqi@0 1238 (JCTree) trees.getTree(elem),
aoqi@0 1239 dprinter);
aoqi@0 1240 break;
aoqi@0 1241 }
aoqi@0 1242 }
aoqi@0 1243 });
aoqi@0 1244
aoqi@0 1245 task.call();
aoqi@0 1246 }
aoqi@0 1247
aoqi@0 1248 TaskEvent.Kind getKind(String s) {
aoqi@0 1249 return TaskEvent.Kind.valueOf(s.toUpperCase());
aoqi@0 1250 }
aoqi@0 1251
aoqi@0 1252 static protected abstract class Handler {
aoqi@0 1253 final String name;
aoqi@0 1254 Handler(String name) {
aoqi@0 1255 this.name = name;
aoqi@0 1256 }
aoqi@0 1257 abstract void handle(String label, JCTree tree, DPrinter dprinter);
aoqi@0 1258 }
aoqi@0 1259
aoqi@0 1260 Map<String,Handler> getHandlers() {
aoqi@0 1261 Map<String,Handler> map = new HashMap<String, Handler>();
aoqi@0 1262 for (Handler h: defaultHandlers) {
aoqi@0 1263 map.put(h.name, h);
aoqi@0 1264 }
aoqi@0 1265 return map;
aoqi@0 1266 }
aoqi@0 1267
aoqi@0 1268 protected final Handler[] defaultHandlers = {
aoqi@0 1269 new Handler("trees") {
aoqi@0 1270 @Override
aoqi@0 1271 void handle(String name, JCTree tree, DPrinter dprinter) {
aoqi@0 1272 dprinter.printTree(name, tree);
aoqi@0 1273 dprinter.out.println();
aoqi@0 1274 }
aoqi@0 1275 },
aoqi@0 1276
aoqi@0 1277 new Handler("symbols") {
aoqi@0 1278 @Override
aoqi@0 1279 void handle(String name, JCTree tree, final DPrinter dprinter) {
aoqi@0 1280 TreeScanner ds = new TreeScanner() {
aoqi@0 1281 @Override
aoqi@0 1282 public void visitClassDef(JCClassDecl tree) {
aoqi@0 1283 visitDecl(tree, tree.sym);
aoqi@0 1284 super.visitClassDef(tree);
aoqi@0 1285 }
aoqi@0 1286
aoqi@0 1287 @Override
aoqi@0 1288 public void visitMethodDef(JCMethodDecl tree) {
aoqi@0 1289 visitDecl(tree, tree.sym);
aoqi@0 1290 super.visitMethodDef(tree);
aoqi@0 1291 }
aoqi@0 1292
aoqi@0 1293 @Override
aoqi@0 1294 public void visitVarDef(JCVariableDecl tree) {
aoqi@0 1295 visitDecl(tree, tree.sym);
aoqi@0 1296 super.visitVarDef(tree);
aoqi@0 1297 }
aoqi@0 1298
aoqi@0 1299 void visitDecl(JCTree tree, Symbol sym) {
aoqi@0 1300 dprinter.printSymbol(sym.name.toString(), sym);
aoqi@0 1301 dprinter.out.println();
aoqi@0 1302 }
aoqi@0 1303 };
aoqi@0 1304 ds.scan(tree);
aoqi@0 1305 }
aoqi@0 1306 },
aoqi@0 1307
aoqi@0 1308 new Handler("types") {
aoqi@0 1309 @Override
aoqi@0 1310 void handle(String name, JCTree tree, final DPrinter dprinter) {
aoqi@0 1311 TreeScanner ts = new TreeScanner() {
aoqi@0 1312 @Override
aoqi@0 1313 public void scan(JCTree tree) {
aoqi@0 1314 if (tree == null) {
aoqi@0 1315 return;
aoqi@0 1316 }
aoqi@0 1317 if (tree.type != null) {
aoqi@0 1318 String label = Pretty.toSimpleString(tree);
aoqi@0 1319 dprinter.printType(label, tree.type);
aoqi@0 1320 dprinter.out.println();
aoqi@0 1321 }
aoqi@0 1322 super.scan(tree);
aoqi@0 1323 }
aoqi@0 1324 };
aoqi@0 1325 ts.scan(tree);
aoqi@0 1326 }
aoqi@0 1327 }
aoqi@0 1328 };
aoqi@0 1329 }
aoqi@0 1330
aoqi@0 1331 // </editor-fold>
aoqi@0 1332
aoqi@0 1333 }

mercurial