Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/checks/usage/transmogrify/DefinitionTraverser.java


1   
2   // Transmogrify License
3   // 
4   // Copyright (c) 2001, ThoughtWorks, Inc.
5   // All rights reserved.
6   // Redistribution and use in source and binary forms, with or without
7   // modification, are permitted provided that the following conditions
8   // are met:
9   // - Redistributions of source code must retain the above copyright notice,
10  //   this list of conditions and the following disclaimer.
11  // - Redistributions in binary form must reproduce the above copyright
12  // notice, this list of conditions and the following disclaimer in the
13  // documentation and/or other materials provided with the distribution.
14  // Neither the name of the ThoughtWorks, Inc. nor the names of its
15  // contributors may be used to endorse or promote products derived from this
16  // software without specific prior written permission.
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  
29  package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
30  
31  
32  
33  import java.util.Enumeration;
34  import java.util.Iterator;
35  
36  public class DefinitionTraverser {
37  
38    protected SymbolTable _symbolTable;
39  
40    public DefinitionTraverser( SymbolTable symbolTable ) {
41      _symbolTable = symbolTable;
42    }
43  
44    public void traverse() {
45      Enumeration packages = _symbolTable.getPackages().elements();
46      while ( packages.hasMoreElements() ) {
47        traverse( (PackageDef)(packages.nextElement()) );
48      }
49    }
50  
51    private void traverse( Definition def ) {
52      if ( def instanceof PackageDef ) {
53        traverse( (PackageDef)def );
54      }
55      else if (def instanceof AnonymousInnerClass) {
56        traverse((AnonymousInnerClass)def);
57      }
58      else if ( def instanceof ClassDef ) {
59        traverse( (ClassDef)def );
60      }
61      else if (def instanceof DefaultConstructor) {
62        traverse((DefaultConstructor)def);
63      }
64      else if ( def instanceof MethodDef ) {
65        traverse( (MethodDef)def );
66      }
67      else if ( def instanceof BlockDef ) {
68        traverse( (BlockDef)def );
69      }
70      else if ( def instanceof VariableDef ) {
71        traverse( (VariableDef)def );
72      }
73      else if ( def instanceof LabelDef ) {
74        traverse( (LabelDef)def );
75      }
76    }
77  
78    private void traverse(PackageDef pkg) {
79      handlePackage(pkg);
80      traversePackage(pkg);
81    }
82  
83    private void traverse(AnonymousInnerClass innerClass) {
84      handleAnonymousInnerClass(innerClass);
85      traverseChildren(innerClass);
86    }
87  
88    private void traverse( ClassDef classDef ) {
89      handleClass( classDef );
90      traverseChildren( classDef );
91    }
92  
93    private void traverse(DefaultConstructor constructor) {
94      handleDefaultConstructor(constructor);
95    }
96  
97    private void traverse( MethodDef method ) {
98      handleMethod( method );
99      traverseChildren( method );
100   }
101 
102   private void traverse( BlockDef block ) {
103     handleBlock( block );
104     traverseChildren( block );
105   }
106 
107   private void traverse( VariableDef variable ) {
108     handleVariable( variable );
109   }
110 
111   private void traverse( LabelDef label ) {
112     handleLabel( label );
113   }
114 
115   private void traversePackage(PackageDef pkg) {
116     Iterator definitions = pkg.getClasses();
117     while (definitions.hasNext()) {
118       ClassDef classDef = (ClassDef)definitions.next();
119       traverse(classDef);
120     }
121   }
122 
123   private void traverseChildren(Scope scope) {
124     Enumeration definitions = scope.getDefinitions();
125     while ( definitions.hasMoreElements() ) {
126       Definition def = (Definition)(definitions.nextElement());
127       traverse(def);
128     }
129   }
130 
131   protected void handlePackage( PackageDef pkg ) {}
132   protected void handleAnonymousInnerClass(AnonymousInnerClass innerClass) {}
133   protected void handleClass( ClassDef classDef ) {}
134   protected void handleDefaultConstructor(DefaultConstructor constructor) {}
135   protected void handleMethod( MethodDef method ) {}
136   protected void handleBlock( BlockDef block ) {}
137   protected void handleVariable( VariableDef variable ) {}
138   protected void handleLabel( LabelDef label ) {}
139 }