KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > builder > DumpAST


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.source.builder;
20
21 import org.netbeans.modules.java.source.engine.RootTree;
22 import org.netbeans.api.java.source.query.CommentHandler;
23
24 import com.sun.source.tree.*;
25 import com.sun.source.util.TreeScanner;
26
27 import javax.lang.model.element.Modifier;
28 import javax.lang.model.type.NullType;
29
30 import java.io.PrintStream JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Set JavaDoc;
33 import org.netbeans.api.java.source.query.CommentSet;
34
35 /**
36  * Print out a tree in a text format, suitable for debugging.
37  */

38 public final class DumpAST extends TreeScanner<Object JavaDoc,Object JavaDoc> {
39     CommentHandler comments;
40     PrintStream JavaDoc out;
41     int indent;
42     
43     public static void dump(Tree tree, CommentHandler comments, PrintStream JavaDoc out) {
44         tree.accept(new DumpAST(out, comments), null);
45     }
46
47     DumpAST(PrintStream JavaDoc out, CommentHandler comments) {
48         this.out = out;
49         this.comments = comments;
50         indent = 0;
51     }
52     
53     private void indent() {
54         for (int i = 0; i < indent; i++)
55             out.print(". ");
56     }
57     
58     private void dump(String JavaDoc name, Tree tree) {
59         indent++;
60         indent();
61         out.print(name + ": ");
62         if (tree != null) {
63             out.println();
64             indent++;
65             scan(tree, null);
66             indent--;
67         }
68         else
69             out.println("null");
70         indent--;
71     }
72     
73     private void dump(String JavaDoc name, List JavaDoc<? extends Tree> trees) {
74         indent++;
75         indent();
76         out.print(name);
77         if (trees == null)
78             out.println(": null");
79         else if (trees.isEmpty())
80             out.println(" {}");
81         else {
82             out.println(" {");
83             indent++;
84             for (Tree t : trees)
85                 scan(t, null);
86             indent--;
87             indent();
88             out.println("}");
89         }
90         indent--;
91     }
92     
93     private void dumpField(String JavaDoc name, Object JavaDoc o) {
94         indent++;
95         indent();
96         out.println(name + ": " + (o != null ? o.toString() : "null"));
97         indent--;
98     }
99     
100     private void dumpFlags(Set JavaDoc<Modifier> flags) {
101     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
102         for (Modifier mod : flags) {
103             buf.append(mod.name());
104             buf.append(' ');
105         }
106         dumpField("flags", buf.toString().trim());
107     }
108     
109     private void header(String JavaDoc s, Tree tree) {
110         indent();
111         out.println(s);
112         dumpField("pos", Integer.toString(((com.sun.tools.javac.tree.JCTree)tree).pos));
113         if (comments.hasComments((com.sun.tools.javac.tree.JCTree)tree)) {
114             CommentSet cs = comments.getComments((com.sun.tools.javac.tree.JCTree)tree);
115             dumpField("comments", cs.toString());
116         }
117     }
118     
119     @Override JavaDoc
120     public Object JavaDoc visitCompilationUnit(CompilationUnitTree tree, Object JavaDoc o) {
121         header("CompilationUnit", tree);
122         dump("packageAnnotations", tree.getPackageAnnotations());
123         dump("pid", tree.getPackageName());
124         dumpField("sourcefile", tree.getSourceFile());
125         dumpField("namedImportScope", tree.getImports());
126         dump("defs", tree.getTypeDecls());
127         return o;
128     }
129
130     @Override JavaDoc
131     public Object JavaDoc visitImport(ImportTree tree, Object JavaDoc o) {
132         header("Import", tree);
133         dumpField("staticImport", Boolean.toString(tree.isStatic()));
134     dump("qualid", tree.getQualifiedIdentifier());
135         return o;
136     }
137
138     @Override JavaDoc
139     public Object JavaDoc visitClass(ClassTree tree, Object JavaDoc o) {
140         header("Class", tree);
141         dumpField("name", tree.getSimpleName());
142         dump("mods", tree.getModifiers());
143         dump("typarams", tree.getTypeParameters());
144         dump("extending", tree.getExtendsClause());
145         dump("implementing", tree.getImplementsClause());
146         dump("defs", tree.getMembers());
147         return o;
148     }
149
150     @Override JavaDoc
151     public Object JavaDoc visitMethod(MethodTree tree, Object JavaDoc o) {
152         header("Method", tree);
153         dumpField("name", tree.getName());
154         dump("mods", tree.getModifiers());
155         dump("restype", tree.getReturnType());
156         dump("typarams", tree.getTypeParameters());
157         dump("params", tree.getParameters());
158         dump("thrown", tree.getThrows());
159         dump("defaultValue", tree.getDefaultValue());
160         dump("body", tree.getBody());
161         return o;
162     }
163     
164     @Override JavaDoc
165     public Object JavaDoc visitVariable(VariableTree tree, Object JavaDoc o) {
166         header("Variable", tree);
167         dumpField("name", tree.getName());
168     dump("mods", tree.getModifiers());
169     dump("vartype", tree.getType());
170     dump("init", tree.getInitializer());
171         return o;
172     }
173     
174     @Override JavaDoc
175     public Object JavaDoc visitEmptyStatement(EmptyStatementTree tree, Object JavaDoc o) {
176         header("EmptyStatement", tree);
177         return o;
178     }
179
180     @Override JavaDoc
181     public Object JavaDoc visitBlock(BlockTree tree, Object JavaDoc o) {
182         header("Block", tree);
183         dumpField("(flags & Static)", tree.isStatic());
184     dump("stats", tree.getStatements());
185         return o;
186     }
187
188     @Override JavaDoc
189     public Object JavaDoc visitDoWhileLoop(DoWhileLoopTree tree, Object JavaDoc o) {
190         header("DoWhileLoop", tree);
191     dump("body", tree.getStatement());
192     dump("cond", tree.getCondition());
193         return o;
194     }
195
196     @Override JavaDoc
197     public Object JavaDoc visitWhileLoop(WhileLoopTree tree, Object JavaDoc o) {
198         header("WhileLoop", tree);
199     dump("cond", tree.getCondition());
200     dump("body", tree.getStatement());
201         return o;
202     }
203
204     @Override JavaDoc
205     public Object JavaDoc visitForLoop(ForLoopTree tree, Object JavaDoc o) {
206         header("ForLoop", tree);
207     dump("init", tree.getInitializer());
208     dump("cond", tree.getCondition());
209     dump("step", tree.getUpdate());
210     dump("body", tree.getStatement());
211         return o;
212     }
213
214     @Override JavaDoc
215     public Object JavaDoc visitEnhancedForLoop(EnhancedForLoopTree tree, Object JavaDoc o) {
216         header("EnhancedForLoop", tree);
217     dump("var", tree.getVariable());
218     dump("expr", tree.getExpression());
219     dump("body", tree.getStatement());
220         return o;
221     }
222
223     @Override JavaDoc
224     public Object JavaDoc visitLabeledStatement(LabeledStatementTree tree, Object JavaDoc o) {
225         header("LabeledStatement", tree);
226         dumpField("label", tree.getLabel());
227     dump("body", tree.getStatement());
228         return o;
229     }
230
231     @Override JavaDoc
232     public Object JavaDoc visitSwitch(SwitchTree tree, Object JavaDoc o) {
233         header("Switch", tree);
234     dump("selector", tree.getExpression());
235     dump("cases", tree.getCases());
236         return o;
237     }
238
239     @Override JavaDoc
240     public Object JavaDoc visitCase(CaseTree tree, Object JavaDoc o) {
241         header("Case", tree);
242     dump("pat", tree.getExpression());
243     dump("stats", tree.getStatements());
244         return o;
245     }
246
247     @Override JavaDoc
248     public Object JavaDoc visitSynchronized(SynchronizedTree tree, Object JavaDoc o) {
249         header("Synchronized", tree);
250     dump("lock", tree.getExpression());
251     dump("body", tree.getBlock());
252         return o;
253     }
254
255     @Override JavaDoc
256     public Object JavaDoc visitTry(TryTree tree, Object JavaDoc o) {
257         header("Try", tree);
258     dump("body", tree.getBlock());
259     dump("catchers", tree.getCatches());
260     dump("finalizer", tree.getFinallyBlock());
261         return o;
262     }
263
264     @Override JavaDoc
265     public Object JavaDoc visitCatch(CatchTree tree, Object JavaDoc o) {
266         header("Catch", tree);
267     dump("param", tree.getParameter());
268     dump("body", tree.getBlock());
269         return o;
270     }
271
272     @Override JavaDoc
273     public Object JavaDoc visitConditionalExpression(ConditionalExpressionTree tree, Object JavaDoc o) {
274         header("ConditionalExpression", tree);
275     dump("cond", tree.getCondition());
276     dump("truepart", tree.getTrueExpression());
277     dump("falsepart", tree.getFalseExpression());
278         return o;
279     }
280
281     @Override JavaDoc
282     public Object JavaDoc visitIf(IfTree tree, Object JavaDoc o) {
283         header("If", tree);
284     dump("cond", tree.getCondition());
285     dump("thenpart", tree.getThenStatement());
286     dump("elsepart", tree.getElseStatement());
287         return o;
288     }
289
290     @Override JavaDoc
291     public Object JavaDoc visitExpressionStatement(ExpressionStatementTree tree, Object JavaDoc o) {
292         header("ExpressionStatement", tree);
293         dump("expr", tree.getExpression());
294         return o;
295     }
296
297     @Override JavaDoc
298     public Object JavaDoc visitBreak(BreakTree tree, Object JavaDoc o) {
299         header("Break", tree);
300         dumpField("label", tree.getLabel());
301         return o;
302     }
303
304     @Override JavaDoc
305     public Object JavaDoc visitContinue(ContinueTree tree, Object JavaDoc o) {
306         header("Continue", tree);
307         dumpField("label", tree.getLabel());
308         return o;
309     }
310
311     @Override JavaDoc
312     public Object JavaDoc visitReturn(ReturnTree tree, Object JavaDoc o) {
313         header("Return", tree);
314     dump("expr", tree.getExpression());
315         return o;
316     }
317
318     @Override JavaDoc
319     public Object JavaDoc visitThrow(ThrowTree tree, Object JavaDoc o) {
320         header("Throw", tree);
321     dump("expr", tree.getExpression());
322         return o;
323     }
324
325     @Override JavaDoc
326     public Object JavaDoc visitAssert(AssertTree tree, Object JavaDoc o) {
327         header("Assert", tree);
328     dump("cond", tree.getCondition());
329     dump("detail", tree.getDetail());
330         return o;
331     }
332
333     @Override JavaDoc
334     public Object JavaDoc visitMethodInvocation(MethodInvocationTree tree, Object JavaDoc o) {
335         header("MethodInvocation", tree);
336         dump("typeargs", tree.getTypeArguments());
337     dump("meth", tree.getMethodSelect());
338     dump("args", tree.getArguments());
339         return o;
340     }
341
342     @Override JavaDoc
343     public Object JavaDoc visitNewClass(NewClassTree tree, Object JavaDoc o) {
344         header("NewClass", tree);
345     dump("encl", tree.getEnclosingExpression());
346         dump("typeargs", tree.getTypeArguments());
347     dump("clazz", tree.getIdentifier());
348     dump("args", tree.getArguments());
349     dump("def", tree.getClassBody());
350         return o;
351     }
352
353     @Override JavaDoc
354     public Object JavaDoc visitNewArray(NewArrayTree tree, Object JavaDoc o) {
355         header("NewArray", tree);
356     dump("elemtype", tree.getType());
357     dump("dims", tree.getDimensions());
358     dump("elems", tree.getInitializers());
359         return o;
360     }
361
362     @Override JavaDoc
363     public Object JavaDoc visitParenthesized(ParenthesizedTree tree, Object JavaDoc o) {
364         header("Parenthesized", tree);
365     dump("expr", tree.getExpression());
366         return o;
367     }
368
369     @Override JavaDoc
370     public Object JavaDoc visitAssignment(AssignmentTree tree, Object JavaDoc o) {
371         header("Assignment", tree);
372     dump("lhs", tree.getVariable());
373     dump("rhs", tree.getExpression());
374         return o;
375     }
376
377     @Override JavaDoc
378     public Object JavaDoc visitCompoundAssignment(CompoundAssignmentTree tree, Object JavaDoc o) {
379         header("CompoundAssignment", tree);
380     dump("lhs", tree.getVariable());
381     dump("rhs", tree.getExpression());
382         return o;
383     }
384
385     @Override JavaDoc
386     public Object JavaDoc visitUnary(UnaryTree tree, Object JavaDoc o) {
387         header("Unary", tree);
388     dump("arg", tree.getExpression());
389         return o;
390     }
391
392     @Override JavaDoc
393     public Object JavaDoc visitBinary(BinaryTree tree, Object JavaDoc o) {
394         header("Binary", tree);
395     dump("lhs", tree.getLeftOperand());
396     dump("rhs", tree.getRightOperand());
397         return o;
398     }
399
400     @Override JavaDoc
401     public Object JavaDoc visitTypeCast(TypeCastTree tree, Object JavaDoc o) {
402         header("TypeCast", tree);
403     dump("clazz", tree.getType());
404     dump("expr", tree.getExpression());
405         return o;
406     }
407
408     @Override JavaDoc
409     public Object JavaDoc visitInstanceOf(InstanceOfTree tree, Object JavaDoc o) {
410         header("InstanceOf", tree);
411     dump("expr", tree.getExpression());
412     dump("clazz", tree.getType());
413         return o;
414     }
415
416     @Override JavaDoc
417     public Object JavaDoc visitArrayAccess(ArrayAccessTree tree, Object JavaDoc o) {
418         header("ArrayAccess", tree);
419     dump("indexed", tree.getExpression());
420     dump("index", tree.getIndex());
421         return o;
422     }
423
424     @Override JavaDoc
425     public Object JavaDoc visitMemberSelect(MemberSelectTree tree, Object JavaDoc o) {
426         header("MemberSelect", tree);
427         dumpField("name", tree.getIdentifier());
428     dump("selected", tree.getExpression());
429         return o;
430     }
431
432     @Override JavaDoc
433     public Object JavaDoc visitIdentifier(IdentifierTree tree, Object JavaDoc o) {
434         header("Identifier", tree);
435         dumpField("name", tree.getName());
436         return o;
437     }
438
439     @Override JavaDoc
440     public Object JavaDoc visitLiteral(LiteralTree tree, Object JavaDoc o) {
441         header("Literal", tree);
442         dumpField("typetag", tree.getKind());
443         dumpField("value", tree.getValue());
444         return o;
445     }
446
447     @Override JavaDoc
448     public Object JavaDoc visitPrimitiveType(PrimitiveTypeTree tree, Object JavaDoc o) {
449         header("PrimitiveType", tree);
450         dumpField("typetag", tree.getKind());
451         return o;
452     }
453
454     @Override JavaDoc
455     public Object JavaDoc visitArrayType(ArrayTypeTree tree, Object JavaDoc o) {
456         header("ArrayType", tree);
457     dump("elemtype", tree.getType());
458         return o;
459     }
460
461     @Override JavaDoc
462     public Object JavaDoc visitParameterizedType(ParameterizedTypeTree tree, Object JavaDoc o) {
463         header("ParameterizedType", tree);
464     dump("clazz", tree.getType());
465     dump("arguments", tree.getTypeArguments());
466         return o;
467     }
468
469     @Override JavaDoc
470     public Object JavaDoc visitTypeParameter(TypeParameterTree tree, Object JavaDoc o) {
471         header("TypeParameter", tree);
472         dumpField("name", tree.getName());
473     dump("bounds", tree.getBounds());
474         return o;
475     }
476
477     @Override JavaDoc
478     public Object JavaDoc visitWildcard(WildcardTree tree, Object JavaDoc o) {
479         header("Wildcard", tree);
480         dumpField("kind", tree.getKind());
481         dump("inner", tree.getBound());
482         return o;
483     }
484     
485     @Override JavaDoc
486     public Object JavaDoc visitModifiers(ModifiersTree tree, Object JavaDoc o) {
487         header("Modifiers", tree);
488         dumpFlags(tree.getFlags());
489     dump("annotations", tree.getAnnotations());
490         return o;
491     }
492
493     @Override JavaDoc
494     public Object JavaDoc visitAnnotation(AnnotationTree tree, Object JavaDoc o) {
495         header("Annotation", tree);
496     dump("annotationType", tree.getAnnotationType());
497     dump("args", tree.getArguments());
498         return o;
499     }
500
501     @Override JavaDoc
502     public Object JavaDoc visitErroneous(ErroneousTree tree, Object JavaDoc o) {
503         header("Erroneous", tree);
504         return o;
505     }
506
507     @Override JavaDoc
508     public Object JavaDoc visitOther(Tree tree, Object JavaDoc o) {
509     assert false;
510         return o;
511     }
512 }
513
Popular Tags