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 package art;
18 
19 import java.lang.ref.Reference;
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.Proxy;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Comparator;
25 
26 public class Test912Art {
run()27   public static void run() throws Exception {
28     doTest();
29   }
30 
doTest()31   public static void doTest() throws Exception {
32     testClassEvents();
33   }
34 
testClassEvents()35   private static void testClassEvents() throws Exception {
36     // Note: the JIT part of this test is about the JIT pulling in a class not yet touched by
37     //       anything else in the system. This could be the verifier or the interpreter. We
38     //       block the interpreter by calling ensureJitCompiled. The verifier, however, must
39     //       run in configurations where dex2oat didn't verify the class itself. So explicitly
40     //       check whether the class has been already loaded, and skip then.
41     // TODO: Add multiple configurations to the run script once that becomes easier to do.
42     if (hasJit() && !isLoadedClass("Lart/Test912Art$ClassD;")) {
43       testClassEventsJit();
44     }
45   }
46 
testClassEventsJit()47   private static void testClassEventsJit() throws Exception {
48     enableClassLoadSeenEvents(true);
49 
50     testClassEventsJitImpl();
51 
52     enableClassLoadSeenEvents(false);
53 
54     if (!hadLoadEvent()) {
55       throw new RuntimeException("Did not get expected load event.");
56     }
57   }
58 
testClassEventsJitImpl()59   private static void testClassEventsJitImpl() throws Exception {
60     ensureJitCompiled(Test912Art.class, "testClassEventsJitImpl");
61 
62     if (ClassD.x != 1) {
63       throw new RuntimeException("Unexpected value");
64     }
65   }
66 
ensureJitCompiled(Class<?> c, String name)67   private static native void ensureJitCompiled(Class<?> c, String name);
68 
hasJit()69   private static native boolean hasJit();
isLoadedClass(String name)70   private static native boolean isLoadedClass(String name);
enableClassLoadSeenEvents(boolean b)71   private static native void enableClassLoadSeenEvents(boolean b);
hadLoadEvent()72   private static native boolean hadLoadEvent();
73 
74   public static class ClassD {
75     static int x = 1;
76   }
77 }
78