KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mirror > util > DeclarationScanner


1 /*
2  * @(#)DeclarationScanner.java 1.5 04/04/20
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.mirror.util;
9
10 import com.sun.mirror.declaration.*;
11
12 /**
13  * A visitor for declarations that scans declarations contained within
14  * the given declaration. For example, when visiting a class, the
15  * methods, fields, constructors, and nested types of the class are
16  * also visited.
17  *
18  * <p> To control the processing done on a declaration, users of this
19  * class pass in their own visitors for pre and post processing. The
20  * preprocessing visitor is called before the contained declarations
21  * are scanned; the postprocessing visitor is called after the
22  * contained declarations are scanned.
23  *
24  * @author Joseph D. Darcy
25  * @author Scott Seligman
26  * @version 1.5 04/04/20
27  * @since 1.5
28  */

29
30 class DeclarationScanner implements DeclarationVisitor {
31     protected DeclarationVisitor pre;
32     protected DeclarationVisitor post;
33
34     DeclarationScanner(DeclarationVisitor pre, DeclarationVisitor post) {
35     this.pre = pre;
36     this.post = post;
37     }
38
39     /**
40      * Visits a declaration.
41      *
42      * @param d the declaration to visit
43      */

44     public void visitDeclaration(Declaration d) {
45     d.accept(pre);
46     d.accept(post);
47     }
48
49     /**
50      * Visits a package declaration.
51      *
52      * @param d the declaration to visit
53      */

54     public void visitPackageDeclaration(PackageDeclaration d) {
55     d.accept(pre);
56
57     for(ClassDeclaration classDecl: d.getClasses()) {
58         classDecl.accept(this);
59     }
60
61     for(InterfaceDeclaration interfaceDecl: d.getInterfaces()) {
62         interfaceDecl.accept(this);
63     }
64
65     d.accept(post);
66     }
67
68     /**
69      * Visits a member or constructor declaration.
70      *
71      * @param d the declaration to visit
72      */

73     public void visitMemberDeclaration(MemberDeclaration d) {
74     visitDeclaration(d);
75     }
76
77     /**
78      * Visits a type declaration.
79      *
80      * @param d the declaration to visit
81      */

82     public void visitTypeDeclaration(TypeDeclaration d) {
83     d.accept(pre);
84
85     for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
86         tpDecl.accept(this);
87     }
88     
89     for(FieldDeclaration fieldDecl: d.getFields()) {
90         fieldDecl.accept(this);
91     }
92     
93     for(MethodDeclaration methodDecl: d.getMethods()) {
94         methodDecl.accept(this);
95     }
96     
97     for(TypeDeclaration typeDecl: d.getNestedTypes()) {
98         typeDecl.accept(this);
99     }
100
101     d.accept(post);
102     }
103
104     /**
105      * Visits a class declaration.
106      *
107      * @param d the declaration to visit
108      */

109     public void visitClassDeclaration(ClassDeclaration d) {
110     d.accept(pre);
111
112     for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
113         tpDecl.accept(this);
114     }
115     
116     for(FieldDeclaration fieldDecl: d.getFields()) {
117         fieldDecl.accept(this);
118     }
119     
120     for(MethodDeclaration methodDecl: d.getMethods()) {
121         methodDecl.accept(this);
122     }
123     
124     for(TypeDeclaration typeDecl: d.getNestedTypes()) {
125         typeDecl.accept(this);
126     }
127
128     for(ConstructorDeclaration ctorDecl: d.getConstructors()) {
129         ctorDecl.accept(this);
130     }
131
132     d.accept(post);
133     }
134
135     /**
136      * Visits an enum declaration.
137      *
138      * @param d the declaration to visit
139      */

140     public void visitEnumDeclaration(EnumDeclaration d) {
141     visitClassDeclaration(d);
142     }
143
144     /**
145      * Visits an interface declaration.
146      *
147      * @param d the declaration to visit
148      */

149     public void visitInterfaceDeclaration(InterfaceDeclaration d) {
150     visitTypeDeclaration(d);
151     }
152
153     /**
154      * Visits an annotation type declaration.
155      *
156      * @param d the declaration to visit
157      */

158     public void visitAnnotationTypeDeclaration(AnnotationTypeDeclaration d) {
159     visitInterfaceDeclaration(d);
160     }
161
162     /**
163      * Visits a field declaration.
164      *
165      * @param d the declaration to visit
166      */

167     public void visitFieldDeclaration(FieldDeclaration d) {
168     visitMemberDeclaration(d);
169     }
170
171     /**
172      * Visits an enum constant declaration.
173      *
174      * @param d the declaration to visit
175      */

176     public void visitEnumConstantDeclaration(EnumConstantDeclaration d) {
177     visitFieldDeclaration(d);
178     }
179
180     /**
181      * Visits a method or constructor declaration.
182      *
183      * @param d the declaration to visit
184      */

185     public void visitExecutableDeclaration(ExecutableDeclaration d) {
186     d.accept(pre);
187
188     for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
189         tpDecl.accept(this);
190     }
191
192     for(ParameterDeclaration pDecl: d.getParameters()) {
193         pDecl.accept(this);
194     }
195
196     d.accept(post);
197     }
198
199     /**
200      * Visits a constructor declaration.
201      *
202      * @param d the declaration to visit
203      */

204     public void visitConstructorDeclaration(ConstructorDeclaration d) {
205     visitExecutableDeclaration(d);
206     }
207
208     /**
209      * Visits a method declaration.
210      *
211      * @param d the declaration to visit
212      */

213     public void visitMethodDeclaration(MethodDeclaration d) {
214     visitExecutableDeclaration(d);
215     }
216
217     /**
218      * Visits an annotation type element declaration.
219      *
220      * @param d the declaration to visit
221      */

222     public void visitAnnotationTypeElementDeclaration(
223         AnnotationTypeElementDeclaration d) {
224     visitMethodDeclaration(d);
225     }
226
227     /**
228      * Visits a parameter declaration.
229      *
230      * @param d the declaration to visit
231      */

232     public void visitParameterDeclaration(ParameterDeclaration d) {
233     visitDeclaration(d);
234     }
235
236     /**
237      * Visits a type parameter declaration.
238      *
239      * @param d the declaration to visit
240      */

241     public void visitTypeParameterDeclaration(TypeParameterDeclaration d) {
242     visitDeclaration(d);
243     }
244 }
245
Popular Tags