xref: /aosp_15_r20/art/test/2285-var-and-methodhandles-staticfield-initialization/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2024 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 static java.lang.invoke.MethodType.methodType;
18 
19 import java.lang.invoke.MethodHandle;
20 import java.lang.invoke.MethodHandles;
21 import java.lang.invoke.VarHandle;
22 
23 public class Main {
24 
main(String[] args)25     public static void main(String[] args) throws Throwable {
26         testMethodHandleStaticFieldGet();
27         testMethodHandleStaticFieldPut();
28         testMethodHandleStaticMethod();
29         testVarHandleStaticFieldGet();
30         testVarHandleStaticFieldPut();
31     }
32 
testMethodHandleStaticFieldGet()33     private static void testMethodHandleStaticFieldGet() throws Throwable {
34         MethodHandle mh = MethodHandles.lookup()
35             .findStaticGetter(MHStaticGetter.class, "a", int.class);
36 
37         int actual = (int) mh.invokeExact();
38 
39         assertEquals(actual, 10);
40         assertEquals(MHStaticGetter.b, 12);
41     }
42 
43 
testMethodHandleStaticFieldPut()44     private static void testMethodHandleStaticFieldPut() throws Throwable {
45         MethodHandle mh = MethodHandles.lookup()
46             .findStaticSetter(MHStaticSetter.class, "a", int.class);
47 
48         mh.invokeExact(100);
49 
50         assertEquals(MHStaticSetter.a, 100);
51         assertEquals(MHStaticGetter.b, 12);
52     }
53 
testMethodHandleStaticMethod()54     private static void testMethodHandleStaticMethod() throws Throwable {
55         MethodHandle mh = MethodHandles.lookup()
56             .findStatic(MHStaticMethod.class, "getB", methodType(int.class));
57 
58         int actualB = (int) mh.invokeExact();
59 
60         assertEquals(actualB, 12);
61         assertEquals(MHStaticMethod.a, 10);
62     }
63 
testVarHandleStaticFieldGet()64     private static void testVarHandleStaticFieldGet() throws Throwable {
65         VarHandle vh = MethodHandles.lookup()
66             .findStaticVarHandle(VHStaticGet.class, "a", int.class);
67 
68         int actual = (int) vh.get();
69 
70         assertEquals(actual, 10);
71         assertEquals(VHStaticGet.b, 12);
72     }
73 
testVarHandleStaticFieldPut()74     private static void testVarHandleStaticFieldPut() throws Throwable {
75         VarHandle vh = MethodHandles.lookup()
76             .findStaticVarHandle(VHStaticGet.class, "a", int.class);
77 
78         vh.set(100);
79 
80         assertEquals(VHStaticGet.a, 100);
81         assertEquals(VHStaticGet.b, 12);
82     }
83 
assertEquals(int actual, int expected)84     private static void assertEquals(int actual, int expected) {
85         if (actual != expected) {
86             throw new AssertionError("Expected: " + expected + ", but got: " + actual);
87         }
88     }
89 }
90