KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > TargetEnumeratingVisitor


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2003,2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.ba;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
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 /**
35  * Visitor to find all of the targets of an instruction
36  * whose InstructionHandle is given.
37  * Note that we don't consider exception edges.
38  *
39  * @author David Hovemeyer
40  * @author Chadd Williams
41  */

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 JavaDoc<Target> targetList;
48     private boolean isBranch, isReturn, isThrow, isExit;
49
50     /**
51      * Constructor.
52      *
53      * @param handle the handle of the instruction whose targets should be enumerated
54      * @param constPoolGen the ConstantPoolGen object for the class
55      */

56     public TargetEnumeratingVisitor(InstructionHandle handle, ConstantPoolGen constPoolGen) {
57         this.handle = handle;
58         this.constPoolGen = constPoolGen;
59         targetList = new LinkedList JavaDoc<Target>();
60         isBranch = isReturn = isThrow = isExit = false;
61
62         handle.getInstruction().accept(this);
63     }
64
65     /**
66      * Is the instruction the end of a basic block?
67      */

68     public boolean isEndOfBasicBlock() {
69         return isBranch || isReturn || isThrow || isExit;
70     }
71
72     /**
73      * Is the analyzed instruction a method return?
74      */

75     public boolean instructionIsReturn() {
76         return isReturn;
77     }
78
79     /**
80      * Is the analyzed instruction an explicit throw?
81      */

82     public boolean instructionIsThrow() {
83         return isThrow;
84     }
85
86     /**
87      * Is the analyzed instruction an exit (call to System.exit())?
88      */

89     public boolean instructionIsExit() {
90         return isExit;
91     }
92
93     /**
94      * Iterate over Target objects representing control flow targets
95      * and their edge types.
96      */

97     public Iterator JavaDoc<Target> targetIterator() {
98         return targetList.iterator();
99     }
100
101     @Override JavaDoc
102          public void visitGotoInstruction(GotoInstruction ins) {
103         isBranch = true;
104         InstructionHandle target = ins.getTarget();
105         if (target == null) throw new IllegalStateException JavaDoc();
106         targetList.add(new Target(target, GOTO_EDGE));
107     }
108
109     @Override JavaDoc
110          public void visitIfInstruction(IfInstruction ins) {
111         isBranch = true;
112         InstructionHandle target = ins.getTarget();
113         if (target == null) throw new IllegalStateException JavaDoc();
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 JavaDoc
120          public void visitSelect(Select ins) {
121         isBranch = true;
122
123         // Add non-default switch edges.
124
InstructionHandle[] targets = ins.getTargets();
125         for (InstructionHandle target : targets) {
126             targetList.add(new Target(target, SWITCH_EDGE));
127         }
128
129         // Add default switch edge.
130
InstructionHandle defaultTarget = ins.getTarget();
131         if (defaultTarget == null) {
132             throw new IllegalStateException JavaDoc();
133         }
134         targetList.add(new Target(defaultTarget, SWITCH_DEFAULT_EDGE));
135     }
136
137     @Override JavaDoc
138          public void visitReturnInstruction(ReturnInstruction ins) {
139         isReturn = true;
140     }
141
142     @Override JavaDoc
143          public void visitATHROW(ATHROW ins) {
144         isThrow = true;
145     }
146
147     @Override JavaDoc
148          public void visitINVOKESTATIC(INVOKESTATIC ins) {
149         // Find calls to System.exit(), since this effectively terminates the basic block.
150

151         String JavaDoc className = ins.getClassName(constPoolGen);
152         String JavaDoc methodName = ins.getName(constPoolGen);
153         String JavaDoc 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