1 /* Test interface dispatch, type checking (instanceof), and casting. */ 2 3 interface IA 4 { a()5 String a(); 6 } 7 8 interface IB extends IA 9 { b()10 String b(); 11 } 12 13 interface IC extends IB 14 { c()15 void c(); d()16 int d(); e(int i)17 IB e(int i); 18 } 19 20 interface ID 21 { z()22 String z(); a()23 String a(); 24 } 25 26 class CA 27 { a()28 String a() 29 { 30 return "CA a()"; 31 } 32 } 33 34 class CB implements IB 35 { a()36 public String a() 37 { 38 return "CB a()"; 39 } 40 b()41 public String b() 42 { 43 return "CB b()"; 44 } 45 } 46 47 class CC extends CB 48 { d()49 public int d() 50 { 51 return 99; 52 } 53 } 54 55 class CD extends CC implements IC 56 { a()57 public String a() 58 { 59 return "CD a()"; 60 } 61 c()62 public void c() 63 { 64 System.out.println("CD c()"); 65 } 66 d()67 public int d() 68 { 69 return 6; 70 } 71 e(int i)72 public IB e(int i) 73 { 74 if (i == 1) 75 return new CB(); 76 else 77 return new CD(); 78 } 79 } 80 81 class CE extends CB implements IB, ID 82 { a()83 public String a() 84 { 85 return ("CE a()"); 86 } 87 b()88 public String b() 89 { 90 return ("CE b()"); 91 } 92 z()93 public String z() 94 { 95 return("CE z()"); 96 } 97 } 98 99 100 public class InterfaceDispatch 101 { main(String args[])102 public static void main(String args[]) 103 { 104 new InterfaceDispatch(); 105 } 106 InterfaceDispatch()107 public InterfaceDispatch() 108 { 109 /* _Jv_InstanceOf */ 110 111 /* Object instanceof CLASS */ 112 Object obj = new CA(); 113 114 if (obj instanceof CA) 115 { 116 System.out.println ("ok 1"); 117 } 118 else 119 { 120 System.out.println ("FAIL 1"); 121 } 122 123 obj = new CD(); 124 125 if (!(obj instanceof CA)) 126 { 127 System.out.println ("ok 2a"); 128 } 129 else 130 { 131 System.out.println ("FAIL 2a"); 132 } 133 134 if (obj instanceof CB) 135 { 136 System.out.println ("ok 2b"); 137 } 138 else 139 { 140 System.out.println ("FAIL 2b"); 141 } 142 143 144 /* Object instanceof INTERFACE */ 145 obj = new CB(); 146 147 if (!(obj instanceof IC)) 148 { 149 System.out.println("ok 3"); 150 } 151 else 152 { 153 System.out.println ("FAIL 3"); 154 } 155 156 if (obj instanceof IB) 157 { 158 System.out.println("ok 4"); 159 } 160 else 161 { 162 System.out.println ("FAIL 4"); 163 } 164 165 /* InterfaceRef instanceof INTERFACE */ 166 167 IA ia = new CB(); 168 169 if (ia instanceof IB) 170 { 171 System.out.println("ok 5"); 172 } 173 else 174 { 175 System.out.println ("FAIL 5"); 176 } 177 178 179 if (!(ia instanceof IC)) 180 { 181 System.out.println("ok 6"); 182 } 183 else 184 { 185 System.out.println ("FAIL 6"); 186 } 187 188 /* InterfaceRef instanceof CLASS */ 189 190 if (ia instanceof CB) 191 { 192 System.out.println("ok 7"); 193 } 194 else 195 { 196 System.out.println ("FAIL 7"); 197 } 198 199 200 if (!(ia instanceof CD)) 201 { 202 System.out.println("ok 8"); 203 } 204 else 205 { 206 System.out.println ("FAIL 8"); 207 } 208 209 210 /* _Jv_CheckCast */ 211 Object obj_ca = new CA(); 212 Object obj_cc = new CC(); 213 214 IA ia2; 215 216 try 217 { 218 ia2 = (IA) obj_cc; 219 System.out.println("ok 9"); 220 } 221 catch (ClassCastException x) 222 { 223 System.out.println("FAIL 9"); 224 } 225 226 CD cd; 227 228 try 229 { 230 cd = (CD) obj_ca; 231 System.out.println("FAIL 10"); 232 } 233 catch (ClassCastException x) 234 { 235 System.out.println("ok 10"); 236 } 237 238 IA ia3; 239 240 try 241 { 242 ia3 = (IB) obj_ca; 243 System.out.println("FAIL 11"); 244 } 245 catch (ClassCastException x) 246 { 247 System.out.println("ok 11"); 248 } 249 250 /* _Jv_LookupInterfaceMethod */ 251 Object obj_cb = new CB(); 252 253 IB ib = (IB) obj_cb; 254 ib.b(); 255 if (ib.a().equalsIgnoreCase("CB a()")) 256 System.out.println("ok 12"); 257 else 258 System.out.println("FAIL 12"); 259 260 IC ic = new CD(); 261 if (ic.a().equalsIgnoreCase("CD a()")) 262 System.out.println("ok 13"); 263 else 264 System.out.println("FAIL 13"); 265 266 if (ic.d() == 6) 267 System.out.println("ok 14"); 268 else 269 System.out.println("FAIL 14"); 270 271 Object ce = new CE(); 272 273 ib = (IB) ce; 274 ID id = (ID) ce; 275 276 if (ib.b().equals("CE b()") && id.a().equals("CE a()")) 277 System.out.println("ok 15"); 278 else 279 System.out.println("FAIL 15"); 280 281 String t = ((ID)ce).z(); 282 283 if (t.equalsIgnoreCase("CE z()")) 284 System.out.println("ok 16"); 285 else 286 System.out.println("FAIL 16"); 287 288 /* Array types */ 289 290 Object[] obj_a = new CC[10]; 291 try 292 { 293 CB[] ca_a = (CB[]) obj_a; 294 System.out.println("ok 17"); 295 } 296 catch (ClassCastException x) 297 { 298 System.out.println("FAIL 17"); 299 } 300 301 if (obj_a instanceof IB[]) 302 { 303 System.out.println("ok 18"); 304 } 305 else 306 { 307 System.out.println("FAIL 18"); 308 } 309 310 IB[] ib_a = new CD[5]; 311 try 312 { 313 CD[] cd_a = (CD[]) ib_a; 314 System.out.println("ok 19"); 315 } 316 catch (ClassCastException x) 317 { 318 System.out.println("FAIL 19"); 319 } 320 321 CA[] ca_a; 322 323 try 324 { 325 ca_a = (CA[]) ib_a; 326 System.out.println("FAIL 20"); 327 } 328 catch (ClassCastException x) 329 { 330 System.out.println("ok 20"); 331 } 332 333 334 /* Primitive types */ 335 336 short[] short_a = new short[100]; 337 338 try 339 { 340 obj = short_a; 341 System.out.println("ok 21"); 342 } 343 catch (ClassCastException x) 344 { 345 System.out.println("FAIL 21"); 346 } 347 348 try 349 { 350 short[] short_b = (short[]) obj; 351 System.out.println("ok 22"); 352 } 353 catch (ClassCastException x) 354 { 355 System.out.println("FAIL 22"); 356 } 357 358 int[] short_b; 359 360 try 361 { 362 short_b = (int[]) obj; 363 System.out.println("FAIL 23"); 364 } 365 catch (ClassCastException x) 366 { 367 System.out.println("ok 23"); 368 } 369 370 Object obj1 = new int[25]; 371 372 if (obj1 instanceof short[]) 373 { 374 System.out.println("FAIL 24"); 375 } 376 else 377 { 378 System.out.println("ok 24"); 379 } 380 381 if (obj1 instanceof int[]) 382 { 383 System.out.println("ok 25"); 384 } 385 else 386 { 387 System.out.println("FAIL 25"); 388 } 389 390 /* null assignment */ 391 392 CA obj_ca2 = null; 393 394 if (obj_ca2 instanceof CA) 395 { 396 System.out.println("FAIL 26"); 397 } 398 else 399 { 400 System.out.println("ok 26"); 401 } 402 } 403 } 404