1 | /* |
2 | * Copyright (c) 2001-2009, 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 incrementCount() { |
47 | currentCount++; |
48 | } |
49 | |
50 | protected int currentCount() { |
51 | return currentCount; |
52 | } |
53 | |
54 | public void visitConstantPool(ConstantPool constantPool) { |
55 | resetCount(); |
56 | for (ConstantPoolEntry entry : constantPool) { |
57 | if (entry != null) { |
58 | entry.accept(this); |
59 | } |
60 | incrementCount(); |
61 | } |
62 | } |
63 | |
64 | // Classfile |
65 | public void visitClassfiles(Collection<Classfile> classfiles) { |
66 | for (Classfile classfile : classfiles) { |
67 | classfile.accept(this); |
68 | } |
69 | } |
70 | |
71 | public void visitClassfile(Classfile classfile) { |
72 | visitClassfileFields(classfile); |
73 | visitClassfileMethods(classfile); |
74 | visitClassfileAttributes(classfile); |
75 | } |
76 | |
77 | protected void visitClassfileFields(Classfile classfile) { |
78 | for (Field_info field : classfile.getAllFields()) { |
79 | field.accept(this); |
80 | } |
81 | } |
82 | |
83 | protected void visitClassfileMethods(Classfile classfile) { |
84 | for (Method_info method : classfile.getAllMethods()) { |
85 | method.accept(this); |
86 | } |
87 | } |
88 | |
89 | protected void visitClassfileAttributes(Classfile classfile) { |
90 | visitAttributes(classfile.getAttributes()); |
91 | } |
92 | |
93 | // ConstantPool entries |
94 | |
95 | public void visitClass_info(Class_info entry) { |
96 | // Do nothing |
97 | } |
98 | |
99 | public void visitFieldRef_info(FieldRef_info entry) { |
100 | // Do nothing |
101 | } |
102 | |
103 | public void visitMethodRef_info(MethodRef_info entry) { |
104 | // Do nothing |
105 | } |
106 | |
107 | public void visitInterfaceMethodRef_info(InterfaceMethodRef_info entry) { |
108 | // Do nothing |
109 | } |
110 | |
111 | public void visitString_info(String_info entry) { |
112 | // Do nothing |
113 | } |
114 | |
115 | public void visitInteger_info(Integer_info entry) { |
116 | // Do nothing |
117 | } |
118 | |
119 | public void visitFloat_info(Float_info entry) { |
120 | // Do nothing |
121 | } |
122 | |
123 | public void visitLong_info(Long_info entry) { |
124 | // Do nothing |
125 | } |
126 | |
127 | public void visitDouble_info(Double_info entry) { |
128 | // Do nothing |
129 | } |
130 | |
131 | public void visitNameAndType_info(NameAndType_info entry) { |
132 | // Do nothing |
133 | } |
134 | |
135 | public void visitUTF8_info(UTF8_info entry) { |
136 | // Do nothing |
137 | } |
138 | |
139 | // Features |
140 | |
141 | public void visitField_info(Field_info entry) { |
142 | visitAttributes(entry.getAttributes()); |
143 | } |
144 | |
145 | public void visitMethod_info(Method_info entry) { |
146 | visitAttributes(entry.getAttributes()); |
147 | } |
148 | |
149 | // Attributes |
150 | |
151 | protected void visitAttributes(Collection<? extends Attribute_info> attributes) { |
152 | for (Attribute_info attribute_info : attributes) { |
153 | attribute_info.accept(this); |
154 | } |
155 | } |
156 | |
157 | public void visitConstantValue_attribute(ConstantValue_attribute attribute) { |
158 | // Do nothing |
159 | } |
160 | |
161 | public void visitCode_attribute(Code_attribute attribute) { |
162 | Logger.getLogger(getClass()).debug("Visiting instruction(s) ..."); |
163 | visitInstructions(attribute); |
164 | |
165 | Collection<? extends ExceptionHandler> exceptionHandlers = attribute.getExceptionHandlers(); |
166 | Logger.getLogger(getClass()).debug("Visiting " + exceptionHandlers.size() + " exception handler(s) ..."); |
167 | visitExceptionHandlers(exceptionHandlers); |
168 | |
169 | Collection<? extends Attribute_info> attributes = attribute.getAttributes(); |
170 | Logger.getLogger(getClass()).debug("Visiting " + attributes.size() + " code attribute(s) ..."); |
171 | visitAttributes(attributes); |
172 | } |
173 | |
174 | public void visitExceptions_attribute(Exceptions_attribute attribute) { |
175 | Logger.getLogger(getClass()).debug("Visiting " + attribute.getExceptions().size() + " exception class(es) ..."); |
176 | |
177 | for (Class_info exception : attribute.getExceptions()) { |
178 | exception.accept(this); |
179 | } |
180 | } |
181 | |
182 | public void visitInnerClasses_attribute(InnerClasses_attribute attribute) { |
183 | Logger.getLogger(getClass()).debug("Visiting " + attribute.getInnerClasses().size() + " inner class(es) ..."); |
184 | |
185 | for (InnerClass innerClass : attribute.getInnerClasses()) { |
186 | innerClass.accept(this); |
187 | } |
188 | } |
189 | |
190 | public void visitEnclosingMethod_attribute(EnclosingMethod_attribute attribute) { |
191 | // Do nothing |
192 | } |
193 | |
194 | public void visitSynthetic_attribute(Synthetic_attribute attribute) { |
195 | // Do nothing |
196 | } |
197 | |
198 | public void visitSignature_attribute(Signature_attribute attribute) { |
199 | // Do nothing |
200 | } |
201 | |
202 | public void visitSourceFile_attribute(SourceFile_attribute attribute) { |
203 | // Do nothing |
204 | } |
205 | |
206 | public void visitSourceDebugExtension_attribute(SourceDebugExtension_attribute attribute) { |
207 | // Do nothing |
208 | } |
209 | |
210 | public void visitLineNumberTable_attribute(LineNumberTable_attribute attribute) { |
211 | Logger.getLogger(getClass()).debug("Visiting " + attribute.getLineNumbers().size() + " line number(s) ..."); |
212 | |
213 | for (LineNumber lineNumber : attribute.getLineNumbers()) { |
214 | lineNumber.accept(this); |
215 | } |
216 | } |
217 | |
218 | public void visitLocalVariableTable_attribute(LocalVariableTable_attribute attribute) { |
219 | Logger.getLogger(getClass()).debug("Visiting " + attribute.getLocalVariables().size() + " local variable(s) ..."); |
220 | |
221 | for (LocalVariable localVariable : attribute.getLocalVariables()) { |
222 | localVariable.accept(this); |
223 | } |
224 | } |
225 | |
226 | public void visitLocalVariableTypeTable_attribute(LocalVariableTypeTable_attribute attribute) { |
227 | Logger.getLogger(getClass()).debug("Visiting " + attribute.getLocalVariableTypes().size() + " local variable type(s) ..."); |
228 | |
229 | for (LocalVariableType localVariableType : attribute.getLocalVariableTypes()) { |
230 | localVariableType.accept(this); |
231 | } |
232 | } |
233 | |
234 | public void visitDeprecated_attribute(Deprecated_attribute attribute) { |
235 | // Do nothing |
236 | } |
237 | |
238 | public void visitRuntimeVisibleAnnotations_attribute(RuntimeVisibleAnnotations_attribute attribute) { |
239 | visitRuntimeAnnotations_attribute(attribute); |
240 | } |
241 | |
242 | public void visitRuntimeInvisibleAnnotations_attribute(RuntimeInvisibleAnnotations_attribute attribute) { |
243 | visitRuntimeAnnotations_attribute(attribute); |
244 | } |
245 | |
246 | protected void visitRuntimeAnnotations_attribute(RuntimeAnnotations_attribute attribute) { |
247 | Logger.getLogger(getClass()).debug("Visiting " + attribute.getAnnotations().size() + " annotation(s) ..."); |
248 | |
249 | for (Annotation annotation : attribute.getAnnotations()) { |
250 | annotation.accept(this); |
251 | } |
252 | } |
253 | |
254 | public void visitRuntimeVisibleParameterAnnotations_attribute(RuntimeVisibleParameterAnnotations_attribute attribute) { |
255 | visitRuntimeParameterAnnotations_attribute(attribute); |
256 | } |
257 | |
258 | public void visitRuntimeInvisibleParameterAnnotations_attribute(RuntimeInvisibleParameterAnnotations_attribute attribute) { |
259 | visitRuntimeParameterAnnotations_attribute(attribute); |
260 | } |
261 | |
262 | protected void visitRuntimeParameterAnnotations_attribute(RuntimeParameterAnnotations_attribute attribute) { |
263 | Logger.getLogger(getClass()).debug("Visiting " + attribute.getParameterAnnotations().size() + " parameter annotation(s) ..."); |
264 | |
265 | for (Parameter parameter : attribute.getParameterAnnotations()) { |
266 | parameter.accept(this); |
267 | } |
268 | } |
269 | |
270 | public void visitAnnotationDefault_attribute(AnnotationDefault_attribute attribute) { |
271 | attribute.getElemementValue().accept(this); |
272 | } |
273 | |
274 | public void visitCustom_attribute(Custom_attribute attribute) { |
275 | // Do nothing |
276 | } |
277 | |
278 | // Attribute helpers |
279 | |
280 | protected void visitInstructions(Code_attribute attribute) { |
281 | for (Instruction instruction : attribute) { |
282 | instruction.accept(this); |
283 | } |
284 | } |
285 | |
286 | public void visitInstruction(Instruction helper) { |
287 | // Do nothing |
288 | } |
289 | |
290 | protected void visitExceptionHandlers(Collection<? extends ExceptionHandler> exceptionHandlers) { |
291 | for (ExceptionHandler exceptionHandler : exceptionHandlers) { |
292 | exceptionHandler.accept(this); |
293 | } |
294 | } |
295 | |
296 | public void visitExceptionHandler(ExceptionHandler helper) { |
297 | // Do nothing |
298 | } |
299 | |
300 | public void visitInnerClass(InnerClass helper) { |
301 | // Do nothing |
302 | } |
303 | |
304 | public void visitLineNumber(LineNumber helper) { |
305 | // Do nothing |
306 | } |
307 | |
308 | public void visitLocalVariable(LocalVariable helper) { |
309 | // Do nothing |
310 | } |
311 | |
312 | public void visitLocalVariableType(LocalVariableType helper) { |
313 | // Do nothing |
314 | } |
315 | |
316 | public void visitParameter(Parameter helper) { |
317 | Logger.getLogger(getClass()).debug("Visiting " + helper.getAnnotations().size() + " annotation(s) ..."); |
318 | |
319 | for (Annotation annotation : helper.getAnnotations()) { |
320 | annotation.accept(this); |
321 | } |
322 | } |
323 | |
324 | public void visitAnnotation(Annotation helper) { |
325 | Logger.getLogger(getClass()).debug("Visiting " + helper.getElementValuePairs().size() + " element value pair(s) ..."); |
326 | |
327 | for (ElementValuePair elementValuePair : helper.getElementValuePairs()) { |
328 | elementValuePair.accept(this); |
329 | } |
330 | } |
331 | |
332 | public void visitElementValuePair(ElementValuePair helper) { |
333 | helper.getElementValue().accept(this); |
334 | } |
335 | |
336 | public void visitByteConstantElementValue(ByteConstantElementValue helper) { |
337 | // Do nothing |
338 | } |
339 | |
340 | public void visitCharConstantElementValue(CharConstantElementValue helper) { |
341 | // Do nothing |
342 | } |
343 | |
344 | public void visitDoubleConstantElementValue(DoubleConstantElementValue helper) { |
345 | // Do nothing |
346 | } |
347 | |
348 | public void visitFloatConstantElementValue(FloatConstantElementValue helper) { |
349 | // Do nothing |
350 | } |
351 | |
352 | public void visitIntegerConstantElementValue(IntegerConstantElementValue helper) { |
353 | // Do nothing |
354 | } |
355 | |
356 | public void visitLongConstantElementValue(LongConstantElementValue helper) { |
357 | // Do nothing |
358 | } |
359 | |
360 | public void visitShortConstantElementValue(ShortConstantElementValue helper) { |
361 | // Do nothing |
362 | } |
363 | |
364 | public void visitBooleanConstantElementValue(BooleanConstantElementValue helper) { |
365 | // Do nothing |
366 | } |
367 | |
368 | public void visitStringConstantElementValue(StringConstantElementValue helper) { |
369 | // Do nothing |
370 | } |
371 | |
372 | public void visitEnumElementValue(EnumElementValue helper) { |
373 | // Do nothing |
374 | } |
375 | |
376 | public void visitClassElementValue(ClassElementValue helper) { |
377 | // Do nothing |
378 | } |
379 | |
380 | public void visitAnnotationElementValue(AnnotationElementValue helper) { |
381 | helper.getAnnotation().accept(this); |
382 | } |
383 | |
384 | public void visitArrayElementValue(ArrayElementValue helper) { |
385 | Logger.getLogger(getClass()).debug("Visiting " + helper.getValues().size() + " value(s) ..."); |
386 | |
387 | for (ElementValue elementValue : helper.getValues()) { |
388 | elementValue.accept(this); |
389 | } |
390 | } |
391 | } |