001: /*
002: * @(#)DeclarationScanner.java 1.5 04/04/20
003: *
004: * Copyright (c) 2004, Sun Microsystems, Inc.
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: * * Neither the name of the Sun Microsystems, Inc. nor the names of
016: * its contributors may be used to endorse or promote products derived from
017: * this software without specific prior written permission.
018: *
019: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
020: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
021: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
022: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
023: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
024: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
025: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
026: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
027: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
028: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
029: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */
031:
032: package com.sun.mirror.util;
033:
034: import com.sun.mirror.declaration.*;
035:
036: /**
037: * A visitor for declarations that scans declarations contained within
038: * the given declaration. For example, when visiting a class, the
039: * methods, fields, constructors, and nested types of the class are
040: * also visited.
041: *
042: * <p> To control the processing done on a declaration, users of this
043: * class pass in their own visitors for pre and post processing. The
044: * preprocessing visitor is called before the contained declarations
045: * are scanned; the postprocessing visitor is called after the
046: * contained declarations are scanned.
047: *
048: * @author Joseph D. Darcy
049: * @author Scott Seligman
050: * @version 1.5 04/04/20
051: * @since 1.5
052: */
053:
054: class DeclarationScanner implements DeclarationVisitor {
055: protected DeclarationVisitor pre;
056: protected DeclarationVisitor post;
057:
058: DeclarationScanner(DeclarationVisitor pre, DeclarationVisitor post) {
059: this .pre = pre;
060: this .post = post;
061: }
062:
063: /**
064: * Visits a declaration.
065: *
066: * @param d the declaration to visit
067: */
068: public void visitDeclaration(Declaration d) {
069: d.accept(pre);
070: d.accept(post);
071: }
072:
073: /**
074: * Visits a package declaration.
075: *
076: * @param d the declaration to visit
077: */
078: public void visitPackageDeclaration(PackageDeclaration d) {
079: d.accept(pre);
080:
081: for (ClassDeclaration classDecl : d.getClasses()) {
082: classDecl.accept(this );
083: }
084:
085: for (InterfaceDeclaration interfaceDecl : d.getInterfaces()) {
086: interfaceDecl.accept(this );
087: }
088:
089: d.accept(post);
090: }
091:
092: /**
093: * Visits a member or constructor declaration.
094: *
095: * @param d the declaration to visit
096: */
097: public void visitMemberDeclaration(MemberDeclaration d) {
098: visitDeclaration(d);
099: }
100:
101: /**
102: * Visits a type declaration.
103: *
104: * @param d the declaration to visit
105: */
106: public void visitTypeDeclaration(TypeDeclaration d) {
107: d.accept(pre);
108:
109: for (TypeParameterDeclaration tpDecl : d
110: .getFormalTypeParameters()) {
111: tpDecl.accept(this );
112: }
113:
114: for (FieldDeclaration fieldDecl : d.getFields()) {
115: fieldDecl.accept(this );
116: }
117:
118: for (MethodDeclaration methodDecl : d.getMethods()) {
119: methodDecl.accept(this );
120: }
121:
122: for (TypeDeclaration typeDecl : d.getNestedTypes()) {
123: typeDecl.accept(this );
124: }
125:
126: d.accept(post);
127: }
128:
129: /**
130: * Visits a class declaration.
131: *
132: * @param d the declaration to visit
133: */
134: public void visitClassDeclaration(ClassDeclaration d) {
135: d.accept(pre);
136:
137: for (TypeParameterDeclaration tpDecl : d
138: .getFormalTypeParameters()) {
139: tpDecl.accept(this );
140: }
141:
142: for (FieldDeclaration fieldDecl : d.getFields()) {
143: fieldDecl.accept(this );
144: }
145:
146: for (MethodDeclaration methodDecl : d.getMethods()) {
147: methodDecl.accept(this );
148: }
149:
150: for (TypeDeclaration typeDecl : d.getNestedTypes()) {
151: typeDecl.accept(this );
152: }
153:
154: for (ConstructorDeclaration ctorDecl : d.getConstructors()) {
155: ctorDecl.accept(this );
156: }
157:
158: d.accept(post);
159: }
160:
161: /**
162: * Visits an enum declaration.
163: *
164: * @param d the declaration to visit
165: */
166: public void visitEnumDeclaration(EnumDeclaration d) {
167: visitClassDeclaration(d);
168: }
169:
170: /**
171: * Visits an interface declaration.
172: *
173: * @param d the declaration to visit
174: */
175: public void visitInterfaceDeclaration(InterfaceDeclaration d) {
176: visitTypeDeclaration(d);
177: }
178:
179: /**
180: * Visits an annotation type declaration.
181: *
182: * @param d the declaration to visit
183: */
184: public void visitAnnotationTypeDeclaration(
185: AnnotationTypeDeclaration d) {
186: visitInterfaceDeclaration(d);
187: }
188:
189: /**
190: * Visits a field declaration.
191: *
192: * @param d the declaration to visit
193: */
194: public void visitFieldDeclaration(FieldDeclaration d) {
195: visitMemberDeclaration(d);
196: }
197:
198: /**
199: * Visits an enum constant declaration.
200: *
201: * @param d the declaration to visit
202: */
203: public void visitEnumConstantDeclaration(EnumConstantDeclaration d) {
204: visitFieldDeclaration(d);
205: }
206:
207: /**
208: * Visits a method or constructor declaration.
209: *
210: * @param d the declaration to visit
211: */
212: public void visitExecutableDeclaration(ExecutableDeclaration d) {
213: d.accept(pre);
214:
215: for (TypeParameterDeclaration tpDecl : d
216: .getFormalTypeParameters()) {
217: tpDecl.accept(this );
218: }
219:
220: for (ParameterDeclaration pDecl : d.getParameters()) {
221: pDecl.accept(this );
222: }
223:
224: d.accept(post);
225: }
226:
227: /**
228: * Visits a constructor declaration.
229: *
230: * @param d the declaration to visit
231: */
232: public void visitConstructorDeclaration(ConstructorDeclaration d) {
233: visitExecutableDeclaration(d);
234: }
235:
236: /**
237: * Visits a method declaration.
238: *
239: * @param d the declaration to visit
240: */
241: public void visitMethodDeclaration(MethodDeclaration d) {
242: visitExecutableDeclaration(d);
243: }
244:
245: /**
246: * Visits an annotation type element declaration.
247: *
248: * @param d the declaration to visit
249: */
250: public void visitAnnotationTypeElementDeclaration(
251: AnnotationTypeElementDeclaration d) {
252: visitMethodDeclaration(d);
253: }
254:
255: /**
256: * Visits a parameter declaration.
257: *
258: * @param d the declaration to visit
259: */
260: public void visitParameterDeclaration(ParameterDeclaration d) {
261: visitDeclaration(d);
262: }
263:
264: /**
265: * Visits a type parameter declaration.
266: *
267: * @param d the declaration to visit
268: */
269: public void visitTypeParameterDeclaration(TypeParameterDeclaration d) {
270: visitDeclaration(d);
271: }
272: }
|