(2)invokedynamic指令 https://www.cnblogs.com/wade-luffy/p/6058087.html
public class InDyTest { public static void main(String[] args) { Runnable x = () -> { //System.out.println("Hello, World!"); }; } }
生成的Class结构如下:
Classfile /C:/Users/mazhi/Desktop/InDyTest.class Last modified 2018-7-5; size 986 bytes MD5 checksum 03d0c485e603b294e8de271d879c7b8f Compiled from "InDyTest.java" public class com.test01.InDyTest SourceFile: "InDyTest.java" BootstrapMethods: 0: #34 invokestatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/la ng/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Lj ava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/Meth odType;)Ljava/lang/invoke/CallSite; Method arguments: #35 ()V #38 invokestatic com/test01/InDyTest.lambda$0:()V #39 ()V InnerClasses: public static final #45= #41 of #43; //Lookup=class java/lang/invoke/Meth odHandles$Lookup of class java/lang/invoke/MethodHandles minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Class #2 // com/test01/InDyTest #2 = Utf8 com/test01/InDyTest #3 = Class #4 // java/lang/Object #4 = Utf8 java/lang/Object #5 = Utf8 <init> #6 = Utf8 ()V #7 = Utf8 Code #8 = Methodref #3.#9 // java/lang/Object."<init>":()V #9 = NameAndType #5:#6 // "<init>":()V #10 = Utf8 LineNumberTable #11 = Utf8 LocalVariableTable #12 = Utf8 this #13 = Utf8 Lcom/test01/InDyTest; #14 = Utf8 main #15 = Utf8 ([Ljava/lang/String;)V #16 = NameAndType #17:#18 // run:()Ljava/lang/Runnable; #17 = Utf8 run #18 = Utf8 ()Ljava/lang/Runnable; #19 = InvokeDynamic #0:#16 // #0:run:()Ljava/lang/Runnable; #20 = Utf8 args #21 = Utf8 [Ljava/lang/String; #22 = Utf8 x #23 = Utf8 Ljava/lang/Runnable; #24 = Utf8 lambda$0 #25 = Utf8 SourceFile #26 = Utf8 InDyTest.java #27 = Utf8 BootstrapMethods #28 = Methodref #29.#31 // java/lang/invoke/LambdaMetafactory .metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lan g/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle; Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; #29 = Class #30 // java/lang/invoke/LambdaMetafactory #30 = Utf8 java/lang/invoke/LambdaMetafactory #31 = NameAndType #32:#33 // metafactory:(Ljava/lang/invoke/Met hodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invo ke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava /lang/invoke/CallSite; #32 = Utf8 metafactory #33 = Utf8 (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/St ring;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke /MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; #34 = MethodHandle #6:#28 // invokestatic java/lang/invoke/Lamb daMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/Str ing;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/ MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; #35 = MethodType #6 // ()V #36 = Methodref #1.#37 // com/test01/InDyTest.lambda$0:()V #37 = NameAndType #24:#6 // lambda$0:()V #38 = MethodHandle #6:#36 // invokestatic com/test01/InDyTest.l ambda$0:()V #39 = MethodType #6 // ()V #40 = Utf8 InnerClasses #41 = Class #42 // java/lang/invoke/MethodHandles$Loo kup #42 = Utf8 java/lang/invoke/MethodHandles$Lookup #43 = Class #44 // java/lang/invoke/MethodHandles #44 = Utf8 java/lang/invoke/MethodHandles #45 = Utf8 Lookup { public com.test01.InDyTest(); flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #8 // Method java/lang/Object."<init> ":()V 4: return LineNumberTable: line 4: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this Lcom/test01/InDyTest; public static void main(java.lang.String[]); flags: ACC_PUBLIC, ACC_STATIC Code: stack=1, locals=2, args_size=1 0: invokedynamic #19, 0 // InvokeDynamic #0:run:()Ljava/la ng/Runnable; 5: astore_1 6: return LineNumberTable: line 7: 0 line 10: 6 LocalVariableTable: Start Length Slot Name Signature 0 7 0 args [Ljava/lang/String; 6 1 1 x Ljava/lang/Runnable; }
import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodType; import static java.lang.invoke.MethodHandles.lookup; public class TestMethod { static class ClassA { public void println(String s) { System.out.println(s); } } public static void main(String[] args) throws Throwable { Object obj = System.currentTimeMillis() % 2 == 0 ? System.out : new ClassA(); /* * 无论obj最终是哪个实现类,下面这句都能正确调用到println方法 **/ getPrintlnMH(obj).invokeExact("icyfenix"); } private static MethodHandle getPrintlnMH(Object reveiver) throws Throwable { /* * MethodType:代表“方法类型”,包含了方法的返回值(methodType()的第一个参数) * 和具体参数(methodType() 第二个及以后的参数) */ MethodType mt = MethodType.methodType(void.class, String.class); /* * lookup()方法来自于MethodHandles.lookup,这句的作用是在指定类中查找符合给定的方法名称、方法类型, * 并且符合调用权限的方法句柄 * 因为这里调用的是一个虚方法,按照Java语言的规则,方法第一个参数是隐式的,代表该方法的接收者,也即是this指向的对象, * 这个参数以前是放在参数列表中进行传递的,而现在提供了bindTo()方法来完成这件事情 */ return lookup().findVirtual(reveiver.getClass(), "println", mt).bindTo(reveiver); } }
import static java.lang.invoke.MethodHandles.lookup; import java.lang.invoke.CallSite; import java.lang.invoke.ConstantCallSite; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; public class InvokeDynamicTest { public static void main(String[] args) throws Throwable { INDY_BootstrapMethod().invokeExact("icyfenix"); } public static void testMethod(String s) { System.out.println("hello String:" + s); } public static CallSite BootstrapMethod( MethodHandles.Lookup lookup, String name, MethodType mt) throws Throwable { return new ConstantCallSite( lookup.findStatic(InvokeDynamicTest.class, name, mt)); } private static MethodType MT_BootstrapMethod() { return MethodType.fromMethodDescriptorString( "(Ljava/lang/invoke/MethodHandles $Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;", null); } private static MethodHandle MH_BootstrapMethod() throws Throwable { return lookup().findStatic( InvokeDynamicTest.class, "BootstrapMethod", MT_BootstrapMethod()); } private static MethodHandle INDY_BootstrapMethod() throws Throwable { CallSite cs = (CallSite) MH_BootstrapMethod().invokeWithArguments( lookup(), "testMethod", MethodType.fromMethodDescriptorString("(Ljava/lang/String;)V", null)); return cs.dynamicInvoker(); } }
import static java.lang.invoke.MethodHandles.lookup; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodType; public class Test02 { class GrandFather { void thinking() { System.out.println("i am grandfather"); } } class Father extends GrandFather { void thinking() { System.out.println("i am father"); } } class Son extends Father { void thinking() { try { MethodType mt = MethodType.methodType(void.class); MethodHandle mh = lookup().findVirtual( GrandFather.class, "thinking", mt) .bindTo(new Test02().new GrandFather()); mh.invokeExact(); } catch (Throwable e) { } } } public static void main(String[] args) { (new Test02().new Son()).thinking(); } }