001    /*
002     * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.mirror.util;
027    
028    import com.sun.mirror.declaration.*;
029    
030    /**
031     * A visitor for declarations that scans declarations contained within
032     * the given declaration.  For example, when visiting a class, the
033     * methods, fields, constructors, and nested types of the class are
034     * also visited.
035     *
036     * <p> To control the processing done on a declaration, users of this
037     * class pass in their own visitors for pre and post processing.  The
038     * preprocessing visitor is called before the contained declarations
039     * are scanned; the postprocessing visitor is called after the
040     * contained declarations are scanned.
041     *
042     * @author Joseph D. Darcy
043     * @author Scott Seligman
044     * @since 1.5
045     */
046    
047    class DeclarationScanner implements DeclarationVisitor {
048        protected DeclarationVisitor pre;
049        protected DeclarationVisitor post;
050    
051        DeclarationScanner(DeclarationVisitor pre, DeclarationVisitor post) {
052            this.pre = pre;
053            this.post = post;
054        }
055    
056        /**
057         * Visits a declaration.
058         *
059         * @param d the declaration to visit
060         */
061        public void visitDeclaration(Declaration d) {
062            d.accept(pre);
063            d.accept(post);
064        }
065    
066        /**
067         * Visits a package declaration.
068         *
069         * @param d the declaration to visit
070         */
071        public void visitPackageDeclaration(PackageDeclaration d) {
072            d.accept(pre);
073    
074            for(ClassDeclaration classDecl: d.getClasses()) {
075                classDecl.accept(this);
076            }
077    
078            for(InterfaceDeclaration interfaceDecl: d.getInterfaces()) {
079                interfaceDecl.accept(this);
080            }
081    
082            d.accept(post);
083        }
084    
085        /**
086         * Visits a member or constructor declaration.
087         *
088         * @param d the declaration to visit
089         */
090        public void visitMemberDeclaration(MemberDeclaration d) {
091            visitDeclaration(d);
092        }
093    
094        /**
095         * Visits a type declaration.
096         *
097         * @param d the declaration to visit
098         */
099        public void visitTypeDeclaration(TypeDeclaration d) {
100            d.accept(pre);
101    
102            for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
103                tpDecl.accept(this);
104            }
105    
106            for(FieldDeclaration fieldDecl: d.getFields()) {
107                fieldDecl.accept(this);
108            }
109    
110            for(MethodDeclaration methodDecl: d.getMethods()) {
111                methodDecl.accept(this);
112            }
113    
114            for(TypeDeclaration typeDecl: d.getNestedTypes()) {
115                typeDecl.accept(this);
116            }
117    
118            d.accept(post);
119        }
120    
121        /**
122         * Visits a class declaration.
123         *
124         * @param d the declaration to visit
125         */
126        public void visitClassDeclaration(ClassDeclaration d) {
127            d.accept(pre);
128    
129            for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
130                tpDecl.accept(this);
131            }
132    
133            for(FieldDeclaration fieldDecl: d.getFields()) {
134                fieldDecl.accept(this);
135            }
136    
137            for(MethodDeclaration methodDecl: d.getMethods()) {
138                methodDecl.accept(this);
139            }
140    
141            for(TypeDeclaration typeDecl: d.getNestedTypes()) {
142                typeDecl.accept(this);
143            }
144    
145            for(ConstructorDeclaration ctorDecl: d.getConstructors()) {
146                ctorDecl.accept(this);
147            }
148    
149            d.accept(post);
150        }
151    
152        /**
153         * Visits an enum declaration.
154         *
155         * @param d the declaration to visit
156         */
157        public void visitEnumDeclaration(EnumDeclaration d) {
158            visitClassDeclaration(d);
159        }
160    
161        /**
162         * Visits an interface declaration.
163         *
164         * @param d the declaration to visit
165         */
166        public void visitInterfaceDeclaration(InterfaceDeclaration d) {
167            visitTypeDeclaration(d);
168        }
169    
170        /**
171         * Visits an annotation type declaration.
172         *
173         * @param d the declaration to visit
174         */
175        public void visitAnnotationTypeDeclaration(AnnotationTypeDeclaration d) {
176            visitInterfaceDeclaration(d);
177        }
178    
179        /**
180         * Visits a field declaration.
181         *
182         * @param d the declaration to visit
183         */
184        public void visitFieldDeclaration(FieldDeclaration d) {
185            visitMemberDeclaration(d);
186        }
187    
188        /**
189         * Visits an enum constant declaration.
190         *
191         * @param d the declaration to visit
192         */
193        public void visitEnumConstantDeclaration(EnumConstantDeclaration d) {
194            visitFieldDeclaration(d);
195        }
196    
197        /**
198         * Visits a method or constructor declaration.
199         *
200         * @param d the declaration to visit
201         */
202        public void visitExecutableDeclaration(ExecutableDeclaration d) {
203            d.accept(pre);
204    
205            for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
206                tpDecl.accept(this);
207            }
208    
209            for(ParameterDeclaration pDecl: d.getParameters()) {
210                pDecl.accept(this);
211            }
212    
213            d.accept(post);
214        }
215    
216        /**
217         * Visits a constructor declaration.
218         *
219         * @param d the declaration to visit
220         */
221        public void visitConstructorDeclaration(ConstructorDeclaration d) {
222            visitExecutableDeclaration(d);
223        }
224    
225        /**
226         * Visits a method declaration.
227         *
228         * @param d the declaration to visit
229         */
230        public void visitMethodDeclaration(MethodDeclaration d) {
231            visitExecutableDeclaration(d);
232        }
233    
234        /**
235         * Visits an annotation type element declaration.
236         *
237         * @param d the declaration to visit
238         */
239        public void visitAnnotationTypeElementDeclaration(
240                AnnotationTypeElementDeclaration d) {
241            visitMethodDeclaration(d);
242        }
243    
244        /**
245         * Visits a parameter declaration.
246         *
247         * @param d the declaration to visit
248         */
249        public void visitParameterDeclaration(ParameterDeclaration d) {
250            visitDeclaration(d);
251        }
252    
253        /**
254         * Visits a type parameter declaration.
255         *
256         * @param d the declaration to visit
257         */
258        public void visitTypeParameterDeclaration(TypeParameterDeclaration d) {
259            visitDeclaration(d);
260        }
261    }