• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2008 The Android Open Source Project
2 
3 import java.io.Serializable;
4 import java.util.Arrays;
5 
6 /**
7  * Exercise some class-related instructions.
8  */
9 public class Classes {
10     int mSome;
11 
subFunc(boolean wantSub)12     public void subFunc(boolean wantSub) {
13         assert(!wantSub);
14     }
15 
checkCast(Object thisRef, Object moreRef, Object nullRef)16     void checkCast(Object thisRef, Object moreRef, Object nullRef) {
17         System.out.println("Classes.checkCast");
18 
19         Classes classes;
20         MoreClasses more;
21 
22         classes = (Classes) thisRef;
23         assert(thisRef instanceof Classes);
24         classes = (Classes) moreRef;
25         assert(moreRef instanceof Classes);
26 
27         more = (MoreClasses) moreRef;
28         assert(moreRef instanceof MoreClasses);
29         assert(!(thisRef instanceof MoreClasses));
30 
31         try {
32             more = (MoreClasses) thisRef;
33             assert(false);
34         } catch (ClassCastException cce) {
35             //System.out.println("  class cast msg: " + cce.getMessage());
36             //Dalvik throws terser message than Hotspot VM
37             assert(cce.getMessage().regionMatches(false, 0, "Classes", 0, 7));
38         }
39         assert(!(thisRef instanceof MoreClasses));
40 
41         /* hopefully these classes cause a resolve */
42         try {
43             java.math.RoundingMode mode = (java.math.RoundingMode) thisRef;
44             assert(false);
45         } catch (ClassCastException cce) {
46             //System.out.println("  class cast msg: " + cce.getMessage());
47             //Dalvik throws terser message than Hotspot VM
48             assert(cce.getMessage().regionMatches(false, 0, "Classes", 0, 7));
49         }
50         assert(!(thisRef instanceof java.math.BigDecimal));
51 
52         /* try some stuff with a null reference */
53         classes = (Classes) nullRef;
54         classes = (MoreClasses) nullRef;
55         more = (MoreClasses) nullRef;
56         assert(!(nullRef instanceof Classes));
57 
58     }
59 
60 
xTests(Object x)61     static void xTests(Object x) {
62         assert(  x instanceof Classes);
63         assert(!(x instanceof MoreClasses));
64     }
yTests(Object y)65     static void yTests(Object y) {
66         assert(  y instanceof Classes);
67         assert(  y instanceof MoreClasses);
68     }
xarTests(Object xar)69     static void xarTests(Object xar) {
70         assert(  xar instanceof Object);
71         assert(!(xar instanceof Classes));
72         assert(  xar instanceof Classes[]);
73         assert(!(xar instanceof MoreClasses[]));
74         assert(  xar instanceof Object[]);
75         assert(!(xar instanceof Object[][]));
76     }
yarTests(Object yar)77     static void yarTests(Object yar) {
78         assert(  yar instanceof Classes[]);
79         assert(  yar instanceof MoreClasses[]);
80     }
xarararTests(Object xararar)81     static void xarararTests(Object xararar) {
82         assert(  xararar instanceof Object);
83         assert(  xararar instanceof Object[]);
84         assert(!(xararar instanceof Classes));
85         assert(!(xararar instanceof Classes[]));
86         assert(!(xararar instanceof Classes[][]));
87         assert(  xararar instanceof Classes[][][]);
88         assert(!(xararar instanceof MoreClasses[][][]));
89         assert(  xararar instanceof Object[][][]);
90         assert(  xararar instanceof Serializable);
91         assert(  xararar instanceof Serializable[]);
92         assert(  xararar instanceof Serializable[][]);
93         assert(!(xararar instanceof Serializable[][][]));
94     }
yarararTests(Object yararar)95     static void yarararTests(Object yararar) {
96         assert(  yararar instanceof Classes[][][]);
97         assert(  yararar instanceof MoreClasses[][][]);
98     }
iarTests(Object iar)99     static void iarTests(Object iar) {
100         assert(  iar instanceof Object);
101         assert(!(iar instanceof Object[]));
102     }
iararTests(Object iarar)103     static void iararTests(Object iarar) {
104         assert(  iarar instanceof Object);
105         assert(  iarar instanceof Object[]);
106         assert(!(iarar instanceof Object[][]));
107     }
108 
109     /*
110      * Exercise filled-new-array and test instanceof on arrays.
111      *
112      * We call out instead of using "instanceof" directly to avoid
113      * compiler optimizations.
114      */
arrayInstance()115     static void arrayInstance() {
116         System.out.println("Classes.arrayInstance");
117 
118         Classes x = new Classes();
119         Classes[] xar = new Classes[1];
120         Classes[][] xarar = new Classes[1][1];
121         Classes[][][] xararar = new Classes[1][2][3];
122         MoreClasses y = new MoreClasses();
123         MoreClasses[] yar = new MoreClasses[3];
124         MoreClasses[][] yarar = new MoreClasses[2][3];
125         MoreClasses[][][] yararar = new MoreClasses[1][2][3];
126         int[] iar = new int[1];
127         int[][] iarar = new int[1][1];
128         Object test;
129 
130         xTests(x);
131         yTests(y);
132         xarTests(xar);
133         yarTests(yar);
134         xarararTests(xararar);
135         yarararTests(yararar);
136         iarTests(iar);
137         iararTests(iarar);
138 
139         yararar[0] = yarar;
140         yararar[0][0] = yar;
141         yararar[0][1] = yar;
142         yararar[0][0][0] = y;
143         yararar[0][0][1] = y;
144         yararar[0][0][2] = y;
145         yararar[0][1][0] = y;
146         yararar[0][1][1] = y;
147         yararar[0][1][2] = y;
148 
149         String strForm;
150 
151         String[][][][] multi1 = new String[2][3][2][1];
152         multi1[0] = new String[2][3][2];
153         multi1[0][1] = new String[3][2];
154         multi1[0][1][2] = new String[2];
155         multi1[0][1][2][1] = "HELLO-1";
156         strForm = Arrays.deepToString(multi1);
157 
158         String[][][][][] multi2 = new String[5][2][3][2][1];
159         multi2[0] = new String[5][2][3][2];
160         multi2[0][1] = new String[5][2][3];
161         multi2[0][1][2] = new String[5][2];
162         multi2[0][1][2][1] = new String[5];
163         multi2[0][1][2][1][4] = "HELLO-2";
164         strForm = Arrays.deepToString(multi2);
165 
166 
167         String[][][][][][] multi3 = new String[2][5][2][3][2][1];
168         multi3[0] = new String[2][][][][];
169         multi3[0][1] = new String[3][][][];
170         multi3[0][1][2] = new String[2][][];
171         multi3[0][1][2][1] = new String[5][];
172         multi3[0][1][2][1][4] = new String[2];
173         multi3[0][1][2][1][4][1] = "HELLO-3";
174         strForm = Arrays.deepToString(multi3);
175 
176         // build up pieces
177         String[][][][][][] multi4 = new String[1][][][][][];
178         multi4[0] = new String[2][][][][];
179         multi4[0][1] = new String[3][][][];
180         multi4[0][1][2] = new String[2][][];
181         multi4[0][1][2][1] = new String[5][];
182         multi4[0][1][2][1][4] = new String[2];
183         multi4[0][1][2][1][4][1] = "HELLO-4";
184         strForm = Arrays.deepToString(multi4);
185 
186         /* this is expected to fail; 1073921584 * 4 overflows 32 bits */
187         try {
188             String[][][][][] multiX = new String[5][2][3][2][1073921584];
189             assert(false);
190         } catch (Error e) {
191             //System.out.println("  Got expected failure: " + e);
192         }
193 
194     }
195 
run()196     public static void run() {
197         Classes classes = new Classes();
198         MoreClasses more = new MoreClasses();
199         classes.checkCast(classes, more, null);
200 
201         more.subFunc(true);
202         more.superFunc(false);
203         arrayInstance();
204     }
205 }
206 
207 class MoreClasses extends Classes {
208     int mMore;
209 
MoreClasses()210     public MoreClasses() {}
211 
subFunc(boolean wantSub)212     public void subFunc(boolean wantSub) {
213         assert(wantSub);
214     }
215 
superFunc(boolean wantSub)216     public void superFunc(boolean wantSub) {
217         super.subFunc(wantSub);
218     }
219 }
220