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 | |
| 12 | |
| 13 | public class InvokeCustom { |
| 14 | |
| 15 | private static String staticField1 = "StaticField1"; |
| 16 | |
| 17 | private static void targetMethodTest1() { |
| 18 | System.out.println("Hello World!"); |
| 19 | } |
| 20 | |
| 21 | private static void targetMethodTest2(MethodHandle mhInvokeStatic, MethodHandle mhGetStatic) |
| 22 | throws Throwable { |
| 23 | mhInvokeStatic.invokeExact(); |
| 24 | System.out.println(mhGetStatic.invoke()); |
| 25 | } |
| 26 | |
Mikaël Peltier | cfd6dac | 2017-10-10 13:45:55 +0200 | [diff] [blame] | 27 | private static void targetMethodTest3(MethodType mt) |
| 28 | throws Throwable { |
| 29 | System.out.println("MethodType: " + mt.toString()); |
| 30 | } |
| 31 | |
Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 32 | public static CallSite bsmLookupStatic(MethodHandles.Lookup caller, String name, MethodType type) |
| 33 | throws NoSuchMethodException, IllegalAccessException { |
| 34 | final MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 35 | final MethodHandle targetMH = lookup.findStatic(lookup.lookupClass(), name, type); |
| 36 | return new ConstantCallSite(targetMH.asType(type)); |
| 37 | } |
Mikaël Peltier | 7b7b53a | 2017-10-09 13:33:21 +0200 | [diff] [blame] | 38 | } |