xref: /aosp_15_r20/art/test/716-jli-jit-samples/src-art/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2018 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.invoke.MethodHandle;
18 import java.lang.invoke.MethodHandles;
19 import java.lang.invoke.MethodType;
20 import java.lang.invoke.VarHandle;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 
24 public class Main {
25     private static final int ITERATIONS = 100;
26 
27     private static final VarHandle widgetIdVarHandle;
28     private static int initialHotnessCounter;
29 
getHotnessCounter(Class<?> cls, String methodName)30     public static native int getHotnessCounter(Class<?> cls, String methodName);
31 
32     public static class Widget {
Widget(int id)33         public Widget(int id) {
34             this.id = id;
35         }
36 
getId()37         int getId() {
38             return id;
39         }
40 
41         int id;
42     }
43 
44     static {
45         try {
46             widgetIdVarHandle = MethodHandles.lookup().findVarHandle(Widget.class, "id", int.class);
47         } catch (Exception e) {
48             throw new Error(e);
49         }
50     }
51 
assertEquals(int i1, int i2)52     private static void assertEquals(int i1, int i2) {
53         if (i1 == i2) {
54             return;
55         }
56         throw new AssertionError("assertEquals i1: " + i1 + ", i2: " + i2);
57     }
58 
assertEquals(Object o, Object p)59     private static void assertEquals(Object o, Object p) {
60         if (o == p) {
61             return;
62         }
63         if (o != null && p != null && o.equals(p)) {
64             return;
65         }
66         throw new AssertionError("assertEquals: o1: " + o + ", o2: " + p);
67     }
68 
fail()69     private static void fail() {
70         System.out.println("fail");
71         Thread.dumpStack();
72     }
73 
fail(String message)74     private static void fail(String message) {
75         System.out.println("fail: " + message);
76         Thread.dumpStack();
77     }
78 
testMethodHandleCounters()79     private static void testMethodHandleCounters() throws Throwable {
80         for (int i = 0; i < ITERATIONS; ++i) {
81             // Regular MethodHandle invocations
82             MethodHandle mh =
83                     MethodHandles.lookup()
84                             .findConstructor(
85                                     Widget.class, MethodType.methodType(void.class, int.class));
86             Widget w = (Widget) mh.invoke(3);
87             w = (Widget) mh.invokeExact(3);
88             assertEquals(initialHotnessCounter, getHotnessCounter(MethodHandle.class, "invoke"));
89             assertEquals(initialHotnessCounter, getHotnessCounter(MethodHandle.class, "invokeExact"));
90 
91             // Reflective MethodHandle invocations
92             String[] methodNames = {"invoke", "invokeExact"};
93             for (String methodName : methodNames) {
94                 Method invokeMethod = MethodHandle.class.getMethod(methodName, Object[].class);
95                 MethodHandle instance =
96                         MethodHandles.lookup()
97                                 .findVirtual(
98                                         Widget.class, "getId", MethodType.methodType(int.class));
99                 try {
100                     invokeMethod.invoke(instance, new Object[] {new Object[] {}});
101                     fail();
102                 } catch (InvocationTargetException ite) {
103                     assertEquals(ite.getCause().getClass(), UnsupportedOperationException.class);
104                 }
105             }
106             assertEquals(initialHotnessCounter,
107                 getHotnessCounter(MethodHandle.class, "invoke"));
108             assertEquals(initialHotnessCounter,
109                 getHotnessCounter(MethodHandle.class, "invokeExact"));
110         }
111 
112         System.out.println("MethodHandle OK");
113     }
114 
testVarHandleCounters()115     private static void testVarHandleCounters() throws Throwable {
116         Widget w = new Widget(0);
117         for (int i = 0; i < ITERATIONS; ++i) {
118             // Regular accessor invocations
119             widgetIdVarHandle.set(w, i);
120             assertEquals(i, widgetIdVarHandle.get(w));
121             assertEquals(initialHotnessCounter, getHotnessCounter(VarHandle.class, "set"));
122             assertEquals(initialHotnessCounter, getHotnessCounter(VarHandle.class, "get"));
123 
124             // Reflective accessor invocations
125             for (String accessorName : new String[] {"get", "set"}) {
126                 Method setMethod = VarHandle.class.getMethod(accessorName, Object[].class);
127                 try {
128                     setMethod.invoke(widgetIdVarHandle, new Object[] {new Object[0]});
129                     fail();
130                 } catch (InvocationTargetException ite) {
131                     assertEquals(ite.getCause().getClass(), UnsupportedOperationException.class);
132                 }
133             }
134             assertEquals(initialHotnessCounter, getHotnessCounter(VarHandle.class, "set"));
135             assertEquals(initialHotnessCounter, getHotnessCounter(VarHandle.class, "get"));
136         }
137         System.out.println("VarHandle OK");
138     }
139 
main(String[] args)140     public static void main(String[] args) throws Throwable {
141         System.loadLibrary(args[0]);
142         initialHotnessCounter = getHotnessCounter(VarHandle.class, "set");
143         testMethodHandleCounters();
144         testVarHandleCounters();
145     }
146 }
147