1 19 20 package edu.umd.cs.findbugs.ba; 21 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 25 import org.apache.bcel.generic.ATHROW; 26 import org.apache.bcel.generic.ConstantPoolGen; 27 import org.apache.bcel.generic.GotoInstruction; 28 import org.apache.bcel.generic.INVOKESTATIC; 29 import org.apache.bcel.generic.IfInstruction; 30 import org.apache.bcel.generic.InstructionHandle; 31 import org.apache.bcel.generic.ReturnInstruction; 32 import org.apache.bcel.generic.Select; 33 34 42 public class TargetEnumeratingVisitor extends org.apache.bcel.generic.EmptyVisitor 43 implements EdgeTypes { 44 45 private InstructionHandle handle; 46 private ConstantPoolGen constPoolGen; 47 private LinkedList <Target> targetList; 48 private boolean isBranch, isReturn, isThrow, isExit; 49 50 56 public TargetEnumeratingVisitor(InstructionHandle handle, ConstantPoolGen constPoolGen) { 57 this.handle = handle; 58 this.constPoolGen = constPoolGen; 59 targetList = new LinkedList <Target>(); 60 isBranch = isReturn = isThrow = isExit = false; 61 62 handle.getInstruction().accept(this); 63 } 64 65 68 public boolean isEndOfBasicBlock() { 69 return isBranch || isReturn || isThrow || isExit; 70 } 71 72 75 public boolean instructionIsReturn() { 76 return isReturn; 77 } 78 79 82 public boolean instructionIsThrow() { 83 return isThrow; 84 } 85 86 89 public boolean instructionIsExit() { 90 return isExit; 91 } 92 93 97 public Iterator <Target> targetIterator() { 98 return targetList.iterator(); 99 } 100 101 @Override 102 public void visitGotoInstruction(GotoInstruction ins) { 103 isBranch = true; 104 InstructionHandle target = ins.getTarget(); 105 if (target == null) throw new IllegalStateException (); 106 targetList.add(new Target(target, GOTO_EDGE)); 107 } 108 109 @Override 110 public void visitIfInstruction(IfInstruction ins) { 111 isBranch = true; 112 InstructionHandle target = ins.getTarget(); 113 if (target == null) throw new IllegalStateException (); 114 targetList.add(new Target(target, IFCMP_EDGE)); 115 InstructionHandle fallThrough = handle.getNext(); 116 targetList.add(new Target(fallThrough, FALL_THROUGH_EDGE)); 117 } 118 119 @Override 120 public void visitSelect(Select ins) { 121 isBranch = true; 122 123 InstructionHandle[] targets = ins.getTargets(); 125 for (InstructionHandle target : targets) { 126 targetList.add(new Target(target, SWITCH_EDGE)); 127 } 128 129 InstructionHandle defaultTarget = ins.getTarget(); 131 if (defaultTarget == null) { 132 throw new IllegalStateException (); 133 } 134 targetList.add(new Target(defaultTarget, SWITCH_DEFAULT_EDGE)); 135 } 136 137 @Override 138 public void visitReturnInstruction(ReturnInstruction ins) { 139 isReturn = true; 140 } 141 142 @Override 143 public void visitATHROW(ATHROW ins) { 144 isThrow = true; 145 } 146 147 @Override 148 public void visitINVOKESTATIC(INVOKESTATIC ins) { 149 151 String className = ins.getClassName(constPoolGen); 152 String methodName = ins.getName(constPoolGen); 153 String methodSig = ins.getSignature(constPoolGen); 154 155 if (className.equals("java.lang.System") && methodName.equals("exit") && methodSig.equals("(I)V")) 156 isExit = true; 157 } 158 159 } 160 | Popular Tags |