This repository was archived by the owner on Jul 12, 2023. It is now read-only.
forked from polyglot-compiler/JLang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayReflection.java
615 lines (516 loc) · 20.1 KB
/
ArrayReflection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
import java.lang.reflect.Array;
public class ArrayReflection {
public static void main(String[] args) {
System.out.println("--> int array test begins");
intArrayTest();
System.out.println("<-- int array test ends");
System.out.println("--> boolean array test begins");
booleanArrayTest();
System.out.println("<-- boolean array test ends");
System.out.println("--> byte array test begins");
byteArrayTest();
System.out.println("<-- byte array test ends");
System.out.println("--> char array test begins");
charArrayTest();
System.out.println("<-- char array test ends");
System.out.println("--> double array test begins");
doubleArrayTest();
System.out.println("<-- double array test ends");
System.out.println("--> float array test begins");
floatArrayTest();
System.out.println("<-- float array test ends");
System.out.println("--> long array test begins");
longArrayTest();
System.out.println("<-- long array test ends");
System.out.println("--> short array test begins");
shortArrayTest();
System.out.println("<-- short array test ends");
System.out.println("<-- object array test starts");
objectArrayTest();
System.out.println("<-- object array test ends");
System.out.println("<-- inheritance array test starts");
inheritanceArrayTest();
System.out.println("<-- inheritance array test ends");
}
private static void intArrayTest() {
// TODO: handle array larger than limit
// Object arr = Array.newInstance(int.class, Integer.MAX_VALUE);
Object arr = Array.newInstance(int.class, 4);
System.out.println(arr.toString());
System.out.println(arr.getClass().getName());
System.out.println(arr.getClass().getComponentType());
Array.set(arr, 0, 1);
Array.set(arr, 1, 2019);
Array.set(arr, 2, 100);
System.out.println(Array.get(arr, 0));
System.out.println(Array.getInt(arr, 1));
System.out.println(Array.get(arr, 2));
Array.setInt(arr, 0, 1019);
Array.setInt(arr, 1, 330);
Array.setInt(arr, 2, 9102);
System.out.println(Array.getInt(arr, 0));
System.out.println(Array.get(arr, 1));
System.out.println(Array.getInt(arr, 2));
// default value
System.out.println(Array.get(arr, 3));
System.out.println(Array.getInt(arr, 3));
// implicit conversion
System.out.println(Array.getDouble(arr, 1));
System.out.println(Array.getFloat(arr, 1));
try {
Array.set(arr, 1, false);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.getBoolean(arr, 2);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.set(arr, 5, 3);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.toString());
}
try {
Array.set(arr, 5, false);
} catch (Exception e) {
// expected exception: ArrayIndexOutOfBoundsException
System.out.println(e.toString());
}
// test Integer array vs int array
Object integerArr = Array.newInstance(Integer.class, 3);
System.out.println(integerArr.toString());
System.out.println(integerArr.getClass().getName());
System.out.println(integerArr.getClass().getComponentType());
Array.set(integerArr, 0, 3);
Array.set(integerArr, 1, Integer.MAX_VALUE);
try {
Array.setInt(integerArr, 2, 1000);
} catch (IllegalArgumentException e) {
// expected exception: argument is not an array
System.out.println(e.toString());
}
try {
System.out.println(Array.getInt(integerArr, 0));
} catch (IllegalArgumentException e) {
// expected exception: argument is not an array
System.out.println(e.toString());
}
System.out.println(Array.get(integerArr, 1));
System.out.println(Array.get(integerArr, 2));
try {
Array.setInt(integerArr, 1, Integer.MAX_VALUE);
} catch (IllegalArgumentException e) {
// expected exception: argument is not an array
System.out.println(e.toString());
}
}
private static void booleanArrayTest() {
System.out.println("boolean array test begins");
Object arr = Array.newInstance(boolean.class, 2);
System.out.println(arr.toString());
System.out.println(arr.getClass().getName());
System.out.println(arr.getClass().getComponentType());
System.out.println(Array.getLength(arr));
Array.set(arr, 0, false);
Array.set(arr, 1, true);
System.out.println(Array.get(arr, 0));
System.out.println(Array.getBoolean(arr, 1));
Array.setBoolean(arr, 0, true);
Array.setBoolean(arr, 1, false);
System.out.println(Array.getBoolean(arr, 0));
System.out.println(Array.get(arr, 1));
try {
Array.set(arr, 1, 1);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.getInt(arr, 0);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.set(arr, 5, false);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.toString());
}
try {
Array.set(arr, 5, 3);
} catch (Exception e) {
// expected exception: ArrayIndexOutOfBoundsException
System.out.println(e.toString());
}
}
private static void byteArrayTest() {
Object arr = Array.newInstance(byte.class, 2);
System.out.println(arr.toString());
System.out.println(arr.getClass().getName());
System.out.println(arr.getClass().getComponentType());
System.out.println(Array.getLength(arr));
Array.set(arr, 0, (byte)5);
Array.set(arr, 1, (byte)65);
System.out.println(Array.get(arr, 0));
System.out.println(Array.getByte(arr, 1));
Array.setByte(arr, 0, (byte)0x12);
Array.setByte(arr, 1, (byte)0x122);
System.out.println(Array.getByte(arr, 0));
System.out.println(Array.get(arr, 1));
// implicit conversion
System.out.println(Array.getInt(arr, 0));
System.out.println(Array.getDouble(arr, 1));
try {
Array.set(arr, 1, 1);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.getChar(arr, 0);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.set(arr, 5, (byte)2);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.toString());
}
try {
Array.set(arr, 5, 3);
} catch (Exception e) {
// expected exception: ArrayIndexOutOfBoundsException
System.out.println(e.toString());
}
}
private static void charArrayTest() {
Object arr = Array.newInstance(char.class, 2);
System.out.println(arr.toString());
System.out.println(arr.getClass().getName());
System.out.println(arr.getClass().getComponentType());
System.out.println(Array.getLength(arr));
Array.set(arr, 0, '1');
Array.set(arr, 1, (char)65);
System.out.println(Array.get(arr, 0));
System.out.println(Array.getChar(arr, 1));
Array.setChar(arr, 0, (char)90);
Array.setChar(arr, 1, '%');
System.out.println(Array.getChar(arr, 0));
System.out.println(Array.get(arr, 1));
// implicit conversion
System.out.println(Array.getInt(arr, 0));
System.out.println(Array.getDouble(arr, 1));
try {
Array.set(arr, 1, 1);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.getBoolean(arr, 0);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.set(arr, 5, 'a');
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.toString());
}
try {
Array.set(arr, 5, 3);
} catch (Exception e) {
// expected exception: ArrayIndexOutOfBoundsException
System.out.println(e.toString());
}
}
private static void doubleArrayTest() {
Object arr = Array.newInstance(double.class, 2);
System.out.println(arr.toString());
System.out.println(arr.getClass().getName());
System.out.println(arr.getClass().getComponentType());
System.out.println(Array.getLength(arr));
Array.set(arr, 0, 0.32);
Array.set(arr, 1, 7.54121);
System.out.println(Array.get(arr, 0));
System.out.println(Array.getDouble(arr, 1));
Array.setDouble(arr, 0, 43.1234);
Array.setDouble(arr, 1, 123.123456);
System.out.println(Array.getDouble(arr, 0));
System.out.println(Array.get(arr, 1));
// implicit conversion
Array.setInt(arr, 0, 3);
Array.setFloat(arr, 1, 34f);
try {
Array.set(arr, 1, false);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.getInt(arr, 0);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.set(arr, 5, 12.12);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.toString());
}
try {
Array.set(arr, 5, 3);
} catch (Exception e) {
// expected exception: ArrayIndexOutOfBoundsException
System.out.println(e.toString());
}
}
private static void floatArrayTest() {
Object arr = Array.newInstance(float.class, 2);
System.out.println(arr.toString());
System.out.println(arr.getClass().getName());
System.out.println(arr.getClass().getComponentType());
System.out.println(Array.getLength(arr));
Array.set(arr, 0, 1.1f);
Array.set(arr, 1, (float) 2.23);
System.out.println(Array.get(arr, 0));
System.out.println(Array.getFloat(arr, 1));
Array.setFloat(arr, 0, 0.43f);
Array.setFloat(arr, 1, (float) 1);
System.out.println(Array.getFloat(arr, 0));
System.out.println(Array.get(arr, 1));
// implicit conversion
Array.setChar(arr, 0, 'a');
System.out.println(Array.getFloat(arr, 0));
System.out.println(Array.getDouble(arr, 1));
try {
Array.set(arr, 1, false);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.getBoolean(arr, 0);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.set(arr, 5, 1.2f);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.toString());
}
try {
Array.set(arr, 5, 3);
} catch (Exception e) {
// expected exception: ArrayIndexOutOfBoundsException
System.out.println(e.toString());
}
}
private static void longArrayTest() {
Object arr = Array.newInstance(long.class, 2);
System.out.println(arr.toString());
System.out.println(arr.getClass().getName());
System.out.println(arr.getClass().getComponentType());
System.out.println(Array.getLength(arr));
Array.set(arr, 0, 1L);
Array.set(arr, 1, 2L);
System.out.println(Array.get(arr, 0));
System.out.println(Array.getLong(arr, 1));
Array.setLong(arr, 0, Long.MAX_VALUE);
Array.setLong(arr, 1, Long.MIN_VALUE);
System.out.println(Array.getLong(arr, 0));
System.out.println(Array.get(arr, 1));
// implicit conversion
Array.setInt(arr, 0, 1);
Array.setChar(arr, 1, 'a');
System.out.println(Array.getLong(arr, 0));
System.out.println(Array.get(arr, 1));
try {
Array.set(arr, 1, false);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.getInt(arr, 0);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.set(arr, 5, false);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.toString());
}
try {
Array.set(arr, 5, 3.0f);
} catch (Exception e) {
// expected exception: ArrayIndexOutOfBoundsException
System.out.println(e.toString());
}
}
private static void shortArrayTest() {
Object arr = Array.newInstance(short.class, 2);
System.out.println(arr.toString());
System.out.println(arr.getClass().getName());
System.out.println(arr.getClass().getComponentType());
System.out.println(Array.getLength(arr));
Array.set(arr, 0, (short) 2);
Array.set(arr, 1, (short) 5);
System.out.println(Array.get(arr, 0));
System.out.println(Array.getShort(arr, 1));
Array.setShort(arr, 0, (short) 10);
Array.setShort(arr, 1, Short.MIN_VALUE);
System.out.println(Array.getShort(arr, 0));
System.out.println(Array.get(arr, 1));
// implicit conversion
Array.setByte(arr, 0, (byte) 5);
System.out.println(Array.getInt(arr, 0));
System.out.println(Array.getDouble(arr, 0));
try {
Array.setChar(arr, 0, 'a');
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.getByte(arr, 0);
} catch (IllegalArgumentException e) {
// expected exception: argument type mismatch
System.out.println(e.toString());
}
try {
Array.set(arr, 5, false);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.toString());
}
try {
Array.set(arr, 5, 3);
} catch (Exception e) {
// expected exception: ArrayIndexOutOfBoundsException
System.out.println(e.toString());
}
}
private static class TestClass {
int a, b;
TestClass c;
TestClass() {
this.a = -1;
this.b = -2;
}
TestClass(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public String toString() {
return super.toString() + "a: " + a + "b: " + b;
}
}
private static void objectArrayTest() {
Object arr = Array.newInstance(TestClass.class, 3);
System.out.println(arr.toString());
System.out.println(arr.getClass().getName());
System.out.println(arr.getClass().getComponentType());
System.out.println(Array.getLength(arr));
Array.set(arr, 0, new TestClass());
Array.set(arr, 1, new TestClass(123, 456));
System.out.println(Array.get(arr, 0));
System.out.println(Array.get(arr, 1));
System.out.println(Array.get(arr, 2));
Array.set(arr, 0, null);
System.out.println(Array.get(arr, 0));
try {
Array.setInt(arr, 0, 0);
} catch (IllegalArgumentException e) {
// expected exception: Argument is not an array
System.out.println(e.toString());
}
try {
Array.set(arr, 0, new Object());
} catch (IllegalArgumentException e) {
// expected exception: array element type mismatch
System.out.println(e.toString());
}
}
private static class TestBaseClass {
protected int a, b;
TestBaseClass(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public String toString() {
return super.toString() + "a: " + a + "b: " + b;
}
}
private static class TestDerivedClass extends TestBaseClass {
int c;
TestDerivedClass(int a, int b, int c) {
super(a, b);
this.c = c;
}
@Override
public String toString() {
return super.toString() + "c: " + c;
}
}
private static void inheritanceArrayTest() {
Object arrBase = Array.newInstance(TestBaseClass.class, 2);
System.out.println(arrBase.toString());
System.out.println(arrBase.getClass().getName());
System.out.println(arrBase.getClass().getComponentType());
System.out.println(Array.getLength(arrBase));
Object arrDerived = Array.newInstance(TestDerivedClass.class, 2);
System.out.println(arrDerived.toString());
System.out.println(arrDerived.getClass().getName());
System.out.println(arrDerived.getClass().getComponentType());
System.out.println(Array.getLength(arrDerived));
Array.set(arrBase, 0, new TestBaseClass(1, 5));
Array.set(arrBase, 1, new TestDerivedClass(7, 9, 110));
System.out.println(Array.get(arrBase, 0));
System.out.println(Array.get(arrBase, 1));
System.out.println((TestBaseClass[])arrBase);
try {
System.out.println((TestDerivedClass[])arrBase);
} catch (ClassCastException e) {
// expected exception: [LArrayReflection$TestBaseClass;
// cannot be cast to [LArrayReflection$TestDerivedClass;
System.out.println(e.toString());
}
TestBaseClass[] castArray = (TestBaseClass[])arrBase;
castArray[0] = new TestBaseClass(12, 3);
castArray[1] = new TestDerivedClass(1, 59, -2);
System.out.println(castArray[0]);
System.out.println(castArray[1]);
try {
Array.set(arrDerived, 0, new TestBaseClass(342, 351));
} catch (IllegalArgumentException e) {
// expected exception: array element type mismatch
System.out.println(e.toString());
}
Array.set(arrDerived, 1, new TestDerivedClass(109, 2018, 2017));
System.out.println(Array.get(arrDerived, 0));
System.out.println(Array.get(arrDerived, 1));
System.out.println((TestDerivedClass[])arrDerived);
System.out.println((TestBaseClass[])arrDerived);
castArray = (TestBaseClass[])arrDerived;
System.out.println(castArray[0]);
System.out.println(castArray[1]);
try {
castArray[0] = new TestBaseClass(1, 2);
} catch (ArrayStoreException e) {
// expected exception: java.lang.ArrayStoreException: ArrayReflection$TestBaseClass
System.out.println(e.toString());
}
castArray[1] = new TestDerivedClass(123, 34, 123);
System.out.println(castArray[0]);
System.out.println(castArray[1]);
}
}