KickJava   Java API By Example, From Geeks To Geeks.

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


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 edu.umd.cs.findbugs.annotations.Nullable;
23
24 /**
25  * A convenient base class for dataflow analysis classes which
26  * use Frames as values.
27  *
28  * @author David Hovemeyer
29  * @see Frame
30  * @see DataflowAnalysis
31  */

32 public abstract class FrameDataflowAnalysis <ValueType, FrameType extends Frame<ValueType>> extends ForwardDataflowAnalysis<FrameType> {
33     public FrameDataflowAnalysis(DepthFirstSearch dfs) {
34         super(dfs);
35     }
36
37     public void copy(FrameType source, FrameType dest) {
38         dest.copyFrom(source);
39     }
40
41     public void initResultFact(FrameType result) {
42         result.setTop();
43     }
44
45     public void makeFactTop(FrameType fact) {
46         fact.setTop();
47     }
48     public boolean isTop(FrameType fact) {
49         return fact.isTop();
50     }
51     public boolean same(FrameType fact1, FrameType fact2) {
52         return fact1.sameAs(fact2);
53     }
54
55     @Override JavaDoc
56          public boolean isFactValid(FrameType fact) {
57         return fact.isValid();
58     }
59
60     @Override JavaDoc
61     public int getLastUpdateTimestamp(FrameType fact) {
62         return fact.getLastUpdateTimestamp();
63     }
64     @Override JavaDoc
65     public void setLastUpdateTimestamp(FrameType fact, int lastTimestamp) {
66         fact.setLastUpdateTimestamp(lastTimestamp);
67     }
68     
69     /**
70      * Create a modifiable copy of a frame.
71      * This is useful for meetInto(), if the frame needs to be
72      * modified in a path-sensitive fashion.
73      * A typical usage pattern is:
74      * <p/>
75      * <pre>
76      * FrameType copy = null;
77      * if (someCondition()) {
78      * copy = modifyFrame(fact, copy);
79      * // modify copy
80      * }
81      * if (someOtherCondition()) {
82      * copy = modifyFrame(fact, copy);
83      * // modify copy
84      * }
85      * if (copy != null)
86      * fact = copy;
87      * <p/>
88      * mergeInto(fact, result);
89      * </pre>
90      * <p/>
91      * The advantage of using modifyFrame() is that new code can be added
92      * before or after other places where the frame is modified, and the
93      * code will remain correct.
94      *
95      * @param orig the original frame
96      * @param copy the modifiable copy (returned by a previous call to modifyFrame()),
97      * or null if this is the first time modifyFrame() is being called
98      * @return a modifiable copy of fact
99      */

100     final protected FrameType modifyFrame(FrameType orig, @Nullable FrameType copy) {
101         if (copy == null) {
102             copy = createFact();
103             copy.copyFrom(orig);
104         }
105         return copy;
106     }
107
108     /**
109      * Merge one frame into another.
110      *
111      * @param other the frame to merge with the result
112      * @param result the result frame, which is modified to be the
113      * merge of the two frames
114      */

115     protected void mergeInto(FrameType other, FrameType result) throws DataflowAnalysisException {
116         // Handle if result Frame or the other Frame is the special "TOP" value.
117
if (result.isTop()) {
118             // Result is the identity element, so copy the other Frame
119
result.copyFrom(other);
120             return;
121         } else if (other.isTop()) {
122             // Other Frame is the identity element, so result stays the same
123
return;
124         }
125
126         // Handle if result Frame or the other Frame is the special "BOTTOM" value.
127
if (result.isBottom()) {
128             // Result is the bottom element, so it stays that way
129
return;
130         } else if (other.isBottom()) {
131             // Other Frame is the bottom element, so result becomes the bottom element too
132
result.setBottom();
133             return;
134         }
135
136         // If the number of slots in the Frames differs,
137
// then the result is the special "BOTTOM" value.
138
if (result.getNumSlots() != other.getNumSlots()) {
139             result.setBottom();
140             return;
141         }
142
143         // Usual case: ordinary Frames consisting of the same number of values.
144
// Merge each value in the two slot lists element-wise.
145
for (int i = 0; i < result.getNumSlots(); ++i) {
146             mergeValues(other, result, i);
147         }
148     }
149
150     /**
151      * Merge the values contained in a given slot of two Frames.
152      *
153      * @param otherFrame a Frame
154      * @param resultFrame a Frame which will contain the resulting merged value
155      * @param slot a slot in both frames
156      * @throws DataflowAnalysisException
157      */

158     protected abstract void mergeValues(FrameType otherFrame, FrameType resultFrame, int slot)
159             throws DataflowAnalysisException;
160 }
161
162 // vim:ts=4
163
Popular Tags