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 {
22   // Workaround for b/18051191.
23   class Inner {}
24 
assertIsInterpreted()25   public static native void assertIsInterpreted();
ensureJitCompiled(Class<?> cls, String methodName)26   public static native void ensureJitCompiled(Class<?> cls, String methodName);
27 
assertEqual(String expected, String actual)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 
main(String[] args)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     {
40       // If the JIT is enabled, ensure it has compiled the method to force the deopt.
41       ensureJitCompiled(c, "testNoAlias");
42       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     {
57       // If the JIT is enabled, ensure it has compiled the method to force the deopt.
58       ensureJitCompiled(c, "testAlias");
59       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 }
74