KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > vna > LoadedFieldSet


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2004,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
20 package edu.umd.cs.findbugs.ba.vna;
21
22 import java.util.BitSet JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.bcel.generic.InstructionHandle;
27 import org.apache.bcel.generic.MethodGen;
28
29 import edu.umd.cs.findbugs.ba.XField;
30
31 /**
32  * Object which stores which fields are loaded and stored
33  * by the instructions in a method (including through inner-class
34  * access methods), and also which fields are loaded/stored
35  * by the overall method. The main purpose is for doing
36  * redundant load elimination and forward substitution
37  * more efficiently, but it might be useful in other situations.
38  *
39  * @author David Hovemeyer
40  */

41 public class LoadedFieldSet {
42     /**
43      * Count number of times a field is loaded and/or stored in the method.
44      */

45     public static class LoadStoreCount {
46         int loadCount, storeCount;
47
48         /** Get the number of times the field is loaded. */
49         public int getLoadCount() {
50             return loadCount;
51         }
52
53         /** Get the number of times the field is stored. */
54         public int getStoreCount() {
55             return storeCount;
56         }
57     }
58
59     // Fields
60
//private MethodGen methodGen;
61
private Map JavaDoc<XField, LoadStoreCount> loadStoreCountMap;
62     private Map JavaDoc<InstructionHandle, XField> handleToFieldMap;
63     private BitSet JavaDoc loadHandleSet;
64
65     /**
66      * Constructor.
67      * Constructs an empty object.
68      *
69      * @param methodGen the method being analyzed for loads/stores
70      */

71     public LoadedFieldSet(MethodGen methodGen) {
72         //this.methodGen = methodGen;
73
this.loadStoreCountMap = new HashMap JavaDoc<XField, LoadStoreCount>();
74         this.handleToFieldMap = new HashMap JavaDoc<InstructionHandle, XField>();
75         this.loadHandleSet = new BitSet JavaDoc();
76     }
77
78     /**
79      * Get the number of times given field is loaded and stored
80      * within the method.
81      * @param field the field
82      * @return the load/store count object
83      */

84     public LoadStoreCount getLoadStoreCount(XField field) {
85         LoadStoreCount loadStoreCount = loadStoreCountMap.get(field);
86         if (loadStoreCount == null) {
87             loadStoreCount = new LoadStoreCount();
88             loadStoreCountMap.put(field, loadStoreCount);
89         }
90         return loadStoreCount;
91     }
92
93     /**
94      * Add a load of given field at given instruction.
95      *
96      * @param handle the instruction
97      * @param field the field
98      */

99     public void addLoad(InstructionHandle handle, XField field) {
100         getLoadStoreCount(field).loadCount++;
101         handleToFieldMap.put(handle, field);
102         loadHandleSet.set(handle.getPosition());
103     }
104
105     /**
106      * Add a store of given field at given instruction.
107      *
108      * @param handle the instruction
109      * @param field the field
110      */

111     public void addStore(InstructionHandle handle, XField field) {
112         getLoadStoreCount(field).storeCount++;
113         handleToFieldMap.put(handle, field);
114     }
115
116     /**
117      * Get the field loaded or stored at given instruction, if any.
118      *
119      * @param handle the instruction
120      * @return the field loaded or stored at the instruction, or null
121      * if the instruction is not a load or store
122      */

123     public XField getField(InstructionHandle handle) {
124         return handleToFieldMap.get(handle);
125     }
126
127     /**
128      * Return whether or not the given field is loaded by any
129      * instruction in the method.
130      *
131      * @param field the field
132      * @return true if the field is loaded somewhere in the method,
133      * false if it is never loaded
134      */

135     public boolean isLoaded(XField field) {
136         return getLoadStoreCount(field).loadCount > 0;
137     }
138
139     /**
140      * Return whether or not the given instruction is a load.
141      *
142      * @param handle the instruction
143      * @return true if the instruction is a load, false if not
144      */

145     public boolean instructionIsLoad(InstructionHandle handle) {
146         return loadHandleSet.get(handle.getPosition());
147     }
148 }
149
150 // vim:ts=4
151
Popular Tags