KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > Fields


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm;
20
21 import gov.nasa.jpf.JPFException;
22 import gov.nasa.jpf.util.HashData;
23
24
25 /**
26  * Represents the fields (adat part) of an object or class. Contains the
27  * values of the fields, not their descriptors. Descriptors are represented by
28  * gov.nasa.jpf.jvm.FieldInfo objects, which are stored in the
29  * ClassInfo structure.
30  * @see gov.nasa.jpf.jvm.FieldInfo
31  */

32 public abstract class Fields implements Cloneable JavaDoc {
33   static int FATTR_MASK = 0xffff; // pass all propagated attributes
34

35   /** Type of the object or class */
36   protected final String JavaDoc type;
37
38   /** the class of this object */
39   protected final ClassInfo ci;
40
41   /** this is where we store the instance data */
42   protected int[] values;
43
44   protected Fields (String JavaDoc type, ClassInfo ci, int dataSize) {
45     this.type = type;
46     this.ci = ci;
47     
48     values = new int[dataSize];
49   }
50   
51   /**
52    * give an approximation of the heap size in bytes - we assume fields a word
53    * aligned, hence the number of fields*4 should be good. Note that this is
54    * overridden by ArrayFields (arrays would be packed)
55    */

56   public int getHeapSize () {
57     return values.length * 4;
58   }
59   
60   /**
61    * do we have a reference field with value objRef? This is used by
62    * the reachability analysis
63    */

64   public boolean hasRefField (int objRef) {
65     return ci.hasRefField( objRef, this);
66   }
67   
68   /**
69    * Returns true if the fields belong to an array.
70    */

71   public boolean isArray () {
72     return Types.getBaseType(type) == Types.T_ARRAY;
73   }
74
75   public boolean isReferenceArray () {
76     return false;
77   }
78   
79   /**
80    * Returns a reference to the class information.
81    */

82   public ClassInfo getClassInfo () {
83     return ci;
84   }
85
86   // <2do> pcm - Grr, this is only here to hide the Dynamic/StaticFieldInfo branching
87
// remove once this is gone!
88
public abstract FieldInfo getFieldInfo (String JavaDoc clsBase, String JavaDoc fname);
89   public abstract int getNumberOfFields ();
90   // NOTE - fieldIndex (ClassInfo) != storageOffset (Fields). We *don't pad anymore!
91
public abstract FieldInfo getFieldInfo (int fieldIndex);
92   
93   // our low level getters and setters
94
public int getIntValue (int index) {
95     return values[index];
96   }
97   
98   // same as above, just here to make intentions clear
99
public int getReferenceValue (int index) {
100     return values[index];
101   }
102   
103   public long getLongValue (int index) {
104     return Types.intsToLong(values[index + 1], values[index]);
105   }
106   
107   public boolean getBooleanValue (int index) {
108     return Types.intToBoolean(values[index]);
109   }
110   
111   public byte getByteValue (int index) {
112     return (byte) values[index];
113   }
114   
115   public char getCharValue (int index) {
116     return (char) values[index];
117   }
118   
119   public short getShortValue (int index) {
120     return (short) values[index];
121   }
122        
123   public void setReferenceValue (int index, int newValue) {
124     values[index] = newValue;
125   }
126   
127   public void setIntValue (int index, int newValue) {
128     values[index] = newValue;
129   }
130   
131   public void setLongValue (int index, long newValue) {
132         values[index++] = Types.hiLong(newValue);
133     values[index] = Types.loLong(newValue);
134   }
135   
136   public void setDoubleValue (int index, double newValue) {
137     values[index++] = Types.hiDouble(newValue);
138     values[index] = Types.loDouble(newValue);
139   }
140   
141   public void setFloatValue (int index, float newValue) {
142     values[index] = Types.floatToInt(newValue);
143   }
144   
145   public float getFloatValue (int index) {
146     return Types.intToFloat(values[index]);
147   }
148   
149   public double getDoubleValue (int index) {
150     return Types.intsToDouble( values[index+1], values[index]);
151   }
152   
153   /**
154    * Returns the type of the object or class associated with the fields.
155    */

156   public String JavaDoc getType () {
157     return type;
158   }
159
160   /**
161    * Creates a clone.
162    */

163   public Object JavaDoc clone () {
164     Fields f;
165
166     try {
167       f = (Fields) super.clone();
168       f.values = (int[]) values.clone();
169     } catch (CloneNotSupportedException JavaDoc e) {
170       throw new InternalError JavaDoc(e.getMessage());
171     }
172
173     return f;
174   }
175
176   /**
177    * Checks for equality.
178    */

179   public boolean equals (Object JavaDoc o) {
180     if (o == null) {
181       return false;
182     }
183
184     if (!(o instanceof Fields)) {
185       return false;
186     }
187
188     Fields f = (Fields) o;
189
190     if (!type.equals(f.type)) {
191       return false;
192     }
193
194     if (ci != f.ci) {
195       return false;
196     }
197
198     int[] v1 = values;
199     int[] v2 = f.values;
200     int l = v1.length;
201
202     if (l != v2.length) {
203       return false;
204     }
205
206     for (int i = 0; i < l; i++) {
207       if (v1[i] != v2[i]) {
208         return false;
209       }
210     }
211
212     return true;
213   }
214
215   /**
216    * Adds some data to the computation of an hashcode.
217    */

218   public void hash (HashData hd) {
219     for (int i = 0, s = values.length; i < s; i++) {
220       hd.add(values[i]);
221     }
222   }
223
224   public int arrayLength () {
225     // re-implemented by ArrayFields
226
throw new JPFException ("attempt to get length of non-array: " + ci.getName());
227   }
228   
229   public boolean[] asBooleanArray () {
230     throw new JPFException( "not an array object: " + ci.getName());
231   }
232   public byte[] asByteArray () {
233     throw new JPFException( "not an array object: " + ci.getName());
234   }
235   public char[] asCharArray () {
236     throw new JPFException( "not an array object: " + ci.getName());
237   }
238   public short[] asShortArray () {
239     throw new JPFException( "not an array object: " + ci.getName());
240   }
241   public int[] asIntArray () {
242     throw new JPFException( "not an array object: " + ci.getName());
243   }
244   public long[] asLongArray () {
245     throw new JPFException( "not an array object: " + ci.getName());
246   }
247   public float[] asFloatArray () {
248     throw new JPFException( "not an array object: " + ci.getName());
249   }
250   public double[] asDoubleArray () {
251     throw new JPFException( "not an array object: " + ci.getName());
252   }
253   
254   /**
255    * Computes an hash code.
256    */

257   public int hashCode () {
258     HashData hd = new HashData();
259
260     hash(hd);
261
262     return hd.getValue();
263   }
264
265   /**
266    * Size of the fields.
267    */

268   public int size () {
269     return values.length;
270   }
271   
272   public String JavaDoc toString () {
273     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
274     sb.append("Fields(");
275
276     sb.append("type=");
277     sb.append(type);
278     sb.append(",");
279
280     sb.append("ci=");
281     sb.append(ci.getName());
282     sb.append(",");
283
284     sb.append("values=");
285     sb.append('[');
286
287     for (int i = 0; i < values.length; i++) {
288       if (i != 0) {
289         sb.append(',');
290       }
291
292       sb.append(values[i]);
293     }
294
295     sb.append(']');
296     sb.append(",");
297
298     sb.append(")");
299
300     return sb.toString();
301   }
302
303   protected abstract String JavaDoc getLogChar ();
304 }
305
306
307
308
309
Popular Tags