KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2003-2006, 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.IdentityHashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.bcel.generic.InstructionHandle;
27
28 /**
29  * A useful starting point for defining a dataflow analysis.
30  * Handles access and caching of start and result facts for
31  * basic blocks.
32  *
33  * <p>
34  * Subclasses that model instructions within basic blocks
35  * should extend AbstractDataflowAnalysis.
36  * </p>
37  *
38  * @author David Hovemeyer
39  */

40 public abstract class BasicAbstractDataflowAnalysis<Fact> implements DataflowAnalysis<Fact> {
41     private IdentityHashMap JavaDoc<BasicBlock, Fact> startFactMap;
42     private IdentityHashMap JavaDoc<BasicBlock, Fact> resultFactMap;
43
44     /**
45      * Constructor.
46      */

47     public BasicAbstractDataflowAnalysis() {
48         this.startFactMap = new IdentityHashMap JavaDoc<BasicBlock, Fact>();
49         this.resultFactMap = new IdentityHashMap JavaDoc<BasicBlock, Fact>();
50     }
51
52     /**
53      * Get an iterator over the result facts.
54      */

55     public Iterator JavaDoc<Fact> resultFactIterator() {
56         return resultFactMap.values().iterator();
57     }
58
59     /**
60      * Call this to get a dataflow value as a String.
61      * By default, we just call toString().
62      * Subclasses may override to get different behavior.
63      */

64     public String JavaDoc factToString(Fact fact) {
65         return fact.toString();
66     }
67
68     public Fact getStartFact(BasicBlock block) {
69         return lookupOrCreateFact(startFactMap, block);
70     }
71
72     public Fact getResultFact(BasicBlock block) {
73         return lookupOrCreateFact(resultFactMap, block);
74     }
75
76     /**
77      * Get dataflow fact at (just before) given Location.
78      * Note "before" is meant in the logical sense, so for backward analyses,
79      * before means after the location in the control flow sense.
80      *
81      * <p>
82      * The default implementation ignores instructions within basic blocks.
83      * Subclasses that model individual instructions must override this method.
84      * </p>
85      *
86      * @param location the Location
87      * @return the dataflow value at given Location
88      * @throws DataflowAnalysisException
89      */

90     public Fact getFactAtLocation(Location location) throws DataflowAnalysisException {
91         return getStartFact(location.getBasicBlock());
92     }
93
94     /**
95      * Get the dataflow fact representing the point just after given Location.
96      * Note "after" is meant in the logical sense, so for backward analyses,
97      * after means before the location in the control flow sense.
98      *
99      * <p>
100      * The default implementation ignores instructions within basic blocks.
101      * Subclasses that model individual instructions must override this method.
102      * </p>
103      *
104      * @param location the Location
105      * @return the dataflow value after given Location
106      * @throws DataflowAnalysisException
107      */

108     public Fact getFactAfterLocation(Location location) throws DataflowAnalysisException {
109         if (location.getBasicBlock().isEmpty()) {
110             return getResultFact(location.getBasicBlock());
111         }
112         
113         InstructionHandle logicalLastInstruction = isForwards()
114             ? location.getBasicBlock().getLastInstruction()
115             : location.getBasicBlock().getFirstInstruction();
116             
117         if (location.getHandle() == logicalLastInstruction) {
118             return getResultFact(location.getBasicBlock());
119         } else {
120             return getStartFact(location.getBasicBlock());
121         }
122     }
123     
124     /**
125      * Get the fact that is true at the target of the given control edge.
126      *
127      * @param edge the edge
128      * @return the fact that is true at the target of the edge
129      * @throws DataflowAnalysisException
130      */

131     public Fact getFactOnEdge(Edge edge) throws DataflowAnalysisException {
132         BasicBlock block = isForwards() ? edge.getSource() : edge.getTarget();
133         
134         Fact predFact = createFact();
135         copy(getResultFact(block), predFact);
136         
137         edgeTransfer(edge, predFact);
138         
139         Fact result = createFact();
140         makeFactTop(result);
141         meetInto(predFact, edge, result);
142
143         return result;
144     }
145     
146     /* (non-Javadoc)
147      * @see edu.umd.cs.findbugs.ba.DataflowAnalysis#startIteration()
148      */

149     public void startIteration() {
150         // Do nothing - subclass may override
151
}
152     
153     /* (non-Javadoc)
154      * @see edu.umd.cs.findbugs.ba.DataflowAnalysis#finishIteration()
155      */

156     public void finishIteration() {
157         // Do nothing - subclass may override
158
}
159     
160     /* (non-Javadoc)
161      * @see edu.umd.cs.findbugs.ba.DataflowAnalysis#edgeTransfer(edu.umd.cs.findbugs.ba.Edge, java.lang.Object)
162      */

163     public void edgeTransfer(Edge edge, Fact fact) throws DataflowAnalysisException {
164         // By default, edge transfer function is identity.
165
// Subclasses may override.
166
}
167
168     private Fact lookupOrCreateFact(Map JavaDoc<BasicBlock, Fact> map, BasicBlock block) {
169         Fact fact = map.get(block);
170         if (fact == null) {
171             fact = createFact();
172             map.put(block, fact);
173         }
174         return fact;
175     }
176     
177     public int getLastUpdateTimestamp(Fact fact) {
178         return 0;
179     }
180     public void setLastUpdateTimestamp(Fact fact, int lastUpdateTimestamp) {
181         
182     }
183     
184 }
185
Popular Tags