KickJava   Java API By Example, From Geeks To Geeks.

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


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
22
23 /**
24  *a Field (data value) store for array objects
25  */

26 public class ArrayFields extends Fields {
27     
28   private int elementStorageSize;
29   private int length;
30   private String JavaDoc elementType;
31   private boolean isReference;
32
33   public ArrayFields (String JavaDoc type, ClassInfo ci, int storageSize, int length,
34                       boolean isReference) {
35     super(type, ci, storageSize);
36     this.length = length;
37     this.isReference = isReference;
38     elementType = type.substring(1);
39     elementStorageSize = Types.getTypeSize(type);
40     
41     // Ok, this seems to be a design anomaly, but array elements are not fields,
42
// hence the only non-0 element initialization is for reference arrays, and
43
// only with const, default 'null' values. All other inits (even the
44
// static init "a = {...}:") is compiled into explicit NEWARRAY, ASTORE..
45
// sequences, and does not need any special, automatic init code
46
if (isReference) {
47       for (int i=0; i<length; i++) {
48         values[i] = -1;
49       }
50     }
51   }
52
53   public int arrayLength () {
54     return length;
55   }
56   
57   public int getHeapSize () {
58     return Types.getTypeSizeInBytes(elementType) * length;
59   }
60   
61   public FieldInfo getFieldInfo(String JavaDoc clsBase, String JavaDoc fname) {
62     // TODO
63
return null;
64   }
65   
66   public int getNumberOfFields() {
67     // TODO
68
return 0;
69   }
70   
71   public FieldInfo getFieldInfo(int fieldIndex) {
72     // TODO
73
return null;
74   }
75
76   /**
77    * @see gov.nasa.jpf.jvm.iFields#getFieldIndex(String, String)
78    */

79   public int getFieldIndex (String JavaDoc name, String JavaDoc referenceType) {
80     // will this ever happen?
81
throw new NoSuchFieldError JavaDoc("array does not have any fields!" +
82                                getClassInfo().getName() + "." + name);
83   }
84
85   public String JavaDoc getFieldName (int index) {
86     return Integer.toString(index / elementStorageSize);
87   }
88
89   public String JavaDoc getFieldType (String JavaDoc name, String JavaDoc referenceType) {
90     // will this ever happen?
91
throw new NoSuchFieldError JavaDoc("array does not have any fields!" +
92                                getClassInfo().getName() + "." + name);
93   }
94
95   public String JavaDoc getFieldType (int findex) {
96     if (elementType == null) {
97       elementType = getType().substring(1);
98     }
99
100     return elementType;
101   }
102
103   public String JavaDoc getLogChar () {
104     return "*";
105   }
106
107   public void setLongField (String JavaDoc name, String JavaDoc referenceType, long value) {
108     throw new NoSuchFieldError JavaDoc("array does not have any fields!" +
109                                getClassInfo().getName() + "." + name);
110   }
111
112   public long getLongField (String JavaDoc name, String JavaDoc referenceType) {
113     throw new NoSuchFieldError JavaDoc("array does not have any fields!" +
114                                getClassInfo().getName() + "." + name);
115   }
116
117   public boolean isReferenceArray() {
118     return isReference;
119   }
120   
121   public boolean isRef (String JavaDoc name, String JavaDoc referenceType) {
122     throw new NoSuchFieldError JavaDoc("array does not have any fields!" +
123                                getClassInfo().getName() + "." + name);
124   }
125
126   public boolean[] asBooleanArray () {
127     // <2do> we probably should check the type first
128
int length = values.length;
129     boolean[] result = new boolean[length];
130
131     for (int i = 0; i < length; i++) {
132       result[i] = Types.intToBoolean(values[i]);
133     }
134
135     return result;
136   }
137   
138   public byte[] asByteArray () {
139     // <2do> we probably should check the type first
140
int length = values.length;
141     byte[] result = new byte[length];
142
143     for (int i = 0; i < length; i++) {
144       result[i] = (byte) values[i];
145     }
146
147     return result;
148   }
149   
150   public char[] asCharArray () {
151     // <2do> we probably should check the type first
152
int length = values.length;
153     char[] result = new char[length];
154
155     for (int i = 0; i < length; i++) {
156       result[i] = (char)values[i];
157     }
158
159     return result;
160   }
161   
162   public short[] asShortArray () {
163     // <2do> we probably should check the type first
164
int length = values.length;
165     short[] result = new short[length];
166
167     for (int i = 0; i < length; i++) {
168       result[i] = (short) values[i];
169     }
170
171     return result;
172   }
173   
174   public int[] asIntArray () {
175     // <2do> we probably should check the type first
176
int length = values.length;
177     int[] result = new int[length];
178
179     for (int i = 0; i < length; i++) {
180       result[i] = values[i];
181     }
182
183     return result;
184   }
185   
186   public long[] asLongArray () {
187     // <2do> we probably should check the type first
188
int length = values.length;
189     long[] result = new long[length];
190
191     length--;
192     for (int i = 0, j=0; i < length; i++, j+=2) {
193       result[i] = Types.intsToLong(values[j + 1], values[j]);
194     }
195
196     return result;
197   }
198   
199   public float[] asFloatArray () {
200     // <2do> we probably should check the type first
201
int length = values.length;
202     float[] result = new float[length];
203
204     for (int i = 0; i < length; i++) {
205       result[i] = Types.intToFloat(values[i]);
206     }
207
208     return result;
209   }
210   
211   public double[] asDoubleArray () {
212     // <2do> we probably should check the type first
213
int length = values.length;
214     double[] result = new double[length];
215
216     length--;
217     for (int i = 0, j=0; i < length; i++, j+=2) {
218       result[i] = Types.intsToDouble(values[j + 1], values[j]);
219     }
220
221     return result;
222   }
223
224 }
225
Popular Tags