KickJava   Java API By Example, From Geeks To Geeks.

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


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.source.builder;
20
21 import com.sun.tools.javac.tree.JCTree;
22 import java.io.PrintWriter JavaDoc;
23
24
25 import com.sun.tools.javac.tree.JCTree.*;
26 import com.sun.tools.javac.tree.TreeInfo;
27 import com.sun.tools.javac.tree.TreeScanner;
28
29 /**
30  * Diagnostic class to dump a javac Parser AST and its underlying Scanner runs.
31  */

32 public class DumpScanner extends TreeScanner {
33
34     BufferRunQueue runs;
35     JCCompilationUnit toplevel;
36
37     PrintWriter JavaDoc out;
38
39     int level = -1;
40     static final String JavaDoc BLANK = " ";
41
42     public static void dump(BufferRunQueue runs, JCCompilationUnit toplevel, PrintWriter JavaDoc out) {
43         new DumpScanner(runs, toplevel, out).scan(toplevel);
44     }
45
46     DumpScanner(BufferRunQueue runs, JCCompilationUnit toplevel, PrintWriter JavaDoc out) {
47         this.runs = runs;
48         this.toplevel = toplevel;
49         this.out = out;
50     }
51
52     //!CQ gjc 2.x scan
53
public void scan(JCTree tree) {
54         level++;
55         super.scan(tree);
56         level--;
57     }
58
59     private static void appendTypeTag(StringBuffer JavaDoc sb, int typetag) {
60         if (typetag > 0 && typetag < TYPE_NAMES.length)
61             sb.append(TYPE_NAMES[typetag]);
62         else
63             sb.append("<BadTypeTag:" + typetag + ">");
64     }
65
66     /** array of type name strings that can be indexed by typetag */
67     private static final String JavaDoc[] TYPE_NAMES = {
68         null,
69         "byte", "char", "short", "int", "long", "float", "double", "boolean", "void",
70         "<class>", "<array>", "<method>", "<package>",
71     };
72
73     public void dumpHead(JCTree t) {
74         if (t != null) {
75             for (;;) {
76                 BufferRun br = runs.getNextBefore(t.pos);
77                 if (br instanceof CommentRun) {
78                     int pos = br.getStart();
79                     int endPos = br.getEnd();
80                     out.print(BLANK.substring(0, level * 4));
81                     out.println("Comment @ " + pos + "-" + endPos + ": " + br.toString());
82                 }
83                 else
84                     break;
85             }
86             out.print(BLANK.substring(0, level * 4));
87
88             String JavaDoc cls = t.getClass().getName();
89             int dollar = cls.indexOf('$');
90             cls = cls.substring(dollar + 1);
91             out.print(cls);
92             if (false)
93                 out.print(":" + Integer.toHexString(t.hashCode()));
94             out.print(" @ " + t.pos);
95
96             if (toplevel.endPositions != null) {
97                 Integer JavaDoc end = (Integer JavaDoc)toplevel.endPositions.get(t);
98                 if (end != null) {
99                     int ep = end.intValue();
100                     out.print("-" + ep);
101                 }
102                 else {
103                     int ep = TreeInfo.endPos(t);
104                     out.print("-(" + ep + ")");
105                 }
106             }
107
108             if (false)
109                 out.print(" type:" + (t.type != null
110                               ? (t.type.tsym != null
111                      ? t.type.tsym.toString()
112                      : t.type.toString())
113                   : "<null>"));
114             out.print(" ");
115         }
116     }
117
118     @Override JavaDoc
119     public void visitTopLevel(JCCompilationUnit tree) {
120         dumpHead(tree);
121         if (tree.sourcefile != null)
122             out.print(" file:" + tree.sourcefile.toString());
123         if (tree.packge != null)
124             out.print(" pkg:" + tree.packge.toString());
125         out.println();
126         super.visitTopLevel(tree);
127     }
128
129     @Override JavaDoc
130     public void visitImport(JCImport tree) {
131         dumpHead(tree);
132         out.println();
133         super.visitImport(tree);
134     }
135
136     @Override JavaDoc
137     public void visitClassDef(JCClassDecl tree) {
138         dumpHead(tree);
139         out.println("\"" + tree.name + "\"");
140         super.visitClassDef(tree);
141     }
142
143     @Override JavaDoc
144     public void visitMethodDef(JCMethodDecl tree) {
145         dumpHead(tree);
146         out.println("\"" + tree.name + "\"");
147         super.visitMethodDef(tree);
148     }
149
150     @Override JavaDoc
151     public void visitVarDef(JCVariableDecl tree) {
152         dumpHead(tree);
153         out.println("\"" + tree.name + "\"");
154         super.visitVarDef(tree);
155     }
156
157     @Override JavaDoc
158     public void visitSkip(JCSkip tree) {
159         dumpHead(tree);
160         out.println();
161         super.visitSkip(tree);
162     }
163
164     @Override JavaDoc
165     public void visitBlock(JCBlock tree) {
166         dumpHead(tree);
167         out.println("flags:" + tree.flags + " endPos:" + tree.endpos);
168         super.visitBlock(tree);
169     }
170
171     @Override JavaDoc
172     public void visitDoLoop(JCDoWhileLoop tree) {
173         dumpHead(tree);
174         out.println();
175         super.visitDoLoop(tree);
176     }
177
178     @Override JavaDoc
179     public void visitWhileLoop(JCWhileLoop tree) {
180         dumpHead(tree);
181         out.println();
182         super.visitWhileLoop(tree);
183     }
184
185     @Override JavaDoc
186     public void visitForLoop(JCForLoop tree) {
187         dumpHead(tree);
188         out.println();
189         super.visitForLoop(tree);
190     }
191
192     @Override JavaDoc
193     public void visitForeachLoop(JCEnhancedForLoop tree) {
194         dumpHead(tree);
195         out.println();
196         super.visitForeachLoop(tree);
197     }
198
199     @Override JavaDoc
200     public void visitLabelled(JCLabeledStatement tree) {
201         dumpHead(tree);
202         out.println();
203         super.visitLabelled(tree);
204     }
205
206     @Override JavaDoc
207     public void visitSwitch(JCSwitch tree) {
208         dumpHead(tree);
209         out.println();
210         super.visitSwitch(tree);
211     }
212
213     @Override JavaDoc
214     public void visitCase(JCCase tree) {
215         dumpHead(tree);
216         out.println();
217         super.visitCase(tree);
218     }
219
220     @Override JavaDoc
221     public void visitSynchronized(JCSynchronized tree) {
222         dumpHead(tree);
223         out.println();
224         super.visitSynchronized(tree);
225     }
226
227     @Override JavaDoc
228     public void visitTry(JCTry tree) {
229         dumpHead(tree);
230         out.println();
231         super.visitTry(tree);
232     }
233
234     @Override JavaDoc
235     public void visitCatch(JCCatch tree) {
236         dumpHead(tree);
237         out.println();
238         super.visitCatch(tree);
239     }
240
241     @Override JavaDoc
242     public void visitConditional(JCConditional tree) {
243         dumpHead(tree);
244         out.println();
245         super.visitConditional(tree);
246     }
247
248     @Override JavaDoc
249     public void visitIf(JCIf tree) {
250         dumpHead(tree);
251         out.println();
252         super.visitIf(tree);
253     }
254
255     @Override JavaDoc
256     public void visitExec(JCExpressionStatement tree) {
257         dumpHead(tree);
258         out.println();
259         super.visitExec(tree);
260     }
261
262     @Override JavaDoc
263     public void visitBreak(JCBreak tree) {
264         dumpHead(tree);
265         out.println();
266         super.visitBreak(tree);
267     }
268
269     @Override JavaDoc
270     public void visitContinue(JCContinue tree) {
271         dumpHead(tree);
272         out.println();
273         super.visitContinue(tree);
274     }
275
276     @Override JavaDoc
277     public void visitReturn(JCReturn tree) {
278         dumpHead(tree);
279         out.println();
280         super.visitReturn(tree);
281     }
282
283     @Override JavaDoc
284     public void visitThrow(JCThrow tree) {
285         dumpHead(tree);
286         out.println();
287         super.visitThrow(tree);
288     }
289
290     @Override JavaDoc
291     public void visitAssert(JCAssert tree) {
292         dumpHead(tree);
293         out.println();
294         super.visitAssert(tree);
295     }
296
297     @Override JavaDoc
298     public void visitApply(JCMethodInvocation tree) {
299         dumpHead(tree);
300         out.println();
301         super.visitApply(tree);
302     }
303
304     @Override JavaDoc
305     public void visitNewClass(JCNewClass tree) {
306         dumpHead(tree);
307         out.println();
308         super.visitNewClass(tree);
309     }
310
311     @Override JavaDoc
312     public void visitNewArray(JCNewArray tree) {
313         dumpHead(tree);
314         out.println();
315         super.visitNewArray(tree);
316     }
317
318     @Override JavaDoc
319     public void visitParens(JCParens tree) {
320         dumpHead(tree);
321         out.println();
322         super.visitParens(tree);
323     }
324
325     @Override JavaDoc
326     public void visitAssign(JCAssign tree) {
327         dumpHead(tree);
328         out.println();
329         super.visitAssign(tree);
330     }
331
332     @Override JavaDoc
333     public void visitAssignop(JCAssignOp tree) {
334         dumpHead(tree);
335         out.println(tree.operator);
336         super.visitAssignop(tree);
337     }
338
339     @Override JavaDoc
340     public void visitUnary(JCUnary tree) {
341         dumpHead(tree);
342         out.println(tree.operator);
343         super.visitUnary(tree);
344     }
345
346     @Override JavaDoc
347     public void visitBinary(JCBinary tree) {
348         dumpHead(tree);
349         out.println(tree.operator);
350         super.visitBinary(tree);
351     }
352
353     @Override JavaDoc
354     public void visitTypeCast(JCTypeCast tree) {
355         dumpHead(tree);
356         out.println();
357         super.visitTypeCast(tree);
358     }
359
360     @Override JavaDoc
361     public void visitTypeTest(JCInstanceOf tree) {
362         dumpHead(tree);
363         out.println();
364         super.visitTypeTest(tree);
365     }
366
367     @Override JavaDoc
368     public void visitIndexed(JCArrayAccess tree) {
369         dumpHead(tree);
370         out.println();
371         super.visitIndexed(tree);
372     }
373
374     @Override JavaDoc
375     public void visitSelect(JCFieldAccess tree) {
376         dumpHead(tree);
377         out.println("\"" + tree.name + "\"");
378         super.visitSelect(tree);
379     }
380
381     @Override JavaDoc
382     public void visitIdent(JCIdent tree) {
383         dumpHead(tree);
384         out.println("\"" + tree.name + "\"");
385         super.visitIdent(tree);
386     }
387
388     @Override JavaDoc
389     public void visitLiteral(JCLiteral tree) {
390         dumpHead(tree);
391         out.println(tree.value);
392         super.visitLiteral(tree);
393     }
394
395     @Override JavaDoc
396     public void visitTypeIdent(JCPrimitiveTypeTree tree) {
397         dumpHead(tree);
398         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
399         appendTypeTag(sb, tree.typetag);
400         out.println(sb + " (" + tree.typetag + ")");
401         super.visitTypeIdent(tree);
402     }
403     
404     @Override JavaDoc
405     public void visitTypeArray(JCArrayTypeTree tree) {
406         dumpHead(tree);
407         out.println();
408         super.visitTypeArray(tree);
409     }
410
411     @Override JavaDoc
412     public void visitTypeApply(JCTypeApply tree) {
413         dumpHead(tree);
414         out.println();
415         super.visitTypeApply(tree);
416     }
417
418     @Override JavaDoc
419     public void visitTypeParameter(JCTypeParameter tree) {
420         dumpHead(tree);
421         out.println();
422         super.visitTypeParameter(tree);
423     }
424
425     @Override JavaDoc
426     public void visitWildcard(JCWildcard tree) {
427         dumpHead(tree);
428         out.println();
429         super.visitWildcard(tree);
430     }
431     
432     @Override JavaDoc
433     public void visitAnnotation(JCAnnotation tree) {
434         dumpHead(tree);
435         out.println();
436         super.visitAnnotation(tree);
437     }
438     
439     @Override JavaDoc
440     public void visitModifiers(JCModifiers tree) {
441         dumpHead(tree);
442         out.println();
443         super.visitModifiers(tree);
444     }
445     
446     @Override JavaDoc
447     public void visitErroneous(JCErroneous tree) {
448         dumpHead(tree);
449         out.println();
450         super.visitErroneous(tree);
451     }
452 }
453
Popular Tags