KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > InstructionScannerDriver


1 /*
2  * FindBugs - Find bugs in Java programs
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;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24
25 import org.apache.bcel.generic.InstructionHandle;
26
27 import edu.umd.cs.findbugs.ba.BasicBlock;
28 import edu.umd.cs.findbugs.ba.Edge;
29
30 /**
31  * Drive an InstructionScannerGenerator over the instructions of
32  * a simple path. The generator will create scanners at certain instructions.
33  * Each instruction and edge is fed to all scanners so created.
34  */

35 public class InstructionScannerDriver {
36     private Iterator JavaDoc<Edge> edgeIter;
37     private LinkedList JavaDoc<InstructionScanner> scannerList;
38
39     private static final boolean DEBUG = SystemProperties.getBoolean("isd.debug");
40
41     /**
42      * Constructor.
43      *
44      * @param edgeIter iterator over Edges specifying path to be scanned
45      */

46     public InstructionScannerDriver(Iterator JavaDoc<Edge> edgeIter) {
47         this.edgeIter = edgeIter;
48         scannerList = new LinkedList JavaDoc<InstructionScanner>();
49     }
50
51     /**
52      * Execute by driving the InstructionScannerGenerator over all instructions.
53      * Each generated InstructionScanner is driven over all instructions and
54      * edges.
55      *
56      * @param generator the InstructionScannerGenerator
57      */

58     public void execute(InstructionScannerGenerator generator) {
59         // Pump the instructions in the path through the generator and all generated scanners
60
while (edgeIter.hasNext()) {
61             Edge edge = edgeIter.next();
62             BasicBlock source = edge.getSource();
63             if (DEBUG) System.out.println("ISD: scanning instructions in block " + source.getId());
64
65             // Traverse all instructions in the source block
66
Iterator JavaDoc<InstructionHandle> i = source.instructionIterator();
67             int count = 0;
68             while (i.hasNext()) {
69                 InstructionHandle handle = i.next();
70
71                 // Check if the generator wants to create a new scanner
72
if (generator.start(handle))
73                     scannerList.add(generator.createScanner());
74
75                 // Pump the instruction into all scanners
76
for (InstructionScanner scanner : scannerList) {
77                     scanner.scanInstruction(handle);
78                 }
79
80                 ++count;
81             }
82
83             if (DEBUG) System.out.println("ISD: scanned " + count + " instructions");
84
85             // Now that we've finished the source block, pump the edge
86
// into all scanners
87
for (InstructionScanner scanner : scannerList) {
88                 scanner.traverseEdge(edge);
89             }
90         }
91     }
92 }
93
94 // vim:ts=4
95
Popular Tags