KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > java > gui > parser > ElementVisitor


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
20 package org.netbeans.test.java.gui.parser;
21
22 import com.sun.source.tree.CompilationUnitTree;
23 import com.sun.source.tree.Tree;
24 import com.sun.source.util.SourcePositions;
25 import com.sun.source.util.Trees;
26 import java.io.PrintStream JavaDoc;
27 import javax.lang.model.element.Element;
28 import javax.lang.model.element.ExecutableElement;
29 import javax.lang.model.element.PackageElement;
30 import javax.lang.model.element.TypeElement;
31 import javax.lang.model.element.TypeParameterElement;
32 import javax.lang.model.element.VariableElement;
33 import javax.lang.model.util.ElementScanner6;
34 import org.netbeans.api.java.source.CompilationController;
35
36 /**
37  *
38  * @author Jiri Prox
39  */

40 public class ElementVisitor extends ElementScanner6<Void JavaDoc,PrintStream JavaDoc> {
41     
42     private CompilationController cc;
43     
44     private boolean canceled = false;
45     
46     private String JavaDoc text;
47     void cancel() {
48         canceled = true;
49     }
50     
51     public ElementVisitor(CompilationController cc, String JavaDoc text) {
52         this.text = text;
53         this.cc = cc;
54     }
55     
56     @Override JavaDoc
57     public Void JavaDoc visitPackage(PackageElement arg0, PrintStream JavaDoc arg1) {
58         if(!canceled) {
59             arg1.print("Package Element: ");
60             arg1.print(arg0.getSimpleName());
61             long[] pos = getPosition(arg0);
62             arg1.println(" "+pos[0]+" "+pos[1]);
63             if(pos[1]!=-1) arg1.println(text.substring((int)pos[0],(int)pos[1]));
64             return super.visitPackage(arg0, arg1);
65         }
66         return null;
67     }
68     
69     @Override JavaDoc
70     public Void JavaDoc visitType(TypeElement arg0, PrintStream JavaDoc arg1) {
71         if(!canceled) {
72             arg1.print("Type Element: ");
73             arg1.print(arg0.getSimpleName());
74             long[] pos = getPosition(arg0);
75             arg1.println(" "+pos[0]+" "+pos[1]);
76             if(pos[1]!=-1) arg1.println(text.substring((int)pos[0],(int)pos[1]));
77             return super.visitType(arg0, arg1);
78         }
79         return null;
80         
81     }
82     
83     @Override JavaDoc
84     public Void JavaDoc visitVariable(VariableElement arg0, PrintStream JavaDoc arg1) {
85         if(!canceled) {
86             arg1.print("Variable Element: ");
87             arg1.print(arg0.getSimpleName());
88             long[] pos = getPosition(arg0);
89             arg1.println(" "+pos[0]+" "+pos[1]);
90             if(pos[1]!=-1) arg1.println(text.substring((int)pos[0],(int)pos[1]));
91             return super.visitVariable(arg0, arg1);
92         }
93         return null;
94     }
95     
96     @Override JavaDoc
97     public Void JavaDoc visitExecutable(ExecutableElement arg0, PrintStream JavaDoc arg1) {
98         if(!canceled) {
99             arg1.print("Executable Element: ");
100             arg1.print(arg0.getSimpleName());
101             long[] pos = getPosition(arg0);
102             arg1.println(" "+pos[0]+" "+pos[1]);
103             if(pos[1]!=-1) arg1.println(text.substring((int)pos[0],(int)pos[1]));
104             return super.visitExecutable(arg0, arg1);
105         }
106         return null;
107     }
108     
109     @Override JavaDoc
110     public Void JavaDoc visitTypeParameter(TypeParameterElement arg0, PrintStream JavaDoc arg1) {
111         if(!canceled) {
112             arg1.print("TypeParameter Element: ");
113             arg1.print(arg0.getSimpleName());
114             long[] pos = getPosition(arg0);
115             arg1.println(" "+pos[0]+" "+pos[1]);
116             if(pos[1]!=-1) arg1.println(text.substring((int)pos[0],(int)pos[1]));
117             return super.visitTypeParameter(arg0, arg1);
118         }
119         return null;
120     }
121     
122     
123     
124     private long[] getPosition( Element e ) {
125         Trees trees = cc.getTrees();
126         CompilationUnitTree cut = cc.getCompilationUnit();
127         Tree t = trees.getTree(e);
128         if ( t == null ) {
129             return new long[]{-1,-1};
130         }
131         SourcePositions sourcePositions = trees.getSourcePositions();
132         return new long[] {sourcePositions.getStartPosition(cut, t),sourcePositions.getEndPosition(cut, t)};
133     }
134                
135 }
136
Popular Tags