1 /*
2  * Copyright 2006 The Android Open Source Project
3  */
4 
5 import java.lang.reflect.Array;
6 
7 /**
8  * Test java.lang.reflect.Array.
9  */
10 public class Main {
main(String[] args)11     public static void main(String[] args) {
12         testSingleInt();
13         testSingleChar();
14         testSingleShort();
15         testSingleLong();
16         testSingle();
17         testMultiInt();
18         testMulti();
19         testAbstract();
20 
21         System.out.println("ReflectArrayTest passed");
22     }
23 
testSingleInt()24     static void testSingleInt() {
25         Object intArray;
26 
27         intArray = Array.newInstance(Integer.TYPE, 2);
28 
29         int[] array = (int[]) intArray;
30         array[0] = 5;
31         Array.setInt(intArray, 1, 6);
32 
33         if (Array.getInt(intArray, 0) != 5)
34             throw new RuntimeException();
35         if (array[1] != 6)
36             throw new RuntimeException();
37         try {
38             array[2] = 27;
39             throw new RuntimeException("store should have failed");
40         } catch (ArrayIndexOutOfBoundsException abe) { }
41         try {
42             Array.setInt(intArray, 2, 27);
43             throw new RuntimeException("store should have failed");
44         } catch (ArrayIndexOutOfBoundsException abe) { }
45         if (array.length != Array.getLength(intArray) ||
46             array.length != 2)
47         {
48             throw new RuntimeException("bad len");
49         }
50 
51         Integer x123 = Integer.valueOf(123);
52         Integer x456 = Integer.valueOf(456);
53 
54         Array.set(intArray, 0, x123);
55         Array.set(intArray, 1, x456);
56         if (!Array.get(intArray, 0).equals(x123) || !Array.get(intArray, 1).equals(x456)) {
57             throw new RuntimeException("bad 123 or 456");
58         }
59 
60         int[][] wrongArray;
61         try {
62             wrongArray = (int[][]) intArray;
63             throw new RuntimeException("cast should have failed");
64         } catch (ClassCastException cce) { }
65 
66         intArray = Array.newInstance(Integer.TYPE, 0);
67         if (Array.getLength(intArray) != 0)
68             throw new RuntimeException();
69         System.out.println("ReflectArrayTest.testSingleInt passed");
70     }
71 
testSingleChar()72     static void testSingleChar() {
73         Object charArray = Array.newInstance(Character.TYPE, 7);
74 
75         char[] array = (char[]) charArray;
76         array[0] = '0';
77         array[1] = 'W';
78         array[2] = '2';
79         array[3] = '3';
80         array[4] = 'X';
81         array[5] = '5';
82         array[6] = '6';
83         Array.setChar(charArray, 1, '1');
84         Array.setChar(charArray, 4, '4');
85         try {
86             Array.setShort(charArray, 3, (short) 'Y');
87             throw new RuntimeException("shouldn't allow short in char array");
88         } catch (IllegalArgumentException iae) {}
89         try {
90             Array.setInt(charArray, 5, 'Z');
91             throw new RuntimeException("shouldn't allow int in char array");
92         } catch (IllegalArgumentException iae) {}
93 
94         try {
95             for (int i = 0; i < array.length; i++) {
96                 if (Array.getInt(charArray, i) - '0' != i) {
97                     throw new RuntimeException("mismatch: " + i + " is " + array[i]);
98                 }
99             }
100 
101             if (Array.getInt(charArray, 4) != '4') {
102                 throw new RuntimeException("load should have worked");
103             }
104         } catch (IllegalArgumentException iae) {
105             iae.printStackTrace(System.out);
106         }
107         try {
108             Array.getByte(charArray, 2);
109             throw new RuntimeException("shouldn't allow read of char as byte");
110         } catch (IllegalArgumentException iae) {}
111 
112         Array.setChar(charArray, 3, (char) 0xffff);
113         try {
114             if (Array.getInt(charArray, 3) != 0xffff) {
115                 throw new RuntimeException("char got sign-extended? "
116                     + Array.getInt(charArray, 3));
117             }
118         } catch (IllegalArgumentException iae) {
119             iae.printStackTrace(System.out);
120         }
121 
122         System.out.println("ReflectArrayTest.testSingleChar passed");
123     }
124 
testSingleShort()125     static void testSingleShort() {
126         Object shortArray = Array.newInstance(Short.TYPE, 1);
127         Array.setShort(shortArray, 0, (short) -1);
128         if (Array.getInt(shortArray, 0) != -1) {
129             throw new RuntimeException("short didn't get sign-extended");
130         }
131 
132         Short box = (Short) Array.get(shortArray, 0);
133 
134         System.out.println("ReflectArrayTest.testSingleShort passed");
135     }
136 
testSingleLong()137     static void testSingleLong() {
138         Object longArray = Array.newInstance(Long.TYPE, 2);
139         Array.setInt(longArray, 0, 123);
140         Array.setLong(longArray, 1, 0x1122334455667788L);
141         try {
142             Array.getInt(longArray, 0);
143             throw new RuntimeException("shouldn't allow read of long as int");
144         } catch (IllegalArgumentException iae) {}
145 
146         long[] array = (long[]) longArray;
147         if (array[0] != 123 || array[1] != 0x1122334455667788L) {
148             throw new RuntimeException();
149         }
150 
151         float f = Array.getFloat(longArray, 0);
152         if (f < 122.9 || f > 123.1) {
153             throw new RuntimeException("long-as-float failed - " + f);
154         }
155         if (Array.getLong(longArray, 1) != 0x1122334455667788L) {
156             throw new RuntimeException("long1 failed");
157         }
158 
159         System.out.println("ReflectArrayTest.testSingleLong passed");
160     }
161 
testSingle()162     static void testSingle() {
163         Object strArray;
164 
165         strArray = Array.newInstance(String.class, 2);
166 
167         String[] array = (String[]) strArray;
168         array[0] = "entry zero";
169         Array.set(strArray, 1, "entry one");
170         try {
171             Array.set(strArray, 2, "entry two");
172             throw new RuntimeException("store should have failed");
173         } catch (ArrayIndexOutOfBoundsException abe) { }
174 
175         //System.out.println("array: " + array);
176 
177         if (!"entry zero".equals(Array.get(strArray, 0)))
178             throw new RuntimeException();
179         if (!"entry one".equals(array[1]))
180             throw new RuntimeException();
181 
182         if (array.length != Array.getLength(strArray) ||
183             array.length != 2)
184         {
185             throw new RuntimeException("bad len");
186         }
187 
188         try {
189             Array.set(strArray, 0, new Integer(5));
190             throw new RuntimeException("store of Integer should have failed");
191         } catch (IllegalArgumentException iae) {}
192         System.out.println("ReflectArrayTest.testSingle passed");
193     }
194 
testMultiInt()195     static void testMultiInt() {
196         Object intIntIntArray;
197         int[] dimensions = { 3, 2, 1 };
198 
199         intIntIntArray = Array.newInstance(Integer.TYPE, dimensions);
200         int[][][] array3 = (int[][][]) intIntIntArray;
201 
202         array3[0][0][0] = 123;      // trouble
203         array3[2][1][0] = 456;
204 
205         try {
206             array3[2][1][1] = 768;
207             throw new RuntimeException("store should have failed");
208         }
209         catch (ArrayIndexOutOfBoundsException abe) {
210         }
211         System.out.println("ReflectArrayTest.testMultiInt passed");
212     }
213 
testMulti()214     static void testMulti() {
215         Object strStrStrArray;
216         int[] dimensions = { 1, 2, 3 };
217 
218         strStrStrArray = Array.newInstance(String.class, dimensions);
219         String[][][] array3 = (String[][][]) strStrStrArray;
220 
221         array3[0][0][0] = "zero zero zero";
222         array3[0][1][2] = "zero one two";
223 
224         try {
225             array3[1][0][0] = "bad store";
226             throw new RuntimeException("store should have failed");
227         }
228         catch (ArrayIndexOutOfBoundsException abe) {
229         }
230 
231         try {
232             String[][] array2 = (String[][]) strStrStrArray;
233             throw new RuntimeException("expecting bad cast");
234         }
235         catch (ClassCastException cce) {
236         }
237 
238         String[] strar = new String[4];
239         strar[2] = "zero one two ++";
240         array3[0][1] = strar;
241         System.out.println(array3[0][1][2]);
242         //System.out.println("array3: " + array3);
243 
244 
245         int[] dimensions2 = { 1, 2 };
246         strStrStrArray = Array.newInstance(String[].class, dimensions2);
247         array3 = (String[][][]) strStrStrArray;
248 
249         array3[0][1] = new String[3];
250         array3[0][1][2] = "zero one two";
251         try {
252             array3[1][0][0] = "bad store";
253             throw new RuntimeException("store should have failed");
254         }
255         catch (ArrayIndexOutOfBoundsException abe) {
256         }
257         System.out.println("ReflectArrayTest.testMulti passed");
258     }
259 
testAbstract()260     static void testAbstract() {
261         Object arrayOfAbstractClasses = Array.newInstance(Number.class, 1);
262         System.out.println(arrayOfAbstractClasses.getClass().toString() + " modifiers: " +
263                            arrayOfAbstractClasses.getClass().getModifiers());
264         arrayOfAbstractClasses = Array.newInstance(Cloneable.class, 1);
265         System.out.println(arrayOfAbstractClasses.getClass().toString() + " modifiers: " +
266                            arrayOfAbstractClasses.getClass().getModifiers());
267         System.out.println("ReflectArrayTest.testAbstract passed");
268     }
269 }
270