2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
18 package gov.nasa.jpf.vm;
20 import gov.nasa.jpf.annotation.MJI;
21 import gov.nasa.jpf.vm.ClassInfo;
22 import gov.nasa.jpf.vm.MJIEnv;
23 import gov.nasa.jpf.vm.NativePeer;
24 import gov.nasa.jpf.vm.Types;
27 * MJI NativePeer class for java.lang.reflect.Array library abstraction
29 public class JPF_java_lang_reflect_Array extends NativePeer {
31 public int getLength__Ljava_lang_Object_2__I (MJIEnv env, int clsObjRef,
33 if (objRef == MJIEnv.NULL) {
34 env.throwException("java.lang.NullPointerException", "array argument is null");
37 if (!env.isArray(objRef)) {
38 env.throwException("java.lang.IllegalArgumentException", "argument is not an array");
42 return env.getArrayLength(objRef);
46 public int newArray__Ljava_lang_Class_2I__Ljava_lang_Object_2 (MJIEnv env, int clsRef,
47 int componentTypeRef, int length) {
48 ClassInfo ci = env.getReferredClassInfo(componentTypeRef);
49 String clsName = ci.getName();
51 return createNewArray( env, clsName, length);
54 static int createNewArray (MJIEnv env, String clsName, int length) {
55 int aRef = MJIEnv.NULL;
57 if ("boolean".equals(clsName)) { aRef = env.newBooleanArray(length); }
58 else if ("byte".equals(clsName)) { aRef = env.newByteArray(length); }
59 else if ("char".equals(clsName)) { aRef = env.newCharArray(length); }
60 else if ("short".equals(clsName)) { aRef = env.newShortArray(length); }
61 else if ("int".equals(clsName)) { aRef = env.newIntArray(length); }
62 else if ("long".equals(clsName)) { aRef = env.newLongArray(length); }
63 else if ("float".equals(clsName)) { aRef = env.newFloatArray(length); }
64 else if ("double".equals(clsName)) { aRef = env.newDoubleArray(length); }
65 else { aRef = env.newObjectArray(clsName, length); }
70 public int multiNewArray__Ljava_lang_Class_2_3I__Ljava_lang_Object_2 (MJIEnv env, int clsRef,
73 ClassInfo ci = env.getReferredClassInfo(componentTypeRef);
74 String clsName = ci.getName();
75 int n = env.getArrayLength(dimArrayRef);
78 clsName = Types.getTypeSignature(clsName, true);
80 String arrayType = "[";
81 for (i=2; i<n; i++) arrayType += '[';
84 int[] dim = new int[n];
86 dim[i] = env.getIntArrayElement(dimArrayRef, i);
89 int aRef = createNewMultiArray(env, arrayType, dim, 0);
93 static int createNewMultiArray (MJIEnv env, String arrayType, int[] dim, int level) {
94 int aRef = MJIEnv.NULL;
97 if (level < dim.length-1) {
98 aRef = env.newObjectArray(arrayType, len);
100 for (int i=0; i<len; i++) {
101 int eRef = createNewMultiArray(env, arrayType.substring(1), dim, level+1);
102 env.setReferenceArrayElement(aRef, i, eRef);
105 aRef = createNewArray( env, arrayType, len);
112 public int get__Ljava_lang_Object_2I__Ljava_lang_Object_2 (MJIEnv env, int clsRef,
113 int aref, int index){
114 String at = Types.getTypeName(env.getArrayType(aref));
115 if (at.equals("int")){
116 int vref = env.newObject("java.lang.Integer");
117 env.setIntField(vref, "value", env.getIntArrayElement(aref,index));
120 } else if (at.equals("long")){
121 int vref = env.newObject("java.lang.Long");
122 env.setLongField(vref, "value", env.getLongArrayElement(aref,index));
125 } else if (at.equals("double")){
126 int vref = env.newObject("java.lang.Double");
127 env.setDoubleField(vref, "value", env.getDoubleArrayElement(aref,index));
130 } else if (at.equals("boolean")){
131 int vref = env.newObject("java.lang.Boolean");
132 env.setBooleanField(vref, "value", env.getBooleanArrayElement(aref,index));
135 } else if (at.equals("char")){
136 int vref = env.newObject("java.lang.Character");
137 env.setCharField(vref, "value", env.getCharArrayElement(aref,index));
140 } else if (at.equals("byte")){
141 int vref = env.newObject("java.lang.Byte");
142 env.setByteField(vref, "value", env.getByteArrayElement(aref,index));
145 } else if (at.equals("short")){
146 int vref = env.newObject("java.lang.Short");
147 env.setShortField(vref, "value", env.getShortArrayElement(aref,index));
150 } else if (at.equals("float")){
151 int vref = env.newObject("java.lang.Float");
152 env.setFloatField(vref, "value", env.getFloatArrayElement(aref,index));
156 return env.getReferenceArrayElement(aref, index);
160 private static boolean check (MJIEnv env, int aref, int index) {
161 if (aref == MJIEnv.NULL) {
162 env.throwException("java.lang.NullPointerException", "array argument is null");
165 if (!env.isArray(aref)) {
166 env.throwException("java.lang.IllegalArgumentException", "argument is not an array");
169 if (index < 0 || index >= env.getArrayLength(aref)) {
170 env.throwException("java.lang.IndexOutOfBoundsException", "index " + index + " is out of bounds");
177 public boolean getBoolean__Ljava_lang_Object_2I__Z (MJIEnv env, int clsRef, int aref, int index) {
178 if (check(env, aref, index)) {
179 return env.getBooleanArrayElement(aref, index);
185 public static byte getByte__Ljava_lang_Object_2I__B (MJIEnv env, int clsRef, int aref, int index) {
186 if (check(env, aref, index)) {
187 return env.getByteArrayElement(aref, index);
193 public char getChar__Ljava_lang_Object_2I__C (MJIEnv env, int clsRef, int aref, int index) {
194 if (check(env, aref, index)) {
195 return env.getCharArrayElement(aref, index);
201 public short getShort__Ljava_lang_Object_2I__S (MJIEnv env, int clsRef, int aref, int index) {
202 if (check(env, aref, index)) {
203 return env.getShortArrayElement(aref, index);
209 public int getInt__Ljava_lang_Object_2I__I (MJIEnv env, int clsRef, int aref, int index) {
210 if (check(env, aref, index)) {
211 return env.getIntArrayElement(aref, index);
217 public long getLong__Ljava_lang_Object_2I__J (MJIEnv env, int clsRef, int aref, int index) {
218 if (check(env, aref, index)) {
219 return env.getLongArrayElement(aref, index);
225 public float getFloat__Ljava_lang_Object_2I__F (MJIEnv env, int clsRef, int aref, int index) {
226 if (check(env, aref, index)) {
227 return env.getFloatArrayElement(aref, index);
233 public double getDouble__Ljava_lang_Object_2I__D (MJIEnv env, int clsRef, int aref, int index) {
234 if (check(env, aref, index)) {
235 return env.getDoubleArrayElement(aref, index);
241 public void setBoolean__Ljava_lang_Object_2IZ__V (MJIEnv env, int clsRef, int aref, int index, boolean val) {
242 if (check(env, aref, index)) {
243 env.setBooleanArrayElement(aref, index, val);
248 public void setByte__Ljava_lang_Object_2IB__V (MJIEnv env, int clsRef, int aref, int index, byte val) {
249 if (check(env, aref, index)) {
250 env.setByteArrayElement(aref, index, val);
255 public void setChar__Ljava_lang_Object_2IC__V (MJIEnv env, int clsRef, int aref, int index, char val) {
256 if (check(env, aref, index)) {
257 env.setCharArrayElement(aref, index, val);
262 public void setShort__Ljava_lang_Object_2IS__V (MJIEnv env, int clsRef, int aref, int index, short val) {
263 if (check(env, aref, index)) {
264 env.setShortArrayElement(aref, index, val);
269 public void setInt__Ljava_lang_Object_2II__V (MJIEnv env, int clsRef, int aref, int index, int val) {
270 if (check(env, aref, index)) {
271 env.setIntArrayElement(aref, index, val);
276 public void setLong__Ljava_lang_Object_2IJ__V (MJIEnv env, int clsRef, int aref, int index, long val) {
277 if (check(env, aref, index)) {
278 env.setLongArrayElement(aref, index, val);
283 public void setFloat__Ljava_lang_Object_2IF__V (MJIEnv env, int clsRef, int aref, int index, float val) {
284 if (check(env, aref, index)) {
285 env.setFloatArrayElement(aref, index, val);
290 public void setDouble__Ljava_lang_Object_2ID__V (MJIEnv env, int clsRef, int aref, int index, double val) {
291 if (check(env, aref, index)) {
292 env.setDoubleArrayElement(aref, index, val);