KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > constant > ConstantAnalysis


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2005, 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 package edu.umd.cs.findbugs.ba.constant;
20
21 import org.apache.bcel.classfile.Method;
22 import org.apache.bcel.generic.InstructionHandle;
23 import org.apache.bcel.generic.MethodGen;
24
25 import edu.umd.cs.findbugs.ba.BasicBlock;
26 import edu.umd.cs.findbugs.ba.CFGBuilderException;
27 import edu.umd.cs.findbugs.ba.ClassContext;
28 import edu.umd.cs.findbugs.ba.Dataflow;
29 import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
30 import edu.umd.cs.findbugs.ba.DataflowTestDriver;
31 import edu.umd.cs.findbugs.ba.DepthFirstSearch;
32 import edu.umd.cs.findbugs.ba.Edge;
33 import edu.umd.cs.findbugs.ba.FrameDataflowAnalysis;
34 import edu.umd.cs.findbugs.ba.Location;
35
36 /**
37  * Dataflow analysis to find constant values.
38  *
39  * @see edu.umd.cs.findbugs.ba.constant.Constant
40  * @author David Hovemeyer
41  */

42 public class ConstantAnalysis extends FrameDataflowAnalysis<Constant, ConstantFrame> {
43     private MethodGen methodGen;
44     private ConstantFrameModelingVisitor visitor;
45     
46     public ConstantAnalysis(MethodGen methodGen, DepthFirstSearch dfs) {
47         super(dfs);
48         this.methodGen = methodGen;
49         this.visitor = new ConstantFrameModelingVisitor(methodGen.getConstantPool());
50     }
51     
52     public ConstantFrame createFact() {
53         return new ConstantFrame(methodGen.getMaxLocals());
54     }
55     
56     public void initEntryFact(ConstantFrame frame) {
57         frame.setValid();
58         frame.clearStack();
59         int numSlots = frame.getNumSlots();
60         for (int i = 0; i < numSlots; ++i) {
61             frame.setValue(i, new Constant("parameter" + i));
62         }
63     }
64     
65     @Override JavaDoc
66          public void transferInstruction(
67             InstructionHandle handle,
68             BasicBlock basicBlock,
69             ConstantFrame frame) throws DataflowAnalysisException {
70         visitor.setFrameAndLocation(frame, new Location(handle, basicBlock));
71         visitor.analyzeInstruction(handle.getInstruction());
72     }
73     
74     public void meetInto(
75             ConstantFrame fact,
76             Edge edge,
77             ConstantFrame result) throws DataflowAnalysisException {
78         
79         if (fact.isValid()) {
80             ConstantFrame tmpFact = null;
81         
82             if (edge.isExceptionEdge()) {
83                 tmpFact = modifyFrame(fact, tmpFact);
84                 tmpFact.clearStack();
85                 tmpFact.pushValue(Constant.NOT_CONSTANT);
86             }
87         
88             if (tmpFact != null) {
89                 fact = tmpFact;
90             }
91         }
92         
93         mergeInto(fact, result);
94     }
95     
96     @Override JavaDoc
97          protected void mergeValues(ConstantFrame otherFrame, ConstantFrame resultFrame, int slot)
98             throws DataflowAnalysisException {
99         Constant value = Constant.merge(resultFrame.getValue(slot), otherFrame.getValue(slot));
100         resultFrame.setValue(slot, value);
101     }
102     
103     /*
104      * Test driver.
105      */

106     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
107         if (argv.length != 1) {
108             System.err.println("Usage: " + ConstantAnalysis.class.getName() + " <class file>");
109             System.exit(1);
110         }
111         
112         DataflowTestDriver<ConstantFrame, ConstantAnalysis> driver =
113             new DataflowTestDriver<ConstantFrame, ConstantAnalysis>() {
114                 @Override JavaDoc
115                                  public Dataflow<ConstantFrame, ConstantAnalysis> createDataflow(
116                         ClassContext classContext,
117                         Method method) throws CFGBuilderException, DataflowAnalysisException {
118                     return classContext.getConstantDataflow(method);
119                 }
120             };
121             
122         driver.execute(argv[0]);
123     }
124 }
125
Popular Tags