KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > VisitorBase


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.classreader;
34
35 import java.util.*;
36
37 import org.apache.log4j.*;
38
39 public abstract class VisitorBase implements Visitor {
40     private int currentCount;
41
42     protected void resetCount() {
43         currentCount = 0;
44     }
45
46     protected void raiseCount() {
47         currentCount++;
48     }
49
50     protected int currentCount() {
51         return currentCount;
52     }
53
54     public void visitConstantPool(ConstantPool constantPool) {
55         Iterator i = constantPool.iterator();
56         while (i.hasNext()) {
57             Visitable entry = (Visitable) i.next();
58             if (entry != null) {
59                 entry.accept(this);
60             }
61             raiseCount();
62         }
63     }
64
65     // Classfile
66
public void visitClassfiles(Collection classfiles) {
67         Iterator i = classfiles.iterator();
68         while (i.hasNext()) {
69             ((Classfile) i.next()).accept(this);
70         }
71     }
72
73     public void visitClassfile(Classfile classfile) {
74         Iterator i;
75
76         i = classfile.getAttributes().iterator();
77         while (i.hasNext()) {
78             ((Visitable) i.next()).accept(this);
79         }
80
81         i = classfile.getAllFields().iterator();
82         while (i.hasNext()) {
83             ((Visitable) i.next()).accept(this);
84         }
85
86         i = classfile.getAllMethods().iterator();
87         while (i.hasNext()) {
88             ((Visitable) i.next()).accept(this);
89         }
90     }
91
92     // ConstantPool entries
93
public void visitClass_info(Class_info entry) {}
94     public void visitFieldRef_info(FieldRef_info entry) {}
95     public void visitMethodRef_info(MethodRef_info entry) {}
96     public void visitInterfaceMethodRef_info(InterfaceMethodRef_info entry) {}
97     public void visitString_info(String_info entry) {}
98     public void visitInteger_info(Integer_info entry) {}
99     public void visitFloat_info(Float_info entry) {}
100     public void visitLong_info(Long_info entry) {}
101     public void visitDouble_info(Double_info entry) {}
102     public void visitNameAndType_info(NameAndType_info entry) {}
103     public void visitUTF8_info(UTF8_info entry) {}
104
105     // Features
106
public void visitField_info(Field_info entry) {
107         Iterator i = entry.getAttributes().iterator();
108         while (i.hasNext()) {
109             ((Visitable) i.next()).accept(this);
110         }
111     }
112
113     public void visitMethod_info(Method_info entry) {
114         Iterator i = entry.getAttributes().iterator();
115         while (i.hasNext()) {
116             ((Visitable) i.next()).accept(this);
117         }
118     }
119
120     // Attributes
121
public void visitConstantValue_attribute(ConstantValue_attribute attribute) {
122         // Do nothing
123
}
124
125     public void visitCode_attribute(Code_attribute attribute) {
126         Iterator i;
127
128         Logger.getLogger(getClass()).debug("Visiting " + attribute.getExceptionHandlers().size() + " exception handler(s) ...");
129         i = attribute.getExceptionHandlers().iterator();
130         while (i.hasNext()) {
131             ((Visitable) i.next()).accept(this);
132         }
133         
134         Logger.getLogger(getClass()).debug("Visiting " + attribute.getAttributes().size() + " code attribute(s) ...");
135         i = attribute.getAttributes().iterator();
136         while (i.hasNext()) {
137             ((Visitable) i.next()).accept(this);
138         }
139     }
140
141     public void visitExceptions_attribute(Exceptions_attribute attribute) {
142         Logger.getLogger(getClass()).debug("Visiting " + attribute.getExceptions().size() + " exception class(es) ...");
143
144         Iterator i = attribute.getExceptions().iterator();
145         while (i.hasNext()) {
146             ((Visitable) i.next()).accept(this);
147         }
148     }
149     
150     public void visitInnerClasses_attribute(InnerClasses_attribute attribute) {
151         Logger.getLogger(getClass()).debug("Visiting " + attribute.getClasses().size() + " inner class(es) ...");
152
153         Iterator i = attribute.getClasses().iterator();
154         while (i.hasNext()) {
155             ((Visitable) i.next()).accept(this);
156         }
157     }
158     
159     public void visitSynthetic_attribute(Synthetic_attribute attribute) {
160         // Do nothing
161
}
162     
163     public void visitSourceFile_attribute(SourceFile_attribute attribute) {
164         // Do nothing
165
}
166     
167     public void visitLineNumberTable_attribute(LineNumberTable_attribute attribute) {
168         Logger.getLogger(getClass()).debug("Visiting " + attribute.getLineNumbers().size() + " line number(s) ...");
169
170         Iterator i = attribute.getLineNumbers().iterator();
171         while (i.hasNext()) {
172             ((Visitable) i.next()).accept(this);
173         }
174     }
175     
176     public void visitLocalVariableTable_attribute(LocalVariableTable_attribute attribute) {
177         Logger.getLogger(getClass()).debug("Visiting " + attribute.getLocalVariables().size() + " local variable(s) ...");
178
179         Iterator i = attribute.getLocalVariables().iterator();
180         while (i.hasNext()) {
181             ((Visitable) i.next()).accept(this);
182         }
183     }
184     
185     public void visitDeprecated_attribute(Deprecated_attribute attribute) {
186         // Do nothing
187
}
188     
189     public void visitCustom_attribute(Custom_attribute attribute) {
190         // Do nothing
191
}
192
193     // Attribute helpers
194
public void visitExceptionHandler(ExceptionHandler helper) {}
195     public void visitInnerClass(InnerClass helper) {}
196     public void visitLineNumber(LineNumber helper) {}
197     public void visitLocalVariable(LocalVariable helper) {}
198 }
199
Popular Tags