blob: b55b0c5ad8c9a018f94ef64d7dd3ea000a70e9ef [file] [log] [blame]
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00001/*
2 * Copyright (C) 2016 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.Field;
18import java.lang.reflect.Method;
19import java.lang.reflect.InvocationTargetException;
20
21public class Main {
22 // Workaround for b/18051191.
23 class Inner {}
24
25 public static native void assertIsInterpreted();
Nicolas Geoffray2ba6ff22020-01-08 14:47:26 +000026 public static native void ensureJitCompiled(Class<?> cls, String methodName);
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000027
28 private static void assertEqual(String expected, String actual) {
29 if (!expected.equals(actual)) {
30 throw new Error("Assertion failed: " + expected + " != " + actual);
31 }
32 }
33
34 public static void main(String[] args) throws Throwable {
35 System.loadLibrary(args[0]);
36 Class<?> c = Class.forName("TestCase");
37 int[] array = new int[1];
38
39 {
Nicolas Geoffray2ba6ff22020-01-08 14:47:26 +000040 // If the JIT is enabled, ensure it has compiled the method to force the deopt.
41 ensureJitCompiled(c, "testNoAlias");
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000042 Method m = c.getMethod("testNoAlias", int[].class, String.class);
43 try {
44 m.invoke(null, new Object[] { array , "foo" });
45 throw new Error("Expected AIOOBE");
46 } catch (InvocationTargetException e) {
47 if (!(e.getCause() instanceof ArrayIndexOutOfBoundsException)) {
48 throw new Error("Expected AIOOBE");
49 }
50 // Ignore
51 }
52 Field field = c.getField("staticField");
53 assertEqual("foo", (String)field.get(null));
54 }
55
56 {
Nicolas Geoffray2ba6ff22020-01-08 14:47:26 +000057 // If the JIT is enabled, ensure it has compiled the method to force the deopt.
58 ensureJitCompiled(c, "testAlias");
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000059 Method m = c.getMethod("testAlias", int[].class, String.class);
60 try {
61 m.invoke(null, new Object[] { array, "bar" });
62 throw new Error("Expected AIOOBE");
63 } catch (InvocationTargetException e) {
64 if (!(e.getCause() instanceof ArrayIndexOutOfBoundsException)) {
65 throw new Error("Expected AIOOBE");
66 }
67 // Ignore
68 }
69 Field field = c.getField("staticField");
70 assertEqual("bar", (String)field.get(null));
71 }
72 }
73}