1 /*
2  * Copyright (C) 2015 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 {
$inline$classTypeTest(Object o)18   public static boolean $inline$classTypeTest(Object o) {
19     return ((SubMain)o) == o;
20   }
21 
$inline$interfaceTypeTest(Object o)22   public static boolean $inline$interfaceTypeTest(Object o) {
23     return ((Itf)o) == o;
24   }
25 
26   public static SubMain subMain;
27   public static Main mainField;
28   public static Unrelated unrelatedField;
29   public static FinalUnrelated finalUnrelatedField;
30 
31   /// CHECK-START: boolean Main.classTypeTestNull() register (after)
32   /// CHECK-NOT: CheckCast
classTypeTestNull()33   public static boolean classTypeTestNull() {
34     return $inline$classTypeTest(null);
35   }
36 
37   /// CHECK-START: boolean Main.classTypeTestExactMain() register (after)
38   /// CHECK: CheckCast
classTypeTestExactMain()39   public static boolean classTypeTestExactMain() {
40     return $inline$classTypeTest(new Main());
41   }
42 
43   /// CHECK-START: boolean Main.classTypeTestExactSubMain() register (after)
44   /// CHECK-NOT: CheckCast
classTypeTestExactSubMain()45   public static boolean classTypeTestExactSubMain() {
46     return $inline$classTypeTest(new SubMain());
47   }
48 
49   /// CHECK-START: boolean Main.classTypeTestSubMainOrNull() register (after)
50   /// CHECK-NOT: CheckCast
classTypeTestSubMainOrNull()51   public static boolean classTypeTestSubMainOrNull() {
52     return $inline$classTypeTest(subMain);
53   }
54 
55   /// CHECK-START: boolean Main.classTypeTestMainOrNull() register (after)
56   /// CHECK: CheckCast
classTypeTestMainOrNull()57   public static boolean classTypeTestMainOrNull() {
58     return $inline$classTypeTest(mainField);
59   }
60 
61   /// CHECK-START: boolean Main.classTypeTestUnrelated() register (after)
62   /// CHECK: CheckCast
classTypeTestUnrelated()63   public static boolean classTypeTestUnrelated() {
64     return $inline$classTypeTest(unrelatedField);
65   }
66 
67   /// CHECK-START: boolean Main.classTypeTestFinalUnrelated() register (after)
68   /// CHECK: CheckCast
classTypeTestFinalUnrelated()69   public static boolean classTypeTestFinalUnrelated() {
70     return $inline$classTypeTest(finalUnrelatedField);
71   }
72 
73   /// CHECK-START: boolean Main.interfaceTypeTestNull() register (after)
74   /// CHECK-NOT: CheckCast
interfaceTypeTestNull()75   public static boolean interfaceTypeTestNull() {
76     return $inline$interfaceTypeTest(null);
77   }
78 
79   /// CHECK-START: boolean Main.interfaceTypeTestExactMain() register (after)
80   /// CHECK: CheckCast
interfaceTypeTestExactMain()81   public static boolean interfaceTypeTestExactMain() {
82     return $inline$interfaceTypeTest(new Main());
83   }
84 
85   /// CHECK-START: boolean Main.interfaceTypeTestExactSubMain() register (after)
86   /// CHECK-NOT: CheckCast
interfaceTypeTestExactSubMain()87   public static boolean interfaceTypeTestExactSubMain() {
88     return $inline$interfaceTypeTest(new SubMain());
89   }
90 
91   /// CHECK-START: boolean Main.interfaceTypeTestSubMainOrNull() register (after)
92   /// CHECK-NOT: CheckCast
interfaceTypeTestSubMainOrNull()93   public static boolean interfaceTypeTestSubMainOrNull() {
94     return $inline$interfaceTypeTest(subMain);
95   }
96 
97   /// CHECK-START: boolean Main.interfaceTypeTestMainOrNull() register (after)
98   /// CHECK: CheckCast
interfaceTypeTestMainOrNull()99   public static boolean interfaceTypeTestMainOrNull() {
100     return $inline$interfaceTypeTest(mainField);
101   }
102 
103   /// CHECK-START: boolean Main.interfaceTypeTestUnrelated() register (after)
104   /// CHECK: CheckCast
interfaceTypeTestUnrelated()105   public static boolean interfaceTypeTestUnrelated() {
106     return $inline$interfaceTypeTest(unrelatedField);
107   }
108 
109   /// CHECK-START: boolean Main.interfaceTypeTestFinalUnrelated() register (after)
110   /// CHECK: CheckCast
interfaceTypeTestFinalUnrelated()111   public static boolean interfaceTypeTestFinalUnrelated() {
112     return $inline$interfaceTypeTest(finalUnrelatedField);
113   }
114 
115   /// CHECK-START: java.lang.String Main.knownTestWithLoadedClass() register (after)
116   /// CHECK-NOT: CheckCast
knownTestWithLoadedClass()117   public static String knownTestWithLoadedClass() {
118     return (String)$inline$getString();
119   }
120 
121   /// CHECK-START: Itf Main.knownTestWithUnloadedClass() register (after)
122   /// CHECK: CheckCast
knownTestWithUnloadedClass()123   public static Itf knownTestWithUnloadedClass() {
124     return (Itf)$inline$getString();
125   }
126 
$inline$getString()127   public static Object $inline$getString() {
128     return new String();
129   }
130 
$inline$getMain()131   public static Object $inline$getMain() {
132     return new Main();
133   }
134 
135   /// CHECK-START: void Main.nonNullBoundType() register (after)
136   /// CHECK-NOT: NullCheck
nonNullBoundType()137   public static void nonNullBoundType() {
138     Main main = (Main)$inline$getMain();
139     main.getClass();
140   }
141 
main(String[] args)142   public static void main(String[] args) {
143     classTypeTestNull();
144     try {
145       classTypeTestExactMain();
146       throw new Error("ClassCastException expected");
147     } catch (ClassCastException e) {}
148     classTypeTestExactSubMain();
149 
150     subMain = null;
151     classTypeTestSubMainOrNull();
152     subMain = new SubMain();
153     classTypeTestSubMainOrNull();
154 
155     mainField = null;
156     classTypeTestMainOrNull();
157     mainField = new Main();
158     try {
159       classTypeTestMainOrNull();
160       throw new Error("ClassCastException expected");
161     } catch (ClassCastException e) {}
162     mainField = new SubMain();
163     classTypeTestMainOrNull();
164 
165     unrelatedField = null;
166     classTypeTestUnrelated();
167     unrelatedField = new Unrelated();
168     try {
169       classTypeTestUnrelated();
170       throw new Error("ClassCastException expected");
171     } catch (ClassCastException e) {}
172 
173     finalUnrelatedField = null;
174     classTypeTestFinalUnrelated();
175     finalUnrelatedField = new FinalUnrelated();
176     try {
177       classTypeTestFinalUnrelated();
178       throw new Error("ClassCastException expected");
179     } catch (ClassCastException e) {}
180 
181     interfaceTypeTestNull();
182     try {
183       interfaceTypeTestExactMain();
184       throw new Error("ClassCastException expected");
185     } catch (ClassCastException e) {}
186     interfaceTypeTestExactSubMain();
187 
188     subMain = null;
189     interfaceTypeTestSubMainOrNull();
190     subMain = new SubMain();
191     interfaceTypeTestSubMainOrNull();
192 
193     mainField = null;
194     interfaceTypeTestMainOrNull();
195     mainField = new Main();
196     try {
197       interfaceTypeTestMainOrNull();
198       throw new Error("ClassCastException expected");
199     } catch (ClassCastException e) {}
200     mainField = new SubMain();
201     interfaceTypeTestMainOrNull();
202 
203     unrelatedField = null;
204     interfaceTypeTestUnrelated();
205     unrelatedField = new Unrelated();
206     try {
207       interfaceTypeTestUnrelated();
208       throw new Error("ClassCastException expected");
209     } catch (ClassCastException e) {}
210 
211     finalUnrelatedField = null;
212     interfaceTypeTestFinalUnrelated();
213     finalUnrelatedField = new FinalUnrelated();
214     try {
215       interfaceTypeTestFinalUnrelated();
216       throw new Error("ClassCastException expected");
217     } catch (ClassCastException e) {}
218   }
219 }
220 
221 interface Itf {
222 }
223 
224 class SubMain extends Main implements Itf {
225 }
226 
227 class Unrelated {
228 }
229 
230 final class FinalUnrelated {
231 }
232