KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > bcp > FieldAccess


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.bcp;
21
22 import org.apache.bcel.generic.ConstantPoolGen;
23 import org.apache.bcel.generic.FieldInstruction;
24 import org.apache.bcel.generic.Type;
25
26 import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
27 import edu.umd.cs.findbugs.ba.vna.ValueNumber;
28 import edu.umd.cs.findbugs.ba.vna.ValueNumberFrame;
29
30 /**
31  * Base class for Load and Store PatternElements.
32  * Handles some of the grunt work of representing fields and
33  * extracting field values from the stack frame.
34  *
35  * @author David Hovemeyer
36  * @see Load
37  * @see Store
38  */

39 public abstract class FieldAccess extends SingleInstruction implements org.apache.bcel.Constants {
40     private String JavaDoc fieldVarName;
41     private String JavaDoc valueVarName;
42
43     /**
44      * Constructor.
45      *
46      * @param fieldVarName name of the variable to bind to the field
47      * @param valueVarName name of the variable to bind to the value store in or loaded from the field
48      */

49     public FieldAccess(String JavaDoc fieldVarName, String JavaDoc valueVarName) {
50         this.fieldVarName = fieldVarName;
51         this.valueVarName = valueVarName;
52     }
53
54     /**
55      * Check that the Variables determined for the field and the value loaded/stored
56      * are consistent with previous variable definitions.
57      *
58      * @param field Variable representing the field
59      * @param value Variable representing the value loaded/stored
60      * @param bindingSet previous definitions
61      * @return a MatchResult containing an updated BindingSet if successful,
62      * or null if unsucessful
63      */

64     protected MatchResult checkConsistent(Variable field, Variable value, BindingSet bindingSet) {
65         // Ensure that the field and value variables are consistent with
66
// previous definitions (if any)
67
bindingSet = addOrCheckDefinition(fieldVarName, field, bindingSet);
68         if (bindingSet == null)
69             return null;
70         bindingSet = addOrCheckDefinition(valueVarName, value, bindingSet);
71         if (bindingSet == null)
72             return null;
73         return new MatchResult(this, bindingSet);
74     }
75
76     /**
77      * Return whether the given FieldInstruction accesses a long or double field.
78      *
79      * @param fieldIns the FieldInstruction
80      * @param cpg the ConstantPoolGen for the method
81      */

82     protected static boolean isLongOrDouble(FieldInstruction fieldIns, ConstantPoolGen cpg) {
83         Type type = fieldIns.getFieldType(cpg);
84         int code = type.getType();
85         return code == T_LONG || code == T_DOUBLE;
86     }
87
88     /**
89      * Get a Variable representing the stack value which will either be stored
90      * into or loaded from a field.
91      *
92      * @param fieldIns the FieldInstruction accessing the field
93      * @param cpg the ConstantPoolGen for the method
94      * @param frame the ValueNumberFrame containing the value to be stored
95      * or the value loaded
96      */

97     protected static Variable snarfFieldValue(FieldInstruction fieldIns, ConstantPoolGen cpg, ValueNumberFrame frame)
98             throws DataflowAnalysisException {
99
100         if (isLongOrDouble(fieldIns, cpg)) {
101             int numSlots = frame.getNumSlots();
102             ValueNumber topValue = frame.getValue(numSlots - 1);
103             ValueNumber nextValue = frame.getValue(numSlots - 2);
104             return new LongOrDoubleLocalVariable(topValue, nextValue);
105         } else {
106             return new LocalVariable(frame.getTopValue());
107         }
108
109     }
110 }
111
112 // vim:ts=4
113
Popular Tags