Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 1 | // 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. |
| 4 | package invokecustom; |
| 5 | |
| 6 | import java.lang.invoke.CallSite; |
| 7 | import java.lang.invoke.ConstantCallSite; |
| 8 | import java.lang.invoke.MethodHandle; |
| 9 | import java.lang.invoke.MethodHandles; |
| 10 | import java.lang.invoke.MethodType; |
| 11 | |
Mads Ager | ab4dc70 | 2018-09-24 09:43:52 +0200 | [diff] [blame] | 12 | class ArgumentType { |
| 13 | } |
| 14 | |
| 15 | class ReturnType { |
| 16 | } |
| 17 | |
| 18 | interface I { |
| 19 | default ReturnType targetMethodTest4(ArgumentType arg) { |
| 20 | System.out.println("I.targetMethodTest4"); |
| 21 | return new ReturnType(); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | class Middle implements I { |
| 26 | } |
| 27 | |
| 28 | class Sub extends Middle { |
| 29 | } |
| 30 | |
| 31 | interface I2 { |
| 32 | default ReturnType targetMethodTest5(ArgumentType arg) { |
| 33 | System.out.println("I2.targetMethodTest5"); |
| 34 | return new ReturnType(); |
| 35 | } |
| 36 | } |
| 37 | |
Mads Ager | ab4dc70 | 2018-09-24 09:43:52 +0200 | [diff] [blame] | 38 | class 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 Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 45 | |
| 46 | public 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 Peltier | cfd6dac | 2017-10-10 13:45:55 +0200 | [diff] [blame] | 60 | private static void targetMethodTest3(MethodType mt) |
| 61 | throws Throwable { |
| 62 | System.out.println("MethodType: " + mt.toString()); |
| 63 | } |
| 64 | |
Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 65 | 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 Ager | ab4dc70 | 2018-09-24 09:43:52 +0200 | [diff] [blame] | 71 | |
| 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 Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 79 | } |