001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import java.util.*;
036:
037: import org.apache.log4j.*;
038:
039: public abstract class VisitorBase implements Visitor {
040: private int currentCount;
041:
042: protected void resetCount() {
043: currentCount = 0;
044: }
045:
046: protected void incrementCount() {
047: currentCount++;
048: }
049:
050: protected int currentCount() {
051: return currentCount;
052: }
053:
054: public void visitConstantPool(ConstantPool constantPool) {
055: for (ConstantPoolEntry entry : constantPool) {
056: if (entry != null) {
057: entry.accept(this );
058: }
059: incrementCount();
060: }
061: }
062:
063: // Classfile
064: public void visitClassfiles(Collection<Classfile> classfiles) {
065: for (Classfile classfile : classfiles) {
066: classfile.accept(this );
067: }
068: }
069:
070: public void visitClassfile(Classfile classfile) {
071: for (Attribute_info attribute : classfile.getAttributes()) {
072: attribute.accept(this );
073: }
074:
075: for (Field_info field : classfile.getAllFields()) {
076: field.accept(this );
077: }
078:
079: for (Method_info method : classfile.getAllMethods()) {
080: method.accept(this );
081: }
082: }
083:
084: // ConstantPool entries
085: public void visitClass_info(Class_info entry) {
086: }
087:
088: public void visitFieldRef_info(FieldRef_info entry) {
089: }
090:
091: public void visitMethodRef_info(MethodRef_info entry) {
092: }
093:
094: public void visitInterfaceMethodRef_info(
095: InterfaceMethodRef_info entry) {
096: }
097:
098: public void visitString_info(String_info entry) {
099: }
100:
101: public void visitInteger_info(Integer_info entry) {
102: }
103:
104: public void visitFloat_info(Float_info entry) {
105: }
106:
107: public void visitLong_info(Long_info entry) {
108: }
109:
110: public void visitDouble_info(Double_info entry) {
111: }
112:
113: public void visitNameAndType_info(NameAndType_info entry) {
114: }
115:
116: public void visitUTF8_info(UTF8_info entry) {
117: }
118:
119: // Features
120: public void visitField_info(Field_info entry) {
121: for (Attribute_info attribute : entry.getAttributes()) {
122: attribute.accept(this );
123: }
124: }
125:
126: public void visitMethod_info(Method_info entry) {
127: for (Attribute_info attribute : entry.getAttributes()) {
128: attribute.accept(this );
129: }
130: }
131:
132: // Attributes
133: public void visitConstantValue_attribute(
134: ConstantValue_attribute attribute) {
135: // Do nothing
136: }
137:
138: public void visitCode_attribute(Code_attribute attribute) {
139: Logger.getLogger(getClass()).debug(
140: "Visiting instruction(s) ...");
141: Iterator i = attribute.iterator();
142: while (i.hasNext()) {
143: ((Instruction) i.next()).accept(this );
144: }
145:
146: Logger.getLogger(getClass()).debug(
147: "Visiting " + attribute.getExceptionHandlers().size()
148: + " exception handler(s) ...");
149: for (ExceptionHandler exceptionHandler : attribute
150: .getExceptionHandlers()) {
151: exceptionHandler.accept(this );
152: }
153:
154: Logger.getLogger(getClass()).debug(
155: "Visiting " + attribute.getAttributes().size()
156: + " code attribute(s) ...");
157: for (Attribute_info attribute_info : attribute.getAttributes()) {
158: attribute_info.accept(this );
159: }
160: }
161:
162: public void visitExceptions_attribute(Exceptions_attribute attribute) {
163: Logger.getLogger(getClass()).debug(
164: "Visiting " + attribute.getExceptions().size()
165: + " exception class(es) ...");
166:
167: for (Class_info exception : attribute.getExceptions()) {
168: exception.accept(this );
169: }
170: }
171:
172: public void visitInnerClasses_attribute(
173: InnerClasses_attribute attribute) {
174: Logger.getLogger(getClass()).debug(
175: "Visiting " + attribute.getInnerClasses().size()
176: + " inner class(es) ...");
177:
178: for (InnerClass innerClass : attribute.getInnerClasses()) {
179: innerClass.accept(this );
180: }
181: }
182:
183: public void visitSynthetic_attribute(Synthetic_attribute attribute) {
184: // Do nothing
185: }
186:
187: public void visitSourceFile_attribute(SourceFile_attribute attribute) {
188: // Do nothing
189: }
190:
191: public void visitLineNumberTable_attribute(
192: LineNumberTable_attribute attribute) {
193: Logger.getLogger(getClass()).debug(
194: "Visiting " + attribute.getLineNumbers().size()
195: + " line number(s) ...");
196:
197: for (LineNumber lineNumber : attribute.getLineNumbers()) {
198: lineNumber.accept(this );
199: }
200: }
201:
202: public void visitLocalVariableTable_attribute(
203: LocalVariableTable_attribute attribute) {
204: Logger.getLogger(getClass()).debug(
205: "Visiting " + attribute.getLocalVariables().size()
206: + " local variable(s) ...");
207:
208: for (LocalVariable localVariable : attribute
209: .getLocalVariables()) {
210: localVariable.accept(this );
211: }
212: }
213:
214: public void visitDeprecated_attribute(Deprecated_attribute attribute) {
215: // Do nothing
216: }
217:
218: public void visitCustom_attribute(Custom_attribute attribute) {
219: // Do nothing
220: }
221:
222: // Attribute helpers
223: public void visitInstruction(Instruction helper) {
224: }
225:
226: public void visitExceptionHandler(ExceptionHandler helper) {
227: }
228:
229: public void visitInnerClass(InnerClass helper) {
230: }
231:
232: public void visitLineNumber(LineNumber helper) {
233: }
234:
235: public void visitLocalVariable(LocalVariable helper) {
236: }
237: }
|