1 /*
2  * Copyright (C) 2017 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.CallSite;
18 import java.lang.invoke.MethodType;
19 import java.lang.invoke.MutableCallSite;
20 
21 public class Main extends TestBase {
22 
TestUninitializedCallSite()23     private static void TestUninitializedCallSite() throws Throwable {
24         CallSite callSite = new MutableCallSite(MethodType.methodType(int.class));
25         try {
26             callSite.getTarget().invoke();
27             fail();
28         } catch (IllegalStateException e) {
29             System.out.println("Caught exception from uninitialized call site");
30         }
31 
32         callSite = new MutableCallSite(MethodType.methodType(String.class, int.class, char.class));
33         try {
34             callSite.getTarget().invoke(1535, 'd');
35             fail();
36         } catch (IllegalStateException e) {
37             System.out.println("Caught exception from uninitialized call site");
38         }
39     }
40 
TestLinkerMethodMultipleArgumentTypes()41     private static void TestLinkerMethodMultipleArgumentTypes() throws Throwable {
42         TestLinkerMethodMultipleArgumentTypes.test(33, 67);
43         TestLinkerMethodMultipleArgumentTypes.test(-10000, 1000);
44         TestLinkerMethodMultipleArgumentTypes.test(-1000, 10000);
45     }
46 
TestLinkerMethodWithRange()47     private static void TestLinkerMethodWithRange() throws Throwable {
48         TestLinkerMethodWithRange.test(0, 1, 2, 3, 4, 5);
49         TestLinkerMethodWithRange.test(-101, -79, 113, 9, 17, 229);
50         TestLinkerMethodWithRange.test(811, 823, 947, 967, 1087, 1093);
51 
52         TestLinkerMethodWithRange.test(null, null, null, null, null, null);
53         TestLinkerMethodWithRange.test(Double.valueOf(1.0), null, Double.valueOf(3.0), null,
54                                        Double.valueOf(37.0), null);
55         TestLinkerMethodWithRange.test(null, Double.valueOf(3.0), null,
56                                        Double.valueOf(37.0), null, Double.valueOf(113.0));
57         TestLinkerMethodWithRange.test(Double.valueOf(1.0), Double.valueOf(2.0),
58                                        Double.valueOf(3.0), Double.valueOf(5.0),
59                                        Double.valueOf(7.0), Double.valueOf(11.0));
60     }
61 
TestLinkerMethodMinimalArguments()62     private static void TestLinkerMethodMinimalArguments() throws Throwable {
63         try {
64             TestLinkerMethodMinimalArguments.test(
65                     TestLinkerMethodMinimalArguments.FAILURE_TYPE_LINKER_METHOD_RETURNS_NULL,
66                     10,
67                     10);
68             assertNotReached();
69         } catch (BootstrapMethodError e) {
70             assertEquals(e.getCause().getClass(), ClassCastException.class);
71         }
72 
73         try {
74             TestLinkerMethodMinimalArguments.test(
75                     TestLinkerMethodMinimalArguments.FAILURE_TYPE_LINKER_METHOD_THROWS, 10, 11);
76             assertNotReached();
77         } catch (BootstrapMethodError e) {
78             assertEquals(e.getCause().getClass(), InstantiationException.class);
79         }
80 
81         try {
82             TestLinkerMethodMinimalArguments.test(
83                     TestLinkerMethodMinimalArguments.FAILURE_TYPE_TARGET_METHOD_THROWS, 10, 12);
84             assertNotReached();
85         } catch (ArithmeticException e) {
86         }
87 
88         TestLinkerMethodMinimalArguments.test(
89                 TestLinkerMethodMinimalArguments.FAILURE_TYPE_NONE, 10, 13);
90     }
91 
main(String[] args)92     public static void main(String[] args) throws Throwable {
93         TestUninitializedCallSite();
94         TestLinkerMethodMinimalArguments();
95         TestLinkerMethodMultipleArgumentTypes();
96         TestLinkerMethodWithRange();
97         TestLinkerUnrelatedBSM.test();
98         TestInvokeCustomWithConcurrentThreads.test();
99         TestInvocationKinds.test();
100         TestDynamicBootstrapArguments.test();
101         TestBadBootstrapArguments.test();
102         TestVariableArityLinkerMethod.test();
103     }
104 }
105