Adding ParameterizedTypeImpl to getGenericSuperclass method.
[jpf-core.git] / src / main / gov / nasa / jpf / vm / NamedFields.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package gov.nasa.jpf.vm;
20
21 import gov.nasa.jpf.util.HashData;
22 import gov.nasa.jpf.util.IntVector;
23
24 /**
25  * value container for non-array classes
26  */
27 public class NamedFields extends Fields {
28
29   /** this is where we store the instance data. Since field types are
30    * heterogenous, we have to map everything into int
31    */
32   protected int[] values;
33
34   public NamedFields (int dataSize) {
35     values = new int[dataSize];
36   }
37
38   @Override
39   public int[] asFieldSlots() {
40     return values;
41   }
42
43   /**
44    * give an approximation of the heap size in bytes - we assume fields are word
45    * aligned, hence the number of values*4 should be good. Note that this is
46    * overridden by ArrayFields (arrays would be packed)
47    */
48   @Override
49   public int getHeapSize () {
50     return values.length*4;
51   }
52
53   // our low level getters and setters
54   @Override
55   public int getIntValue (int index) {
56     return values[index];
57   }
58
59   public boolean isEqual(Fields o, int off, int len, int otherOff) {
60     if (o instanceof NamedFields) {
61       NamedFields other = (NamedFields) o;
62       int iEnd = off + len;
63       int jEnd = otherOff + len;
64       int[] v = other.values;
65
66       if ((iEnd > values.length) || (jEnd > v.length)) {
67         return false;
68       }
69
70       for (int i = off, j = otherOff; i < iEnd; i++, j++) {
71         if (values[i] != v[j]) {
72           return false;
73         }
74       }
75
76       return true;
77     } else {
78       return false;
79     }
80   }
81
82   // same as above, just here to make intentions clear
83   @Override
84   public int getReferenceValue (int index) {
85     return values[index];
86   }
87
88   @Override
89   public long getLongValue (int index) {
90     return Types.intsToLong(values[index + 1], values[index]);
91   }
92
93   @Override
94   public boolean getBooleanValue (int index) {
95     return Types.intToBoolean(values[index]);
96   }
97
98   @Override
99   public byte getByteValue (int index) {
100     return (byte) values[index];
101   }
102
103   @Override
104   public char getCharValue (int index) {
105     return (char) values[index];
106   }
107
108   @Override
109   public short getShortValue (int index) {
110     return (short) values[index];
111   }
112
113   // <2do> get rid of it!! this is only for internal use, to increase efficiency
114   public int[] getValues() {
115     return values;
116   }
117
118   //--- the field modifier methods (both instance and static)
119
120   @Override
121   public void setReferenceValue (int index, int newValue) {
122     values[index] = newValue;
123   }
124
125   @Override
126   public void setBooleanValue (int index, boolean newValue) {
127     values[index] = newValue ? 1 : 0;
128   }
129
130   @Override
131   public void setByteValue (int index, byte newValue) {
132     values[index] = newValue;
133   }
134
135   @Override
136   public void setCharValue (int index, char newValue) {
137     values[index] = newValue;
138   }
139
140   @Override
141   public void setShortValue (int index, short newValue) {
142     values[index] = newValue;
143   }
144
145   @Override
146   public void setFloatValue (int index, float newValue) {
147     values[index] = Types.floatToInt(newValue);
148   }
149
150   @Override
151   public void setIntValue (int index, int newValue) {
152     values[index] = newValue;
153   }
154
155   @Override
156   public void setLongValue (int index, long newValue) {
157                 values[index++] = Types.hiLong(newValue);
158     values[index] = Types.loLong(newValue);
159   }
160
161   @Override
162   public void setDoubleValue (int index, double newValue) {
163     values[index++] = Types.hiDouble(newValue);
164     values[index] = Types.loDouble(newValue);
165   }
166
167
168   @Override
169   public float getFloatValue (int index) {
170     return Types.intToFloat(values[index]);
171   }
172
173   @Override
174   public double getDoubleValue (int index) {
175     return Types.intsToDouble( values[index+1], values[index]);
176   }
177
178   /**
179    * Creates a clone.
180    */
181   @Override
182   public NamedFields clone () {
183     NamedFields f = (NamedFields) cloneFields();
184     f.values = values.clone();
185     return f;
186   }
187
188   /**
189    * Checks for equality.
190    */
191   @Override
192   public boolean equals (Object o) {
193     if (o instanceof NamedFields) {
194       NamedFields other = (NamedFields) o;
195
196       //--- check values
197       int[] v1 = values;
198       int[] v2 = other.values;
199       int l = v1.length;
200       if (l != v2.length) {
201         return false;
202       }
203       for (int i = 0; i < l; i++) {
204         if (v1[i] != v2[i]) {
205           return false;
206         }
207       }
208       
209       return super.compareAttrs(other);
210
211     } else {
212       return false;
213     }
214   }
215
216   // serialization interface
217   @Override
218   public void appendTo(IntVector v) {
219     v.append(values);
220   }
221
222
223   /**
224    * Adds some data to the computation of an hashcode.
225    */
226   @Override
227   public void hash (HashData hd) {
228     int[] v = values;
229     for (int i=0, l=v.length; i < l; i++) {
230       hd.add(v[i]);
231     }
232   }
233
234   /**
235    * Size of the fields.
236    */
237   public int size () {
238     return values.length;
239   }
240
241   @Override
242   public String toString () {
243     StringBuilder sb = new StringBuilder("NamedFields[");
244
245     sb.append("values=");
246     sb.append('[');
247
248     for (int i = 0; i < values.length; i++) {
249       if (i != 0) {
250         sb.append(',');
251       }
252
253       sb.append(values[i]);
254     }
255
256     sb.append(']');
257     sb.append(',');
258
259     sb.append(']');
260
261     return sb.toString();
262   }
263
264   // <2do> replace with copyTo() !!
265   public int[] getRawValues() {
266     return values;
267   }
268
269   public void copyFrom(Fields other) {
270     System.arraycopy(((NamedFields)other).values, 0, this.values, 0, values.length);
271     super.copyAttrs(other);
272   }
273
274 }