KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > bcel > VisitorSet


1 //Tested with BCEL-5.1
2
//http://jakarta.apache.org/builds/jakarta-bcel/release/v5.1/
3

4 package com.puppycrawl.tools.checkstyle.bcel;
5
6 import java.util.HashSet JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Set JavaDoc;
9
10 import org.apache.bcel.classfile.Code;
11 import org.apache.bcel.classfile.ConstantPool;
12 import org.apache.bcel.classfile.EmptyVisitor;
13 import org.apache.bcel.classfile.Field;
14 import org.apache.bcel.classfile.JavaClass;
15 import org.apache.bcel.classfile.LocalVariable;
16 import org.apache.bcel.classfile.LocalVariableTable;
17 import org.apache.bcel.classfile.Method;
18 import org.apache.bcel.classfile.Visitor;
19 import org.apache.bcel.generic.InstructionHandle;
20 import org.apache.bcel.generic.InstructionList;
21
22 /**
23  * Manages a set of visitors that are accepted by nodes visited by
24  * a VisitorSet. Any visit to this object is passed on to its managed
25  * visitors.
26  * @author Rick Giles
27  */

28 // TODO: review visitXXX
29
public class VisitorSet
30     extends EmptyVisitor
31 {
32     /** the managed visitors */
33     private Set JavaDoc mVisitors = new HashSet JavaDoc();
34
35     /** Creates a <code>VisitorSet</code> for a set of visitors.
36      * @param aVisitors the set of managed visitors.
37      */

38     public VisitorSet(Set JavaDoc aVisitors)
39     {
40         mVisitors = aVisitors;
41     }
42
43    /**
44      * @see org.apache.bcel.classfile.Visitor#visitCode
45      */

46     public void visitCode(Code aCode)
47     {
48         for (Iterator JavaDoc iter = mVisitors.iterator(); iter.hasNext();) {
49             IDeepVisitor visitor = (IDeepVisitor) iter.next();
50             Visitor v = visitor.getClassFileVisitor();
51             aCode.accept(v);
52         }
53         
54         // perform a deep visit
55
final byte[] code = aCode.getCode();
56         final InstructionList list = new InstructionList(code);
57         final Iterator JavaDoc it = list.iterator();
58         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
59             InstructionHandle instruction = (InstructionHandle) iter.next();
60             visitInstructionHandle(instruction);
61         }
62     }
63
64     /**
65      * Deep visit of an InstructionHandle
66      * @param aInstruction the InstructionHandle
67      */

68     private void visitInstructionHandle(InstructionHandle aInstruction)
69     {
70         for (Iterator JavaDoc iter = mVisitors.iterator(); iter.hasNext();) {
71             final IDeepVisitor visitor = (IDeepVisitor) iter.next();
72             org.apache.bcel.generic.Visitor v =
73                 visitor.getGenericVisitor();
74             aInstruction.accept(v);
75         }
76     }
77
78     /**
79      * @see org.apache.bcel.classfile.Visitor
80      */

81     public void visitConstantPool(ConstantPool aConstantPool)
82     {
83         for (Iterator JavaDoc iter = mVisitors.iterator(); iter.hasNext();) {
84             IDeepVisitor visitor = (IDeepVisitor) iter.next();
85             Visitor v = visitor.getClassFileVisitor();
86             aConstantPool.accept(v);
87         }
88     }
89
90     /**
91      * @see org.apache.bcel.classfile.Visitor
92      */

93     public void visitField(Field aField)
94     {
95         for (Iterator JavaDoc iter = mVisitors.iterator(); iter.hasNext();) {
96             IDeepVisitor visitor = (IDeepVisitor) iter.next();
97             Visitor v = visitor.getClassFileVisitor();
98             aField.accept(v);
99         }
100     }
101
102     /**
103      * @see org.apache.bcel.classfile.Visitor
104      */

105     public void visitJavaClass(JavaClass aJavaClass)
106     {
107         for (Iterator JavaDoc iter = mVisitors.iterator(); iter.hasNext();) {
108             IDeepVisitor visitor = (IDeepVisitor) iter.next();
109             Visitor v = visitor.getClassFileVisitor();
110             aJavaClass.accept(v);
111         }
112     }
113
114 // /**
115
// * @see org.apache.bcel.classfile.Visitor
116
// */
117
// public void visitLocalVariable(LocalVariable aLocalVariable)
118
// {
119
// for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
120
// IDeepVisitor visitor = (IDeepVisitor) iter.next();
121
// Visitor v = visitor.getClassFileVisitor();
122
// aLocalVariable.accept(v);
123
// }
124
// }
125
//
126
/**
127      * @see org.apache.bcel.classfile.Visitor
128      */

129     public void visitLocalVariableTable(LocalVariableTable aTable)
130     {
131         for (Iterator JavaDoc iter = mVisitors.iterator(); iter.hasNext();) {
132             IDeepVisitor visitor = (IDeepVisitor) iter.next();
133             Visitor v = visitor.getClassFileVisitor();
134             aTable.accept(v);
135         }
136     }
137
138     /**
139      * @see org.apache.bcel.classfile.Visitor
140      */

141     public void visitMethod(Method aMethod)
142     {
143         for (Iterator JavaDoc iter = mVisitors.iterator(); iter.hasNext();) {
144             IDeepVisitor visitor = (IDeepVisitor) iter.next();
145             Visitor v = visitor.getClassFileVisitor();
146             aMethod.accept(v);
147         }
148     }
149 }
150
Popular Tags