blob: be7b1082b0ac482a7e01271b2871c88e3ef05af0 [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 {
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000022 public static native void assertIsInterpreted();
Nicolas Geoffray2ba6ff22020-01-08 14:47:26 +000023 public static native void ensureJitCompiled(Class<?> cls, String methodName);
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000024
25 private static void assertEqual(String expected, String actual) {
26 if (!expected.equals(actual)) {
27 throw new Error("Assertion failed: " + expected + " != " + actual);
28 }
29 }
30
31 public static void main(String[] args) throws Throwable {
32 System.loadLibrary(args[0]);
33 Class<?> c = Class.forName("TestCase");
34 int[] array = new int[1];
35
36 {
Nicolas Geoffray2ba6ff22020-01-08 14:47:26 +000037 // If the JIT is enabled, ensure it has compiled the method to force the deopt.
38 ensureJitCompiled(c, "testNoAlias");
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000039 Method m = c.getMethod("testNoAlias", int[].class, String.class);
40 try {
41 m.invoke(null, new Object[] { array , "foo" });
42 throw new Error("Expected AIOOBE");
43 } catch (InvocationTargetException e) {
44 if (!(e.getCause() instanceof ArrayIndexOutOfBoundsException)) {
45 throw new Error("Expected AIOOBE");
46 }
47 // Ignore
48 }
49 Field field = c.getField("staticField");
50 assertEqual("foo", (String)field.get(null));
51 }
52
53 {
Nicolas Geoffray2ba6ff22020-01-08 14:47:26 +000054 // If the JIT is enabled, ensure it has compiled the method to force the deopt.
55 ensureJitCompiled(c, "testAlias");
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000056 Method m = c.getMethod("testAlias", int[].class, String.class);
57 try {
58 m.invoke(null, new Object[] { array, "bar" });
59 throw new Error("Expected AIOOBE");
60 } catch (InvocationTargetException e) {
61 if (!(e.getCause() instanceof ArrayIndexOutOfBoundsException)) {
62 throw new Error("Expected AIOOBE");
63 }
64 // Ignore
65 }
66 Field field = c.getField("staticField");
67 assertEqual("bar", (String)field.get(null));
68 }
69 }
70}