• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
18 
19   static Object exactCheck = new ExactCheck();
20   static Object abstractCheckImpl = new AbstractCheckImpl();
21   static Object interfaceCheckImpl = new InterfaceCheckImpl();
22   static Object normalCheck = new NormalCheck();
23   static Object regularObject = new Object();
24   static Object objectArray = new Object[2];
25   static Object intArray = new int[2];
26   static Object doubleArray = new double[2];
27   static Object exactArray = new ExactCheck[2];
28   static Object normalArray = new NormalCheck[2];
29 
30   static Object field;
31 
main(String[] args)32   public static void main(String[] args) {
33     checkInstanceOfNonTryCatch();
34     // We also check for a method with try/catch because the compiler then makes a slow
35     // path unconditionally save its live registers.
36     checkInstanceOfTryCatch();
37 
38     checkCheckCast();
39   }
40 
checkInstanceOfNonTryCatch()41   public static void checkInstanceOfNonTryCatch() {
42     check(true, exactCheck instanceof ExactCheck);
43     check(false, regularObject instanceof ExactCheck);
44 
45     check(true, abstractCheckImpl instanceof AbstractCheck);
46     check(false, regularObject instanceof AbstractCheck);
47 
48     check(true,  interfaceCheckImpl instanceof InterfaceCheck);
49     check(false, regularObject instanceof InterfaceCheck);
50 
51     check(true, normalCheck instanceof NormalCheck);
52     check(true, exactCheck instanceof NormalCheck);
53     check(false, regularObject instanceof NormalCheck);
54 
55     check(false, regularObject instanceof int[]);
56     check(false, objectArray instanceof int[]);
57     check(true, intArray instanceof int[]);
58     check(false, doubleArray instanceof int[]);
59 
60     check(false, regularObject instanceof ExactCheck[]);
61     check(false, objectArray instanceof ExactCheck[]);
62     check(false, doubleArray instanceof ExactCheck[]);
63     check(true, exactArray instanceof ExactCheck[]);
64     check(false, normalArray instanceof ExactCheck[]);
65 
66     check(false, regularObject instanceof NormalCheck[]);
67     check(false, objectArray instanceof NormalCheck[]);
68     check(false, doubleArray instanceof NormalCheck[]);
69     check(true, exactArray instanceof NormalCheck[]);
70     check(true, normalArray instanceof NormalCheck[]);
71 
72     check(false, regularObject instanceof Object[]);
73     check(true, objectArray instanceof Object[]);
74     check(false, doubleArray instanceof Object[]);
75     check(true, exactArray instanceof Object[]);
76     check(true, normalArray instanceof Object[]);
77   }
78 
checkInstanceOfTryCatch()79   public static void checkInstanceOfTryCatch() {
80     try {
81       check(true, exactCheck instanceof ExactCheck);
82       check(false, regularObject instanceof ExactCheck);
83 
84       check(true, abstractCheckImpl instanceof AbstractCheck);
85       check(false, regularObject instanceof AbstractCheck);
86 
87       check(true,  interfaceCheckImpl instanceof InterfaceCheck);
88       check(false, regularObject instanceof InterfaceCheck);
89 
90       check(true, normalCheck instanceof NormalCheck);
91       check(true, exactCheck instanceof NormalCheck);
92       check(false, regularObject instanceof NormalCheck);
93 
94       check(false, regularObject instanceof int[]);
95       check(false, objectArray instanceof int[]);
96       check(true, intArray instanceof int[]);
97       check(false, doubleArray instanceof int[]);
98 
99       check(false, regularObject instanceof ExactCheck[]);
100       check(false, objectArray instanceof ExactCheck[]);
101       check(false, doubleArray instanceof ExactCheck[]);
102       check(true, exactArray instanceof ExactCheck[]);
103       check(false, normalArray instanceof ExactCheck[]);
104 
105       check(false, regularObject instanceof NormalCheck[]);
106       check(false, objectArray instanceof NormalCheck[]);
107       check(false, doubleArray instanceof NormalCheck[]);
108       check(true, exactArray instanceof NormalCheck[]);
109       check(true, normalArray instanceof NormalCheck[]);
110 
111       check(false, regularObject instanceof Object[]);
112       check(true, objectArray instanceof Object[]);
113       check(false, doubleArray instanceof Object[]);
114       check(true, exactArray instanceof Object[]);
115       check(true, normalArray instanceof Object[]);
116     } catch (Throwable t) {
117       throw new Error("Unreachable");
118     }
119   }
120 
check(boolean expected, boolean actual)121   public static void check(boolean expected, boolean actual) {
122     if (actual != expected) {
123       throw new Error("Expected " + expected + ", got " + actual);
124     }
125   }
126 
checkCheckCast()127   public static void checkCheckCast() {
128     // Exact check.
129     field = (ExactCheck)exactCheck;
130     try {
131       field = (ExactCheck)regularObject;
132       throw new Error("Can't reach here");
133     } catch (ClassCastException ignore) {}
134 
135     // Abstract check.
136     field = (AbstractCheck)abstractCheckImpl;
137     try {
138       field = (AbstractCheck)regularObject;
139       throw new Error("Can't reach here");
140     } catch (ClassCastException ignore) {}
141 
142     // Interface check.
143     field = (InterfaceCheck)interfaceCheckImpl;
144     try {
145       field = (InterfaceCheck)regularObject;
146       throw new Error("Can't reach here");
147     } catch (ClassCastException ignore) {}
148 
149     // Normal check.
150     field = (NormalCheck)normalCheck;
151     field = (NormalCheck)exactCheck;
152     try {
153       field = (NormalCheck)regularObject;
154       throw new Error("Can't reach here");
155     } catch (ClassCastException ignore) {}
156 
157     // Primitive array check.
158     try {
159       field = (int[])regularObject;
160       throw new Error("Can't reach here");
161     } catch (ClassCastException ignore) {}
162 
163     try {
164       field = (int[])objectArray;
165       throw new Error("Can't reach here");
166     } catch (ClassCastException ignore) {}
167 
168     field = (int[])intArray;
169     try {
170       field = (int[])doubleArray;
171       throw new Error("Can't reach here");
172     } catch (ClassCastException ignore) {}
173 
174     // Array with final component type check.
175     try {
176       field = (ExactCheck[])regularObject;
177       throw new Error("Can't reach here");
178     } catch (ClassCastException ignore) {}
179 
180     try {
181       field = (ExactCheck[])objectArray;
182       throw new Error("Can't reach here");
183     } catch (ClassCastException ignore) {}
184 
185     try {
186       field = (ExactCheck[])doubleArray;
187       throw new Error("Can't reach here");
188     } catch (ClassCastException ignore) {}
189 
190     field = (ExactCheck[])exactArray;
191     try {
192       field = (ExactCheck[])normalArray;
193       throw new Error("Can't reach here");
194     } catch (ClassCastException ignore) {}
195 
196     // Array with non final component type check.
197     try {
198       field = (NormalCheck[])regularObject;
199       throw new Error("Can't reach here");
200     } catch (ClassCastException ignore) {}
201 
202     try {
203       field = (NormalCheck[])objectArray;
204       throw new Error("Can't reach here");
205     } catch (ClassCastException ignore) {}
206 
207     try {
208       field = (NormalCheck[])doubleArray;
209       throw new Error("Can't reach here");
210     } catch (ClassCastException ignore) {}
211 
212     field = (NormalCheck[])exactArray;
213     field = (NormalCheck[])normalArray;
214 
215     // Object[] check.
216     try{
217       field = (Object[])regularObject;
218       throw new Error("Can't reach here");
219     } catch (ClassCastException ignore) {}
220 
221     field = (Object[])objectArray;
222     try {
223       field = (Object[])doubleArray;
224       throw new Error("Can't reach here");
225     } catch (ClassCastException ignore) {}
226 
227     field = (Object[])exactArray;
228     field = (Object[])normalArray;
229   }
230 }
231 
232 class NormalCheck {
233 }
234 
235 final class ExactCheck extends NormalCheck {
236 }
237 
238 abstract class AbstractCheck {
239 }
240 
241 class AbstractCheckImpl extends AbstractCheck {
242 }
243 
244 interface InterfaceCheck {
245 }
246 
247 class InterfaceCheckImpl implements InterfaceCheck {
248 }
249