FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / lang / reflect / natArray.cc
blob78751229eaba658af31cebc5ac75537c916e2890
1 // natField.cc - Implementation of java.lang.reflect.Field native methods.
3 /* Copyright (C) 1999, 2000, 2001 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
13 #include <stdlib.h>
15 #include <jvm.h>
16 #include <gcj/cni.h>
17 #include <java/lang/reflect/Array.h>
18 #include <java/lang/IllegalArgumentException.h>
19 #include <java/lang/Byte.h>
20 #include <java/lang/Short.h>
21 #include <java/lang/Integer.h>
22 #include <java/lang/Long.h>
23 #include <java/lang/Float.h>
24 #include <java/lang/Double.h>
25 #include <java/lang/Boolean.h>
26 #include <java/lang/Character.h>
28 jobject
29 java::lang::reflect::Array::newInstance (jclass componentType, jint length)
31 if (componentType->isPrimitive())
33 // We could check for this in _Jv_NewPrimArray, but that seems
34 // like needless overhead when the only real route to this
35 // problem is here.
36 if (componentType == JvPrimClass (void))
37 throw new java::lang::IllegalArgumentException ();
38 return _Jv_NewPrimArray (componentType, length);
40 else
41 return JvNewObjectArray (length, componentType, NULL);
45 jobject
46 java::lang::reflect::Array::newInstance (jclass componentType,
47 jintArray dimensions)
49 jint ndims = dimensions->length;
50 if (ndims == 0)
51 throw new java::lang::IllegalArgumentException ();
52 jint* dims = elements (dimensions);
53 if (ndims == 1)
54 return newInstance (componentType, dims[0]);
55 jclass arrayType = componentType;
56 for (int i = 0; i < ndims; i++) // FIXME 2nd arg should
57 // be "current" loader
58 arrayType = _Jv_GetArrayClass (arrayType, 0);
60 return _Jv_NewMultiArray (arrayType, ndims, dims);
63 jint
64 java::lang::reflect::Array::getLength (jobject array)
66 jclass arrayType = array->getClass();
67 if (! arrayType->isArray ())
68 throw new java::lang::IllegalArgumentException;
69 return ((__JArray*) array)->length;
72 jclass
73 java::lang::reflect::Array::getElementType (jobject array, jint index)
75 jclass arrayType = array->getClass();
76 if (! arrayType->isArray ())
77 throw new java::lang::IllegalArgumentException;
78 jint length = ((__JArray*) array)->length;
79 if ((_Jv_uint) index >= (_Jv_uint) length)
80 _Jv_ThrowBadArrayIndex(index);
81 return arrayType->getComponentType ();
84 jboolean
85 java::lang::reflect::Array::getBoolean (jobject array, jint index)
87 jclass elementType = getElementType (array, index);
88 if (elementType == JvPrimClass (boolean))
89 return elements ((jbooleanArray) array) [index];
90 throw new java::lang::IllegalArgumentException;
93 jchar
94 java::lang::reflect::Array::getChar (jobject array, jint index)
96 jclass elementType = getElementType (array, index);
97 if (elementType == JvPrimClass (char))
98 return elements ((jcharArray) array) [index];
99 throw new java::lang::IllegalArgumentException;
102 jbyte
103 java::lang::reflect::Array::getByte (jobject array, jint index)
105 jclass elementType = getElementType (array, index);
106 if (elementType == JvPrimClass (byte))
107 return elements ((jbyteArray) array) [index];
108 throw new java::lang::IllegalArgumentException;
111 jshort
112 java::lang::reflect::Array::getShort (jobject array, jint index)
114 jclass elementType = getElementType (array, index);
115 if (elementType == JvPrimClass (short))
116 return elements ((jshortArray) array) [index];
117 if (elementType == JvPrimClass (byte))
118 return elements ((jbyteArray) array) [index];
119 throw new java::lang::IllegalArgumentException;
122 jint
123 java::lang::reflect::Array::getInt (jobject array, jint index)
125 jclass elementType = getElementType (array, index);
126 if (elementType == JvPrimClass (int))
127 return elements ((jintArray) array) [index];
128 if (elementType == JvPrimClass (short))
129 return elements ((jshortArray) array) [index];
130 if (elementType == JvPrimClass (byte))
131 return elements ((jbyteArray) array) [index];
132 if (elementType == JvPrimClass (char))
133 return elements ((jcharArray) array) [index];
134 throw new java::lang::IllegalArgumentException;
137 jlong
138 java::lang::reflect::Array::getLong (jobject array, jint index)
140 jclass elementType = getElementType (array, index);
141 if (elementType == JvPrimClass (long))
142 return elements ((jlongArray) array) [index];
143 if (elementType == JvPrimClass (int))
144 return elements ((jintArray) array) [index];
145 if (elementType == JvPrimClass (short))
146 return elements ((jshortArray) array) [index];
147 if (elementType == JvPrimClass (byte))
148 return elements ((jbyteArray) array) [index];
149 if (elementType == JvPrimClass (char))
150 return elements ((jcharArray) array) [index];
151 throw new java::lang::IllegalArgumentException;
154 jfloat
155 java::lang::reflect::Array::getFloat (jobject array, jint index)
157 jclass elementType = getElementType (array, index);
158 if (elementType == JvPrimClass (float))
159 return elements ((jfloatArray) array) [index];
160 if (elementType == JvPrimClass (long))
161 return elements ((jlongArray) array) [index];
162 if (elementType == JvPrimClass (int))
163 return elements ((jintArray) array) [index];
164 if (elementType == JvPrimClass (short))
165 return elements ((jshortArray) array) [index];
166 if (elementType == JvPrimClass (byte))
167 return elements ((jbyteArray) array) [index];
168 if (elementType == JvPrimClass (char))
169 return elements ((jcharArray) array) [index];
170 throw new java::lang::IllegalArgumentException;
173 jdouble
174 java::lang::reflect::Array::getDouble (jobject array, jint index)
176 jclass elementType = getElementType (array, index);
177 if (elementType == JvPrimClass (double))
178 return elements ((jdoubleArray) array) [index];
179 if (elementType == JvPrimClass (float))
180 return elements ((jfloatArray) array) [index];
181 if (elementType == JvPrimClass (long))
182 return elements ((jlongArray) array) [index];
183 if (elementType == JvPrimClass (int))
184 return elements ((jintArray) array) [index];
185 if (elementType == JvPrimClass (short))
186 return elements ((jshortArray) array) [index];
187 if (elementType == JvPrimClass (byte))
188 return elements ((jbyteArray) array) [index];
189 if (elementType == JvPrimClass (char))
190 return elements ((jcharArray) array) [index];
191 throw new java::lang::IllegalArgumentException;
194 jobject
195 java::lang::reflect::Array::get (jobject array, jint index)
197 jclass elementType = getElementType (array, index);
198 if (! elementType->isPrimitive ())
199 return elements ((jobjectArray) array) [index];
200 if (elementType == JvPrimClass (double))
201 return new java::lang::Double (elements ((jdoubleArray) array) [index]);
202 if (elementType == JvPrimClass (float))
203 return new java::lang::Float (elements ((jfloatArray) array) [index]);
204 if (elementType == JvPrimClass (long))
205 return new java::lang::Long (elements ((jlongArray) array) [index]);
206 if (elementType == JvPrimClass (int))
207 return new java::lang::Integer (elements ((jintArray) array) [index]);
208 if (elementType == JvPrimClass (short))
209 return new java::lang::Short (elements ((jshortArray) array) [index]);
210 if (elementType == JvPrimClass (byte))
211 return new java::lang::Byte (elements ((jbyteArray) array) [index]);
212 if (elementType == JvPrimClass (char))
213 return new java::lang::Character (elements ((jcharArray) array) [index]);
214 if (elementType == JvPrimClass (boolean))
216 _Jv_InitClass (&java::lang::Boolean::class$);
217 if (elements ((jbooleanArray) array) [index])
218 return java::lang::Boolean::TRUE;
219 else
220 return java::lang::Boolean::FALSE;
222 throw new java::lang::IllegalArgumentException;
225 void
226 java::lang::reflect::Array::setChar (jobject array, jint index, jchar value)
228 jclass elementType = getElementType (array, index);
229 if (elementType == JvPrimClass (char))
230 elements ((jcharArray) array) [index] = value;
231 else if (elementType == JvPrimClass (int))
232 elements ((jintArray) array) [index] = value;
233 else if (elementType == JvPrimClass (long))
234 elements ((jlongArray) array) [index] = value;
235 else if (elementType == JvPrimClass (float))
236 elements ((jfloatArray) array) [index] = value;
237 else if (elementType == JvPrimClass (double))
238 elements ((jdoubleArray) array) [index] = value;
239 else
240 throw new java::lang::IllegalArgumentException;
243 void
244 java::lang::reflect::Array::setByte (jobject array, jint index, jbyte value)
246 jclass elementType = getElementType (array, index);
247 if (elementType == JvPrimClass (byte))
248 elements ((jbyteArray) array) [index] = value;
249 else if (elementType == JvPrimClass (short))
250 elements ((jshortArray) array) [index] = value;
251 else if (elementType == JvPrimClass (int))
252 elements ((jintArray) array) [index] = value;
253 else if (elementType == JvPrimClass (long))
254 elements ((jlongArray) array) [index] = value;
255 else if (elementType == JvPrimClass (float))
256 elements ((jfloatArray) array) [index] = value;
257 else if (elementType == JvPrimClass (double))
258 elements ((jdoubleArray) array) [index] = value;
259 else
260 throw new java::lang::IllegalArgumentException;
263 void
264 java::lang::reflect::Array::setShort (jobject array, jint index, jshort value)
266 jclass elementType = getElementType (array, index);
267 if (elementType == JvPrimClass (short))
268 elements ((jshortArray) array) [index] = value;
269 else if (elementType == JvPrimClass (int))
270 elements ((jintArray) array) [index] = value;
271 else if (elementType == JvPrimClass (long))
272 elements ((jlongArray) array) [index] = value;
273 else if (elementType == JvPrimClass (float))
274 elements ((jfloatArray) array) [index] = value;
275 else if (elementType == JvPrimClass (double))
276 elements ((jdoubleArray) array) [index] = value;
277 else
278 throw new java::lang::IllegalArgumentException;
281 void
282 java::lang::reflect::Array::setInt (jobject array, jint index, jint value)
284 jclass elementType = getElementType (array, index);
285 if (elementType == JvPrimClass (int))
286 elements ((jintArray) array) [index] = value;
287 else if (elementType == JvPrimClass (long))
288 elements ((jlongArray) array) [index] = value;
289 else if (elementType == JvPrimClass (float))
290 elements ((jfloatArray) array) [index] = value;
291 else if (elementType == JvPrimClass (double))
292 elements ((jdoubleArray) array) [index] = value;
293 else
294 throw new java::lang::IllegalArgumentException;
297 void
298 java::lang::reflect::Array::setLong (jobject array, jint index, jlong value)
300 jclass elementType = getElementType (array, index);
301 if (elementType == JvPrimClass (long))
302 elements ((jlongArray) array) [index] = value;
303 else if (elementType == JvPrimClass (float))
304 elements ((jfloatArray) array) [index] = value;
305 else if (elementType == JvPrimClass (double))
306 elements ((jdoubleArray) array) [index] = value;
307 else
308 throw new java::lang::IllegalArgumentException;
311 void
312 java::lang::reflect::Array::setFloat (jobject array, jint index, jfloat value)
314 jclass elementType = getElementType (array, index);
315 if (elementType == JvPrimClass (float))
316 elements ((jfloatArray) array) [index] = value;
317 else if (elementType == JvPrimClass (double))
318 elements ((jdoubleArray) array) [index] = value;
319 else
320 throw new java::lang::IllegalArgumentException;
323 void
324 java::lang::reflect::Array::setDouble (jobject array, jint index, jdouble value)
326 jclass elementType = getElementType (array, index);
327 if (elementType == JvPrimClass (double))
328 elements ((jdoubleArray) array) [index] = value;
329 else
330 throw new java::lang::IllegalArgumentException;
333 void
334 java::lang::reflect::Array::setBoolean (jobject array,
335 jint index, jboolean value)
337 jclass elementType = getElementType (array, index);
338 if (elementType == JvPrimClass (boolean))
339 elements ((jbooleanArray) array) [index] = value;
340 else
341 throw new java::lang::IllegalArgumentException;
344 void
345 java::lang::reflect::Array::set (jobject array, jint index,
346 jobject value, jclass elType)
348 if (! _Jv_IsInstanceOf (value, elType))
349 throw new java::lang::IllegalArgumentException;
350 elements ((jobjectArray) array) [index] = value;