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
17import java.lang.reflect.Method;
18
19public class Main  {
20
21  class MyClassA {}
22  class MyClassB extends MyClassA {}
23
24  public static void main(String[] args) throws Exception {
25    testReferenceTypePropagation();
26    invokeTestInliner();
27  }
28
29  // Reference type propagation (RTP) used to assume that if a class is final,
30  // then the type must be exact. This does not hold for arrays which are always
31  // final, i.e. not extendable, but may be assigned to from values of the
32  // components type subclasses.
33
34  public static void testReferenceTypePropagation() throws Exception {
35    boolean expectTrue;
36
37    // Bug #1: RTP would set the type of `array` to exact Object[]. Instruction
38    // simplifier would then simplify the instanceof to `false`.
39    Object[] array = $noinline$getArray();
40    expectTrue = array instanceof MyClassA[];
41    if (!expectTrue) {
42      throw new Exception("Incorrect type check.");
43    }
44
45    // Bug #2: This is the true-branch of the instanceof above. The bound type
46    // for `array` would be again set to exact MyClassA[] and incorrectly
47    // simplify the second instanceof to `false`.
48    expectTrue = array instanceof MyClassB[];
49    if (!expectTrue) {
50      throw new Exception("Incorrect type bound.");
51    }
52  }
53
54  public static void invokeTestInliner() throws Exception {
55    Class<?> c = Class.forName("TestCase");
56    Method m = c.getMethod("testInliner");
57    m.invoke(null);
58  }
59
60  public static Object[] $noinline$getArray() {
61    if (doThrow) throw new Error();
62    return new MyClassB[2];
63  }
64
65  static boolean doThrow = false;
66}
67