001: //Tested with BCEL-5.1
002: //http://jakarta.apache.org/builds/jakarta-bcel/release/v5.1/
003:
004: package com.puppycrawl.tools.checkstyle.bcel;
005:
006: import java.util.HashSet;
007: import java.util.Iterator;
008: import java.util.Set;
009:
010: import org.apache.bcel.classfile.Code;
011: import org.apache.bcel.classfile.ConstantPool;
012: import org.apache.bcel.classfile.EmptyVisitor;
013: import org.apache.bcel.classfile.Field;
014: import org.apache.bcel.classfile.JavaClass;
015: import org.apache.bcel.classfile.LocalVariable;
016: import org.apache.bcel.classfile.LocalVariableTable;
017: import org.apache.bcel.classfile.Method;
018: import org.apache.bcel.classfile.Visitor;
019: import org.apache.bcel.generic.InstructionHandle;
020: import org.apache.bcel.generic.InstructionList;
021:
022: /**
023: * Manages a set of visitors that are accepted by nodes visited by
024: * a VisitorSet. Any visit to this object is passed on to its managed
025: * visitors.
026: * @author Rick Giles
027: */
028: // TODO: review visitXXX
029: public class VisitorSet extends EmptyVisitor {
030: /** the managed visitors */
031: private Set mVisitors = new HashSet();
032:
033: /** Creates a <code>VisitorSet</code> for a set of visitors.
034: * @param aVisitors the set of managed visitors.
035: */
036: public VisitorSet(Set aVisitors) {
037: mVisitors = aVisitors;
038: }
039:
040: /**
041: * @see org.apache.bcel.classfile.Visitor#visitCode
042: */
043: public void visitCode(Code aCode) {
044: for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
045: IDeepVisitor visitor = (IDeepVisitor) iter.next();
046: Visitor v = visitor.getClassFileVisitor();
047: aCode.accept(v);
048: }
049:
050: // perform a deep visit
051: final byte[] code = aCode.getCode();
052: final InstructionList list = new InstructionList(code);
053: final Iterator it = list.iterator();
054: for (Iterator iter = list.iterator(); iter.hasNext();) {
055: InstructionHandle instruction = (InstructionHandle) iter
056: .next();
057: visitInstructionHandle(instruction);
058: }
059: }
060:
061: /**
062: * Deep visit of an InstructionHandle
063: * @param aInstruction the InstructionHandle
064: */
065: private void visitInstructionHandle(InstructionHandle aInstruction) {
066: for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
067: final IDeepVisitor visitor = (IDeepVisitor) iter.next();
068: org.apache.bcel.generic.Visitor v = visitor
069: .getGenericVisitor();
070: aInstruction.accept(v);
071: }
072: }
073:
074: /**
075: * @see org.apache.bcel.classfile.Visitor
076: */
077: public void visitConstantPool(ConstantPool aConstantPool) {
078: for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
079: IDeepVisitor visitor = (IDeepVisitor) iter.next();
080: Visitor v = visitor.getClassFileVisitor();
081: aConstantPool.accept(v);
082: }
083: }
084:
085: /**
086: * @see org.apache.bcel.classfile.Visitor
087: */
088: public void visitField(Field aField) {
089: for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
090: IDeepVisitor visitor = (IDeepVisitor) iter.next();
091: Visitor v = visitor.getClassFileVisitor();
092: aField.accept(v);
093: }
094: }
095:
096: /**
097: * @see org.apache.bcel.classfile.Visitor
098: */
099: public void visitJavaClass(JavaClass aJavaClass) {
100: for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
101: IDeepVisitor visitor = (IDeepVisitor) iter.next();
102: Visitor v = visitor.getClassFileVisitor();
103: aJavaClass.accept(v);
104: }
105: }
106:
107: // /**
108: // * @see org.apache.bcel.classfile.Visitor
109: // */
110: // public void visitLocalVariable(LocalVariable aLocalVariable)
111: // {
112: // for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
113: // IDeepVisitor visitor = (IDeepVisitor) iter.next();
114: // Visitor v = visitor.getClassFileVisitor();
115: // aLocalVariable.accept(v);
116: // }
117: // }
118: //
119: /**
120: * @see org.apache.bcel.classfile.Visitor
121: */
122: public void visitLocalVariableTable(LocalVariableTable aTable) {
123: for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
124: IDeepVisitor visitor = (IDeepVisitor) iter.next();
125: Visitor v = visitor.getClassFileVisitor();
126: aTable.accept(v);
127: }
128: }
129:
130: /**
131: * @see org.apache.bcel.classfile.Visitor
132: */
133: public void visitMethod(Method aMethod) {
134: for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
135: IDeepVisitor visitor = (IDeepVisitor) iter.next();
136: Visitor v = visitor.getClassFileVisitor();
137: aMethod.accept(v);
138: }
139: }
140: }
|