• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         testSingle();
14         testMultiInt();
15         testMulti();
16 
17         System.out.println("ReflectArrayTest passed");
18     }
19 
testSingleInt()20     static void testSingleInt() {
21         Object intArray;
22 
23         intArray = Array.newInstance(Integer.TYPE, 2);
24 
25         int[] array = (int[]) intArray;
26         array[0] = 5;
27         Array.setInt(intArray, 1, 6);
28 
29         if (Array.getInt(intArray, 0) != 5)
30             throw new RuntimeException();
31         if (array[1] != 6)
32             throw new RuntimeException();
33         try {
34             array[2] = 27;
35             throw new RuntimeException("store should have failed");
36         }
37         catch (ArrayIndexOutOfBoundsException abe) {
38         }
39         if (array.length != Array.getLength(intArray) ||
40             array.length != 2)
41         {
42             throw new RuntimeException("bad len");
43         }
44 
45         int[][] wrongArray;
46         try {
47             wrongArray = (int[][]) intArray;
48             throw new RuntimeException("cast should have failed");
49         }
50         catch (ClassCastException cce) {
51         }
52 
53         intArray = Array.newInstance(Integer.TYPE, 0);
54         if (Array.getLength(intArray) != 0)
55             throw new RuntimeException();
56         System.out.println("ReflectArrayTest.testSingleInt passed");
57     }
58 
testSingle()59     static void testSingle() {
60         Object strArray;
61 
62         strArray = Array.newInstance(String.class, 2);
63 
64         String[] array = (String[]) strArray;
65         array[0] = "entry zero";
66         Array.set(strArray, 1, "entry one");
67 
68         //System.out.println("array: " + array);
69 
70         if (!"entry zero".equals(Array.get(strArray, 0)))
71             throw new RuntimeException();
72         if (!"entry one".equals(array[1]))
73             throw new RuntimeException();
74 
75         if (array.length != Array.getLength(strArray) ||
76             array.length != 2)
77         {
78             throw new RuntimeException("bad len");
79         }
80         System.out.println("ReflectArrayTest.testSingle passed");
81     }
82 
testMultiInt()83     static void testMultiInt() {
84         Object intIntIntArray;
85         int[] dimensions = { 3, 2, 1 };
86 
87         intIntIntArray = Array.newInstance(Integer.TYPE, dimensions);
88         int[][][] array3 = (int[][][]) intIntIntArray;
89 
90         array3[0][0][0] = 123;      // trouble
91         array3[2][1][0] = 456;
92 
93         try {
94             array3[2][1][1] = 768;
95             throw new RuntimeException("store should have failed");
96         }
97         catch (ArrayIndexOutOfBoundsException abe) {
98         }
99         System.out.println("ReflectArrayTest.testMultiInt passed");
100     }
101 
testMulti()102     static void testMulti() {
103         Object strStrStrArray;
104         int[] dimensions = { 1, 2, 3 };
105 
106         strStrStrArray = Array.newInstance(String.class, dimensions);
107         String[][][] array3 = (String[][][]) strStrStrArray;
108 
109         array3[0][0][0] = "zero zero zero";
110         array3[0][1][2] = "zero one two";
111 
112         try {
113             array3[1][0][0] = "bad store";
114             throw new RuntimeException("store should have failed");
115         }
116         catch (ArrayIndexOutOfBoundsException abe) {
117         }
118 
119         try {
120             String[][] array2 = (String[][]) strStrStrArray;
121             throw new RuntimeException("expecting bad cast");
122         }
123         catch (ClassCastException cce) {
124         }
125 
126         String[] strar = new String[4];
127         strar[2] = "zero one two ++";
128         array3[0][1] = strar;
129         System.out.println(array3[0][1][2]);
130         //System.out.println("array3: " + array3);
131 
132 
133         int[] dimensions2 = { 1, 2 };
134         strStrStrArray = Array.newInstance(String[].class, dimensions2);
135         array3 = (String[][][]) strStrStrArray;
136 
137         array3[0][1] = new String[3];
138         array3[0][1][2] = "zero one two";
139         try {
140             array3[1][0][0] = "bad store";
141             throw new RuntimeException("store should have failed");
142         }
143         catch (ArrayIndexOutOfBoundsException abe) {
144         }
145         System.out.println("ReflectArrayTest.testMulti passed");
146     }
147 }
148 
149