blob: 76fed75313afc7f8ce1fe5c6b10b5fd280ea32f7 [file] [log] [blame]
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +02001// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4package invokecustom;
5
6import java.lang.invoke.CallSite;
7import java.lang.invoke.ConstantCallSite;
8import java.lang.invoke.MethodHandle;
9import java.lang.invoke.MethodHandles;
10import java.lang.invoke.MethodType;
11
Mads Agerab4dc702018-09-24 09:43:52 +020012class ArgumentType {
13}
14
15class ReturnType {
16}
17
18interface I {
19 default ReturnType targetMethodTest4(ArgumentType arg) {
20 System.out.println("I.targetMethodTest4");
21 return new ReturnType();
22 }
23}
24
25class Middle implements I {
26}
27
28class Sub extends Middle {
29}
30
31interface I2 {
32 default ReturnType targetMethodTest5(ArgumentType arg) {
33 System.out.println("I2.targetMethodTest5");
34 return new ReturnType();
35 }
36}
37
Mads Agerab4dc702018-09-24 09:43:52 +020038class Impl implements I2 {
39 @Override
40 public ReturnType targetMethodTest5(ArgumentType arg) {
41 System.out.println("Impl.targetMethodTest5");
42 return new ReturnType();
43 }
44}
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +020045
46public class InvokeCustom {
47
48 private static String staticField1 = "StaticField1";
49
50 private static void targetMethodTest1() {
51 System.out.println("Hello World!");
52 }
53
54 private static void targetMethodTest2(MethodHandle mhInvokeStatic, MethodHandle mhGetStatic)
55 throws Throwable {
56 mhInvokeStatic.invokeExact();
57 System.out.println(mhGetStatic.invoke());
58 }
59
Mikaël Peltiercfd6dac2017-10-10 13:45:55 +020060 private static void targetMethodTest3(MethodType mt)
61 throws Throwable {
62 System.out.println("MethodType: " + mt.toString());
63 }
64
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +020065 public static CallSite bsmLookupStatic(MethodHandles.Lookup caller, String name, MethodType type)
66 throws NoSuchMethodException, IllegalAccessException {
67 final MethodHandles.Lookup lookup = MethodHandles.lookup();
68 final MethodHandle targetMH = lookup.findStatic(lookup.lookupClass(), name, type);
69 return new ConstantCallSite(targetMH.asType(type));
70 }
Mads Agerab4dc702018-09-24 09:43:52 +020071
72 public static void doInvokeSubWithArg(MethodHandle handle) throws Throwable {
73 handle.invoke(new Sub(), new ArgumentType());
74 }
75
76 public static void doInvokeExactImplWithArg(MethodHandle handle) throws Throwable {
77 ReturnType result = (ReturnType) handle.invokeExact(new Impl(), new ArgumentType());
78 }
Mikaël Peltier7b7b53a2017-10-09 13:33:21 +020079}