IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Array.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2007-2013 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25
26using IKVM.Runtime;
27
29{
30
31 static class Array
32 {
33
34#if FIRST_PASS
35
36 public static int getLength(object arrayObj)
37 {
38 return 0;
39 }
40
41 public static object get(object arrayObj, int index)
42 {
43 return null;
44 }
45
46 public static bool getBoolean(object arrayObj, int index)
47 {
48 return false;
49 }
50
51 public static byte getByte(object arrayObj, int index)
52 {
53 return 0;
54 }
55
56 public static char getChar(object arrayObj, int index)
57 {
58 return '\u0000';
59 }
60
61 public static short getShort(object arrayObj, int index)
62 {
63 return 0;
64 }
65
66 public static int getInt(object arrayObj, int index)
67 {
68 return 0;
69 }
70
71 public static float getFloat(object arrayObj, int index)
72 {
73 return 0;
74 }
75
76 public static long getLong(object arrayObj, int index)
77 {
78 return 0;
79 }
80
81 public static double getDouble(object arrayObj, int index)
82 {
83 return 0;
84 }
85
86 public static void set(object arrayObj, int index, object value)
87 {
88 }
89
90 public static void setBoolean(object arrayObj, int index, bool value)
91 {
92 }
93
94 public static void setByte(object arrayObj, int index, byte value)
95 {
96 }
97
98 public static void setChar(object arrayObj, int index, char value)
99 {
100 }
101
102 public static void setShort(object arrayObj, int index, short value)
103 {
104 }
105
106 public static void setInt(object arrayObj, int index, int value)
107 {
108 }
109
110 public static void setFloat(object arrayObj, int index, float value)
111 {
112 }
113
114 public static void setLong(object arrayObj, int index, long value)
115 {
116 }
117
118 public static void setDouble(object arrayObj, int index, double value)
119 {
120 }
121
122 public static object newArray(global::java.lang.Class componentType, int length)
123 {
124 return null;
125 }
126
127 public static object multiNewArray(global::java.lang.Class componentType, int[] dimensions)
128 {
129 return null;
130 }
131#else
132 private static global::System.Array CheckArray(object arrayObj)
133 {
134 if (arrayObj == null)
135 {
136 throw new global::java.lang.NullPointerException();
137 }
138 global::System.Array arr = arrayObj as global::System.Array;
139 if (arr != null)
140 {
141 return arr;
142 }
143 throw new global::java.lang.IllegalArgumentException("Argument is not an array");
144 }
145
146 public static int getLength(object arrayObj)
147 {
148 return CheckArray(arrayObj).Length;
149 }
150
151 public static object get(object arrayObj, int index)
152 {
153 global::System.Array arr = CheckArray(arrayObj);
154 if (index < 0 || index >= arr.Length)
155 {
156 throw new global::java.lang.ArrayIndexOutOfBoundsException();
157 }
158 // We need to look at the actual type here, because "is" or "as"
159 // will convert enums to their underlying type and unsigned integral types
160 // to their signed counter parts.
161 Type type = arrayObj.GetType();
162 if (type == typeof(bool[]))
163 {
164 return global::java.lang.Boolean.valueOf(((bool[])arr)[index]);
165 }
166 if (type == typeof(byte[]))
167 {
168 return global::java.lang.Byte.valueOf(((byte[])arr)[index]);
169 }
170 if (type == typeof(short[]))
171 {
172 return global::java.lang.Short.valueOf(((short[])arr)[index]);
173 }
174 if (type == typeof(char[]))
175 {
176 return global::java.lang.Character.valueOf(((char[])arr)[index]);
177 }
178 if (type == typeof(int[]))
179 {
180 return global::java.lang.Integer.valueOf(((int[])arr)[index]);
181 }
182 if (type == typeof(float[]))
183 {
184 return global::java.lang.Float.valueOf(((float[])arr)[index]);
185 }
186 if (type == typeof(long[]))
187 {
188 return global::java.lang.Long.valueOf(((long[])arr)[index]);
189 }
190 if (type == typeof(double[]))
191 {
192 return global::java.lang.Double.valueOf(((double[])arr)[index]);
193 }
194 return arr.GetValue(index);
195 }
196
197 public static bool getBoolean(object arrayObj, int index)
198 {
199 if (arrayObj == null)
200 {
201 throw new global::java.lang.NullPointerException();
202 }
203 bool[] arr = arrayObj as bool[];
204 if (arr != null)
205 {
206 if (index < 0 || index >= arr.Length)
207 {
208 throw new global::java.lang.ArrayIndexOutOfBoundsException();
209 }
210 return arr[index];
211 }
212 throw new global::java.lang.IllegalArgumentException("argument type mismatch");
213 }
214
215 public static byte getByte(object arrayObj, int index)
216 {
217 if (arrayObj == null)
218 {
219 throw new global::java.lang.NullPointerException();
220 }
221 byte[] arr = arrayObj as byte[];
222 if (arr != null)
223 {
224 if (index < 0 || index >= arr.Length)
225 {
226 throw new global::java.lang.ArrayIndexOutOfBoundsException();
227 }
228 return arr[index];
229 }
230 throw new global::java.lang.IllegalArgumentException("argument type mismatch");
231 }
232
233 public static char getChar(object arrayObj, int index)
234 {
235 if (arrayObj == null)
236 {
237 throw new global::java.lang.NullPointerException();
238 }
239 char[] arr = arrayObj as char[];
240 if (arr != null)
241 {
242 if (index < 0 || index >= arr.Length)
243 {
244 throw new global::java.lang.ArrayIndexOutOfBoundsException();
245 }
246 return arr[index];
247 }
248 throw new global::java.lang.IllegalArgumentException("argument type mismatch");
249 }
250
251 public static short getShort(object arrayObj, int index)
252 {
253 if (arrayObj == null)
254 {
255 throw new global::java.lang.NullPointerException();
256 }
257 short[] arr = arrayObj as short[];
258 if (arr != null)
259 {
260 if (index < 0 || index >= arr.Length)
261 {
262 throw new global::java.lang.ArrayIndexOutOfBoundsException();
263 }
264 return arr[index];
265 }
266 return (sbyte)getByte(arrayObj, index);
267 }
268
269 public static int getInt(object arrayObj, int index)
270 {
271 if (arrayObj == null)
272 {
273 throw new global::java.lang.NullPointerException();
274 }
275 int[] arr1 = arrayObj as int[];
276 if (arr1 != null)
277 {
278 if (index < 0 || index >= arr1.Length)
279 {
280 throw new global::java.lang.ArrayIndexOutOfBoundsException();
281 }
282 return arr1[index];
283 }
284 char[] arr2 = arrayObj as char[];
285 if (arr2 != null)
286 {
287 if (index < 0 || index >= arr2.Length)
288 {
289 throw new global::java.lang.ArrayIndexOutOfBoundsException();
290 }
291 return arr2[index];
292 }
293 return getShort(arrayObj, index);
294 }
295
296 public static float getFloat(object arrayObj, int index)
297 {
298 if (arrayObj == null)
299 {
300 throw new global::java.lang.NullPointerException();
301 }
302 float[] arr = arrayObj as float[];
303 if (arr != null)
304 {
305 if (index < 0 || index >= arr.Length)
306 {
307 throw new global::java.lang.ArrayIndexOutOfBoundsException();
308 }
309 return arr[index];
310 }
311 return getLong(arrayObj, index);
312 }
313
314 public static long getLong(object arrayObj, int index)
315 {
316 if (arrayObj == null)
317 {
318 throw new global::java.lang.NullPointerException();
319 }
320 long[] arr = arrayObj as long[];
321 if (arr != null)
322 {
323 if (index < 0 || index >= arr.Length)
324 {
325 throw new global::java.lang.ArrayIndexOutOfBoundsException();
326 }
327 return arr[index];
328 }
329 return getInt(arrayObj, index);
330 }
331
332 public static double getDouble(object arrayObj, int index)
333 {
334 if (arrayObj == null)
335 {
336 throw new global::java.lang.NullPointerException();
337 }
338 double[] arr = arrayObj as double[];
339 if (arr != null)
340 {
341 if (index < 0 || index >= arr.Length)
342 {
343 throw new global::java.lang.ArrayIndexOutOfBoundsException();
344 }
345 return arr[index];
346 }
347 return getFloat(arrayObj, index);
348 }
349
350 public static void set(object arrayObj, int index, object value)
351 {
352 if (arrayObj == null)
353 {
354 throw new global::java.lang.NullPointerException();
355 }
356 Type type = arrayObj.GetType();
357 if (ReflectUtil.IsVector(type) && JVM.Context.ClassLoaderFactory.GetJavaTypeFromType(type.GetElementType()).IsPrimitive)
358 {
359 global::java.lang.Boolean booleanValue = value as global::java.lang.Boolean;
360 if (booleanValue != null)
361 {
362 setBoolean(arrayObj, index, booleanValue.booleanValue());
363 return;
364 }
365 global::java.lang.Byte byteValue = value as global::java.lang.Byte;
366 if (byteValue != null)
367 {
368 setByte(arrayObj, index, byteValue.byteValue());
369 return;
370 }
371 global::java.lang.Character charValue = value as global::java.lang.Character;
372 if (charValue != null)
373 {
374 setChar(arrayObj, index, charValue.charValue());
375 return;
376 }
377 global::java.lang.Short shortValue = value as global::java.lang.Short;
378 if (shortValue != null)
379 {
380 setShort(arrayObj, index, shortValue.shortValue());
381 return;
382 }
383 global::java.lang.Integer intValue = value as global::java.lang.Integer;
384 if (intValue != null)
385 {
386 setInt(arrayObj, index, intValue.intValue());
387 return;
388 }
389 global::java.lang.Float floatValue = value as global::java.lang.Float;
390 if (floatValue != null)
391 {
392 setFloat(arrayObj, index, floatValue.floatValue());
393 return;
394 }
395 global::java.lang.Long longValue = value as global::java.lang.Long;
396 if (longValue != null)
397 {
398 setLong(arrayObj, index, longValue.longValue());
399 return;
400 }
401 global::java.lang.Double doubleValue = value as global::java.lang.Double;
402 if (doubleValue != null)
403 {
404 setDouble(arrayObj, index, doubleValue.doubleValue());
405 return;
406 }
407 }
408 try
409 {
410 CheckArray(arrayObj).SetValue(value, index);
411 }
412 catch (InvalidCastException)
413 {
414 throw new global::java.lang.IllegalArgumentException("argument type mismatch");
415 }
416 catch (IndexOutOfRangeException)
417 {
418 throw new global::java.lang.ArrayIndexOutOfBoundsException();
419 }
420 }
421
422 public static void setBoolean(object arrayObj, int index, bool value)
423 {
424 if (arrayObj == null)
425 {
426 throw new global::java.lang.NullPointerException();
427 }
428 bool[] arr = arrayObj as bool[];
429 if (arr != null)
430 {
431 if (index < 0 || index >= arr.Length)
432 {
433 throw new global::java.lang.ArrayIndexOutOfBoundsException();
434 }
435 arr[index] = value;
436 }
437 else
438 {
439 throw new global::java.lang.IllegalArgumentException("argument type mismatch");
440 }
441 }
442
443 public static void setByte(object arrayObj, int index, byte value)
444 {
445 if (arrayObj == null)
446 {
447 throw new global::java.lang.NullPointerException();
448 }
449 byte[] arr = arrayObj as byte[];
450 if (arr != null)
451 {
452 if (index < 0 || index >= arr.Length)
453 {
454 throw new global::java.lang.ArrayIndexOutOfBoundsException();
455 }
456 arr[index] = value;
457 }
458 else
459 {
460 setShort(arrayObj, index, (sbyte)value);
461 }
462 }
463
464 public static void setChar(object arrayObj, int index, char value)
465 {
466 if (arrayObj == null)
467 {
468 throw new global::java.lang.NullPointerException();
469 }
470 char[] arr = arrayObj as char[];
471 if (arr != null)
472 {
473 if (index < 0 || index >= arr.Length)
474 {
475 throw new global::java.lang.ArrayIndexOutOfBoundsException();
476 }
477 arr[index] = value;
478 }
479 else
480 {
481 setInt(arrayObj, index, value);
482 }
483 }
484
485 public static void setShort(object arrayObj, int index, short value)
486 {
487 if (arrayObj == null)
488 {
489 throw new global::java.lang.NullPointerException();
490 }
491 short[] arr = arrayObj as short[];
492 if (arr != null)
493 {
494 if (index < 0 || index >= arr.Length)
495 {
496 throw new global::java.lang.ArrayIndexOutOfBoundsException();
497 }
498 arr[index] = value;
499 }
500 else
501 {
502 setInt(arrayObj, index, value);
503 }
504 }
505
506 public static void setInt(object arrayObj, int index, int value)
507 {
508 if (arrayObj == null)
509 {
510 throw new global::java.lang.NullPointerException();
511 }
512 int[] arr = arrayObj as int[];
513 if (arr != null)
514 {
515 if (index < 0 || index >= arr.Length)
516 {
517 throw new global::java.lang.ArrayIndexOutOfBoundsException();
518 }
519 arr[index] = value;
520 }
521 else
522 {
523 setLong(arrayObj, index, value);
524 }
525 }
526
527 public static void setFloat(object arrayObj, int index, float value)
528 {
529 if (arrayObj == null)
530 {
531 throw new global::java.lang.NullPointerException();
532 }
533 float[] arr = arrayObj as float[];
534 if (arr != null)
535 {
536 if (index < 0 || index >= arr.Length)
537 {
538 throw new global::java.lang.ArrayIndexOutOfBoundsException();
539 }
540 arr[index] = value;
541 }
542 else
543 {
544 setDouble(arrayObj, index, value);
545 }
546 }
547
548 public static void setLong(object arrayObj, int index, long value)
549 {
550 if (arrayObj == null)
551 {
552 throw new global::java.lang.NullPointerException();
553 }
554 long[] arr = arrayObj as long[];
555 if (arr != null)
556 {
557 if (index < 0 || index >= arr.Length)
558 {
559 throw new global::java.lang.ArrayIndexOutOfBoundsException();
560 }
561 arr[index] = value;
562 }
563 else
564 {
565 setFloat(arrayObj, index, value);
566 }
567 }
568
569 public static void setDouble(object arrayObj, int index, double value)
570 {
571 if (arrayObj == null)
572 {
573 throw new global::java.lang.NullPointerException();
574 }
575 double[] arr = arrayObj as double[];
576 if (arr != null)
577 {
578 if (index < 0 || index >= arr.Length)
579 {
580 throw new global::java.lang.ArrayIndexOutOfBoundsException();
581 }
582 arr[index] = value;
583 }
584 else
585 {
586 throw new global::java.lang.IllegalArgumentException("argument type mismatch");
587 }
588 }
589
590 public static object newArray(global::java.lang.Class componentType, int length)
591 {
592 if (componentType == null)
593 {
594 throw new global::java.lang.NullPointerException();
595 }
596 if (componentType == global::java.lang.Void.TYPE)
597 {
598 throw new global::java.lang.IllegalArgumentException();
599 }
600 if (length < 0)
601 {
602 throw new global::java.lang.NegativeArraySizeException();
603 }
604 try
605 {
606 RuntimeJavaType wrapper = RuntimeJavaType.FromClass(componentType);
607 wrapper.Finish();
608 object obj = global::System.Array.CreateInstance(wrapper.TypeAsArrayType, length);
609 if (wrapper.IsGhost || wrapper.IsGhostArray)
610 {
611 IKVM.Runtime.GhostTag.SetTag(obj, wrapper.MakeArrayType(1));
612 }
613 return obj;
614 }
616 {
617 throw x.ToJava();
618 }
619 catch (NotSupportedException x)
620 {
621 // This happens when you try to create an array from TypedReference, ArgIterator, ByRef,
622 // RuntimeArgumentHandle or an open generic type.
623 throw new global::java.lang.IllegalArgumentException(x.Message);
624 }
625 }
626
627 public static object multiNewArray(global::java.lang.Class componentType, int[] dimensions)
628 {
629 if (componentType == null || dimensions == null)
630 {
631 throw new global::java.lang.NullPointerException();
632 }
633 if (componentType == global::java.lang.Void.TYPE)
634 {
635 throw new global::java.lang.IllegalArgumentException();
636 }
637 if (dimensions.Length == 0 || dimensions.Length > 255)
638 {
639 throw new global::java.lang.IllegalArgumentException();
640 }
641 try
642 {
643 RuntimeJavaType wrapper = RuntimeJavaType.FromClass(componentType).MakeArrayType(dimensions.Length);
644 wrapper.Finish();
645 object obj = IKVM.Runtime.ByteCodeHelper.multianewarray(wrapper.TypeAsArrayType.TypeHandle, dimensions);
646 if (wrapper.IsGhostArray)
647 {
648 IKVM.Runtime.GhostTag.SetTag(obj, wrapper);
649 }
650 return obj;
651 }
653 {
654 throw x.ToJava();
655 }
656 catch (NotSupportedException x)
657 {
658 // This happens when you try to create an array from TypedReference, ArgIterator, ByRef,
659 // RuntimeArgumentHandle or an open generic type.
660 throw new global::java.lang.IllegalArgumentException(x.Message);
661 }
662 }
663
664#endif // FIRST_PASS
665
666 }
667
668}
IKVM.Reflection.Type Type
static char getChar(object arrayObj, int index)
Definition Array.cs:233
static long getLong(object arrayObj, int index)
Definition Array.cs:314
static object multiNewArray(global::java.lang.Class componentType, int[] dimensions)
Definition Array.cs:627
static void setInt(object arrayObj, int index, int value)
Definition Array.cs:506
static void setLong(object arrayObj, int index, long value)
Definition Array.cs:548
static int getInt(object arrayObj, int index)
Definition Array.cs:269
static short getShort(object arrayObj, int index)
Definition Array.cs:251
static void setChar(object arrayObj, int index, char value)
Definition Array.cs:464
static bool getBoolean(object arrayObj, int index)
Definition Array.cs:197
static int getLength(object arrayObj)
Definition Array.cs:146
static double getDouble(object arrayObj, int index)
Definition Array.cs:332
static void setShort(object arrayObj, int index, short value)
Definition Array.cs:485
static byte getByte(object arrayObj, int index)
Definition Array.cs:215
static float getFloat(object arrayObj, int index)
Definition Array.cs:296
static void setByte(object arrayObj, int index, byte value)
Definition Array.cs:443
static void setFloat(object arrayObj, int index, float value)
Definition Array.cs:527
static object newArray(global::java.lang.Class componentType, int length)
Definition Array.cs:590
static void setBoolean(object arrayObj, int index, bool value)
Definition Array.cs:422
static void setDouble(object arrayObj, int index, double value)
Definition Array.cs:569
static object multianewarray(RuntimeTypeHandle typeHandle, int[] lengths)
Main state of the running JVM.
.NET exception that corresponds to a Java exception.
RuntimeClassLoaderFactory ClassLoaderFactory
Gets the RuntimeClassLoaderFactory associated with this instance of the runtime.