test/tools/javac/lib/DPrinter.java

Tue, 15 Oct 2013 15:57:13 -0700

author
jjg
date
Tue, 15 Oct 2013 15:57:13 -0700
changeset 2134
b0c086cd4520
parent 2014
6cffcd15a17e
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8026564: import changes from type-annotations forest
Reviewed-by: jjg
Contributed-by: wdietl@gmail.com, steve.sides@oracle.com

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

mercurial