1 /*
   2  * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package com.sun.mirror.util;
  27 
  28 import com.sun.mirror.declaration.*;
  29 
  30 /**
  31  * A visitor for declarations that scans declarations contained within
  32  * the given declaration.  For example, when visiting a class, the
  33  * methods, fields, constructors, and nested types of the class are
  34  * also visited.
  35  *
  36  * <p> To control the processing done on a declaration, users of this
  37  * class pass in their own visitors for pre and post processing.  The
  38  * preprocessing visitor is called before the contained declarations
  39  * are scanned; the postprocessing visitor is called after the
  40  * contained declarations are scanned.
  41  *
  42  * @author Joseph D. Darcy
  43  * @author Scott Seligman
  44  * @since 1.5
  45  */
  46 
  47 class DeclarationScanner implements DeclarationVisitor {
  48     protected DeclarationVisitor pre;
  49     protected DeclarationVisitor post;
  50 
  51     DeclarationScanner(DeclarationVisitor pre, DeclarationVisitor post) {
  52         this.pre = pre;
  53         this.post = post;
  54     }
  55 
  56     /**
  57      * Visits a declaration.
  58      *
  59      * @param d the declaration to visit
  60      */
  61     public void visitDeclaration(Declaration d) {
  62         d.accept(pre);
  63         d.accept(post);
  64     }
  65 
  66     /**
  67      * Visits a package declaration.
  68      *
  69      * @param d the declaration to visit
  70      */
  71     public void visitPackageDeclaration(PackageDeclaration d) {
  72         d.accept(pre);
  73 
  74         for(ClassDeclaration classDecl: d.getClasses()) {
  75             classDecl.accept(this);
  76         }
  77 
  78         for(InterfaceDeclaration interfaceDecl: d.getInterfaces()) {
  79             interfaceDecl.accept(this);
  80         }
  81 
  82         d.accept(post);
  83     }
  84 
  85     /**
  86      * Visits a member or constructor declaration.
  87      *
  88      * @param d the declaration to visit
  89      */
  90     public void visitMemberDeclaration(MemberDeclaration d) {
  91         visitDeclaration(d);
  92     }
  93 
  94     /**
  95      * Visits a type declaration.
  96      *
  97      * @param d the declaration to visit
  98      */
  99     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 }