1 /* 2 * Copyright (C) 2014 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 public class Main { 18 public static Object a; 19 $opt$CheckCastMain()20 public static Object $opt$CheckCastMain() { 21 return (Main)a; 22 } 23 $opt$CheckCastFinalClass()24 public static Object $opt$CheckCastFinalClass() { 25 return (FinalClass)a; 26 } 27 main(String[] args)28 public static void main(String[] args) { 29 $opt$TestMain(); 30 $opt$TestFinalClass(); 31 } 32 $opt$TestMain()33 public static void $opt$TestMain() { 34 a = new Main(); 35 $opt$CheckCastMain(); 36 37 a = null; 38 $opt$CheckCastMain(); 39 40 a = new MainChild(); 41 $opt$CheckCastMain(); 42 43 a = new Object(); 44 try { 45 $opt$CheckCastMain(); 46 throw new Error("Should have gotten a ClassCastException"); 47 } catch (ClassCastException ex) {} 48 } 49 $opt$TestFinalClass()50 public static void $opt$TestFinalClass() { 51 a = new FinalClass(); 52 $opt$CheckCastFinalClass(); 53 54 a = null; 55 $opt$CheckCastFinalClass(); 56 57 a = new Main(); 58 try { 59 $opt$CheckCastFinalClass(); 60 throw new Error("Should have gotten a ClassCastException"); 61 } catch (ClassCastException ex) {} 62 63 a = new Object(); 64 try { 65 $opt$CheckCastFinalClass(); 66 throw new Error("Should have gotten a ClassCastException"); 67 } catch (ClassCastException ex) {} 68 } 69 70 static class MainChild extends Main {} 71 72 static final class FinalClass {} 73 } 74