KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Bytecode Analysis Framework
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 import edu.umd.cs.findbugs.SystemProperties;
29 import edu.umd.cs.findbugs.annotations.CheckForNull;
30 import edu.umd.cs.findbugs.annotations.CheckReturnValue;
31
32 /**
33  * Abstract base class providing functionality that will be useful
34  * for most dataflow analysis implementations that model instructions
35  * within basic blocks.
36  *
37  * @author David Hovemeyer
38  * @see Dataflow
39  * @see DataflowAnalysis
40  */

41 public abstract class AbstractDataflowAnalysis <Fact> extends BasicAbstractDataflowAnalysis<Fact> {
42     private static final boolean DEBUG = SystemProperties.getBoolean("dataflow.transfer");
43
44     /* ----------------------------------------------------------------------
45      * Public methods
46      * ---------------------------------------------------------------------- */

47
48     /**
49      * Transfer function for a single instruction.
50      *
51      * @param handle the instruction
52      * @param basicBlock the BasicBlock containing the instruction; needed to disambiguate
53      * instructions in inlined JSR subroutines
54      * @param fact which should be modified based on the instruction
55      */

56     public abstract void transferInstruction(InstructionHandle handle, BasicBlock basicBlock, Fact fact) throws DataflowAnalysisException;
57
58     /**
59      * Determine whether the given fact is <em>valid</em>
60      * (neither top nor bottom).
61      */

62     @CheckReturnValue
63     public abstract boolean isFactValid(Fact fact);
64
65     /**
66      * Get the dataflow fact representing the point just before given Location.
67      * Note "before" is meant in the logical sense, so for backward analyses,
68      * before means after the location in the control flow sense.
69      *
70      * @param location the location
71      * @return the fact at the point just before the location
72      */

73     @Override JavaDoc
74     public Fact getFactAtLocation(Location location) throws DataflowAnalysisException {
75         Fact start = getStartFact(location.getBasicBlock());
76         Fact result = createFact();
77         makeFactTop(result);
78         transfer(location.getBasicBlock(), location.getHandle(), start, result);
79         return result;
80     }
81
82     /**
83      * Get the dataflow fact representing the point just after given Location.
84      * Note "after" is meant in the logical sense, so for backward analyses,
85      * after means before the location in the control flow sense.
86      */

87     @Override JavaDoc
88     public Fact getFactAfterLocation(Location location) throws DataflowAnalysisException {
89         BasicBlock basicBlock = location.getBasicBlock();
90         InstructionHandle handle = location.getHandle();
91
92         if (handle == (isForwards() ? basicBlock.getLastInstruction() : basicBlock.getFirstInstruction()))
93             return getResultFact(basicBlock);
94         else
95             return getFactAtLocation(new Location(isForwards() ? handle.getNext() : handle.getPrev(), basicBlock));
96     }
97
98     /* ----------------------------------------------------------------------
99      * Implementations of interface methods
100      * ---------------------------------------------------------------------- */

101
102     public void transfer(BasicBlock basicBlock, @CheckForNull InstructionHandle end, Fact start, Fact result) throws DataflowAnalysisException {
103         copy(start, result);
104
105         if (isFactValid(result)) {
106             Iterator JavaDoc<InstructionHandle> i = isForwards() ? basicBlock.instructionIterator() : basicBlock.instructionReverseIterator();
107
108             while (i.hasNext()) {
109                 InstructionHandle handle = i.next();
110                 if (handle == end)
111                     break;
112
113                 if (DEBUG && end == null) System.out.print("Transfer " + handle);
114     
115                 // Transfer the dataflow value
116
transferInstruction(handle, basicBlock, result);
117
118                 if (DEBUG && end == null) System.out.println(" ==> " + result.toString());
119             }
120         }
121     }
122
123 }
124
125 // vim:ts=4
126
Popular Tags