小王经过上面一系列学习,开始思考Lambda的原理是什么,因为Java8中每一个Lambda表达式必须有一个函数式接口与之对应,小王就思考经过编译器编译以后到可能实现的方式有两种,一种生成实现接口的类,另外一种是内部类,于是决定看一下反编译的以后代码,以解除心中的疑惑;
@FunctionalInterfacepublic interface Func {
int add(int x, int y);
}
public class LambdaTest {
public static void main(String[] args) {
Func func = (x, y) -> x + y;
System.out.println(func.add(1, 2));
}
}
通过javap -p -v -c LambdaTest.class查看反编译后的代码,
Classfile /Users/wangtongzhou/Documents/Java/learning/target/classes/com/springboot2/learning/javabasic/java8/LambdaTest.classLast modified 2020-7-11; size 1392 bytes
MD5 checksum ec7d77a8b0b0a0cb5940f80a9b27b3d0
Compiled from "LambdaTest.java"
public class com.springboot2.learning.javabasic.java8.LambdaTest
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #7.#29 // java/lang/Object."<init>":()V
#2 = InvokeDynamic #0:#34 // #0:add:()Lcom/springboot2/learning/javabasic/java8/Func;
#3 = Fieldref #35.#36 // java/lang/System.out:Ljava/io/PrintStream;
#4 = InterfaceMethodref #37.#38 // com/springboot2/learning/javabasic/java8/Func.add:(II)I
#5 = Methodref #39.#40 // java/io/PrintStream.println:(I)V
#6 = Class #41 // com/springboot2/learning/javabasic/java8/LambdaTest
#7 = Class #42 // java/lang/Object
#8 = Utf8 <init>
#9 = Utf8 ()V
#10 = Utf8 Code
#11 = Utf8 LineNumberTable
#12 = Utf8 LocalVariableTable
#13 = Utf8 this
#14 = Utf8 Lcom/springboot2/learning/javabasic/java8/LambdaTest;
#15 = Utf8 main
#16 = Utf8 ([Ljava/lang/String;)V
#17 = Utf8 args
#18 = Utf8 [Ljava/lang/String;
#19 = Utf8 func
#20 = Utf8 Lcom/springboot2/learning/javabasic/java8/Func;
#21 = Utf8 MethodParameters
#22 = Utf8 lambda$main$0
#23 = Utf8 (II)I
#24 = Utf8 x
#25 = Utf8 I
#26 = Utf8 y
#27 = Utf8 SourceFile
#28 = Utf8 LambdaTest.java
#29 = NameAndType #8:#9 // "<init>":()V
#30 = Utf8 BootstrapMethods
#31 = MethodHandle #6:#43 // invokestatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
#32 = MethodType #23 // (II)I
#33 = MethodHandle #6:#44 // invokestatic com/springboot2/learning/javabasic/java8/LambdaTest.lambda$main$0:(II)I
#34 = NameAndType #45:#46 // add:()Lcom/springboot2/learning/javabasic/java8/Func;
#35 = Class #47 // java/lang/System
#36 = NameAndType #48:#49 // out:Ljava/io/PrintStream;
#37 = Class #50 // com/springboot2/learning/javabasic/java8/Func
#38 = NameAndType #45:#23 // add:(II)I
#39 = Class #51 // java/io/PrintStream
#40 = NameAndType #52:#53 // println:(I)V
#41 = Utf8 com/springboot2/learning/javabasic/java8/LambdaTest
#42 = Utf8 java/lang/Object
#43 = Methodref #54.#55 // java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
#44 = Methodref #6.#56 // com/springboot2/learning/javabasic/java8/LambdaTest.lambda$main$0:(II)I
#45 = Utf8 add
#46 = Utf8 ()Lcom/springboot2/learning/javabasic/java8/Func;
#47 = Utf8 java/lang/System
#48 = Utf8 out
#49 = Utf8 Ljava/io/PrintStream;
#50 = Utf8 com/springboot2/learning/javabasic/java8/Func
#51 = Utf8 java/io/PrintStream
#52 = Utf8 println
#53 = Utf8 (I)V
#54 = Class #57 // java/lang/invoke/LambdaMetafactory
#55 = NameAndType #58:#62 // metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
#56 = NameAndType #22:#23 // lambda$main$0:(II)I
#57 = Utf8 java/lang/invoke/LambdaMetafactory
#58 = Utf8 metafactory
#59 = Class #64 // java/lang/invoke/MethodHandles$Lookup
#60 = Utf8 Lookup
#61 = Utf8 InnerClasses
#62 = Utf8 (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
#63 = Class #65 // java/lang/invoke/MethodHandles
#64 = Utf8 java/lang/invoke/MethodHandles$Lookup
#65 = Utf8 java/lang/invoke/MethodHandles
{
public com.springboot2.learning.javabasic.java8.LambdaTest();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/springboot2/learning/javabasic/java8/LambdaTest;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=4, locals=2, args_size=1
0: invokedynamic #2, 0 // InvokeDynamic #0:add:()Lcom/springboot2/learning/javabasic/java8/Func;
5: astore_1
6: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
9: aload_1
10: iconst_1
11: iconst_2
12: invokeinterface #4, 3 // InterfaceMethod com/springboot2/learning/javabasic/java8/Func.add:(II)I
17: invokevirtual #5 // Method java/io/PrintStream.println:(I)V
20: return
LineNumberTable:
line 5: 0
line 6: 6
line 7: 20
LocalVariableTable:
Start Length Slot Name Signature
0 21 0 args [Ljava/lang/String;
6 15 1 func Lcom/springboot2/learning/javabasic/java8/Func;
MethodParameters:
Name Flags
args
private static int lambda$main$0(int, int);
descriptor: (II)I
flags: ACC_PRIVATE, ACC_STATIC, ACC_SYNTHETIC
Code:
stack=2, locals=2, args_size=2
0: iload_0
1: iload_1
2: iadd
3: ireturn
LineNumberTable:
line 5: 0
LocalVariableTable:
Start Length Slot Name Signature
0 4 0 x I
0 4 1 y I
MethodParameters:
Name Flags
x synthetic
y synthetic
}
SourceFile: "LambdaTest.java"
InnerClasses:
public static final #60= #59 of #63; //Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles
BootstrapMethods:
0: #31 invokestatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
Method arguments:
#32 (II)I
#33 invokestatic com/springboot2/learning/javabasic/java8/LambdaTest.lambda$main$0:(II)I
反编译以后lambda表达式被编译成为一个lambda$main$0的函数,其实就是一段(x, y) -> x + y的方法,在看main方法主要分为以下8个步骤:
通过invokedynamic指令生成调用对象;
存入本地缓存;
加载java.lang.System.out静态方法;
将lambda表达式生成的对象加载入执行栈;
将int类型1加载入执行栈;
将int类型2加载入执行栈;
执行lambda表达式生成的对象的add方法;