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