1 /*
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 
17 import java.lang.reflect.Field;
18 import java.lang.reflect.Method;
19 import java.lang.reflect.InvocationTargetException;
20 
21 public class Main {
assertIsInterpreted()22   public static native void assertIsInterpreted();
ensureJitCompiled(Class<?> cls, String methodName)23   public static native void ensureJitCompiled(Class<?> cls, String methodName);
24 
assertEqual(String expected, String actual)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 
main(String[] args)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     {
37       // If the JIT is enabled, ensure it has compiled the method to force the deopt.
38       ensureJitCompiled(c, "testNoAlias");
39       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     {
54       // If the JIT is enabled, ensure it has compiled the method to force the deopt.
55       ensureJitCompiled(c, "testAlias");
56       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 }
71