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