KickJava   Java API By Example, From Geeks To Geeks.

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


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.generic.BIPUSH;
22 import org.apache.bcel.generic.ConstantPoolGen;
23 import org.apache.bcel.generic.ICONST;
24 import org.apache.bcel.generic.IINC;
25 import org.apache.bcel.generic.LDC;
26 import org.apache.bcel.generic.LDC2_W;
27 import org.apache.bcel.generic.SIPUSH;
28
29 import edu.umd.cs.findbugs.ba.AbstractFrameModelingVisitor;
30
31 /**
32  * Visitor to model the effect of bytecode instructions
33  * on ConstantFrames.
34  *
35  * <p>For now, only String constants are modeled.
36  * In the future we can add other kinds of constants.</p>
37  *
38  * @see edu.umd.cs.findbugs.ba.constant.ConstantAnalysis
39  * @author David Hovemeyer
40  */

41 public class ConstantFrameModelingVisitor
42         extends AbstractFrameModelingVisitor<Constant, ConstantFrame> {
43
44     public ConstantFrameModelingVisitor(ConstantPoolGen cpg) {
45         super(cpg);
46     }
47             
48     @Override JavaDoc
49     public Constant getDefaultValue() {
50         return Constant.NOT_CONSTANT;
51     }
52     @Override JavaDoc
53     public void visitIINC(IINC obj) {
54         // System.out.println("before iinc: " + getFrame());
55
int v = obj.getIndex();
56         int amount = obj.getIncrement();
57         ConstantFrame f = getFrame();
58         Constant c = f.getValue(v);
59         if (c.isConstantInteger())
60             f.setValue(v, new Constant(c.getConstantInt() + amount));
61         else f.setValue(v, Constant.NOT_CONSTANT);
62         // System.out.println("after iinc: " + getFrame());
63
}
64
65     @Override JavaDoc
66     public void visitICONST(ICONST obj) {
67         Number JavaDoc value = obj.getValue();
68         Constant c = new Constant(value);
69         getFrame().pushValue(c);
70         }
71
72
73     @Override JavaDoc
74     public void visitBIPUSH(BIPUSH obj) {
75         Number JavaDoc value = obj.getValue();
76         Constant c = new Constant(value);
77         getFrame().pushValue(c);
78     }
79     
80     @Override JavaDoc
81     public void visitSIPUSH(SIPUSH obj) {
82         Number JavaDoc value = obj.getValue();
83         Constant c = new Constant(value);
84         getFrame().pushValue(c);
85     }
86
87     
88     @Override JavaDoc
89     public void visitLDC(LDC obj) {
90         Object JavaDoc value = obj.getValue(getCPG());
91         Constant c = new Constant(value);
92         getFrame().pushValue(c);
93     }
94     
95     @Override JavaDoc
96     public void visitLDC2_W(LDC2_W obj) {
97         Object JavaDoc value = obj.getValue(getCPG());
98         Constant c = new Constant(value);
99         getFrame().pushValue(c);
100         getFrame().pushValue(c);
101     }
102
103 }
104
Popular Tags