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 annotations.BootstrapMethod;
18 import annotations.CalledByIndy;
19 import annotations.Constant;
20 import java.lang.invoke.CallSite;
21 import java.lang.invoke.ConstantCallSite;
22 import java.lang.invoke.MethodHandle;
23 import java.lang.invoke.MethodHandles;
24 import java.lang.invoke.MethodType;
25 
26 class TestDynamicBootstrapArguments extends TestBase {
27     private static int bsmCalls = 0;
28 
bsm( MethodHandles.Lookup lookup, String name, MethodType methodType, String otherNameComponent, long nameSuffix)29     static CallSite bsm(
30             MethodHandles.Lookup lookup,
31             String name,
32             MethodType methodType,
33             String otherNameComponent,
34             long nameSuffix)
35             throws Throwable {
36         bsmCalls = bsmCalls + 1;
37         Class<?> definingClass = TestDynamicBootstrapArguments.class;
38         String methodName = name + otherNameComponent + nameSuffix;
39         MethodHandle mh = lookup.findStatic(definingClass, methodName, methodType);
40         System.out.println("bsm");
41         return new ConstantCallSite(mh);
42     }
43 
44     @CalledByIndy(
45         bootstrapMethod =
46                 @BootstrapMethod(
47                     enclosingType = TestDynamicBootstrapArguments.class,
48                     name = "bsm",
49                     parameterTypes = {
50                         MethodHandles.Lookup.class,
51                         String.class,
52                         MethodType.class,
53                         String.class,
54                         long.class
55                     }
56                 ),
57         fieldOrMethodName = "target",
58         returnType = int.class,
59         parameterTypes = {int.class, String.class, double.class},
60         constantArgumentsForBootstrapMethod = {
61             @Constant(stringValue = "A"),
62             @Constant(longValue = 100000000l)
63         }
64     )
testDynamic(int i, String s, Double d)65     private static int testDynamic(int i, String s, Double d) {
66         assertNotReached();
67         return 0;
68     }
69 
targetA100000000(int i, String s, Double d)70     private static int targetA100000000(int i, String s, Double d) {
71         System.out.print(i);
72         System.out.print(", ");
73         System.out.print(s);
74         System.out.print(", ");
75         System.out.println(d);
76         return i;
77     }
78 
testCallSites()79     static void testCallSites() {
80         assertEquals(0, testDynamic(0, "One", Math.PI));
81         assertEquals(1, testDynamic(1, "Two", Math.E));
82         assertEquals(2, testDynamic(2, "Three", 0.0));
83     }
84 
test()85     static void test() {
86         System.out.println("TestDynamicArguments");
87         testCallSites();
88         assertEquals(3, bsmCalls);
89         testCallSites();
90         assertEquals(3, bsmCalls);
91     }
92 }
93