lambda表达式

2018-09-24  本文已影响12人  uranusleon

lambda表达式

wiki

学习目标

How:实现原理

使用代码

package CompilerTestPackage;

import java.util.function.Consumer;

public class LambdaTest {
    public static void main(String[] args)
    {
        Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.getName());
        greeter.accept(new Person("Lambda"));
    }
}

class Person{
    public String name;

    public Person(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return this.name;
    }
}

通过cfr反编译代码

package CompilerTestPackage;

import CompilerTestPackage.Person;
import java.io.PrintStream;
import java.lang.invoke.LambdaMetafactory;
import java.util.function.Consumer;

public class LambdaTest {
    public static void main(String[] args) {
        Consumer<Person> greeter = (Consumer<Person>)LambdaMetafactory.metafactory(null, null, null, (Ljava/lang/Object;)V, lambda$main$0(CompilerTestPackage.Person ), (LCompilerTestPackage/Person;)V)();
        greeter.accept(new Person("Lambda"));
    }

    private static /* synthetic */ void lambda$main$0(Person p) {
        System.out.println("Hello, " + p.getName());
    }
}

字节码层面

Classfile /D:/test/LambdaTest.class
  Last modified 2018-8-5; size 1703 bytes
  MD5 checksum f7703a9885aa7896abfb5a773b708be2
  Compiled from "LambdaTest.java"
public class CompilerTestPackage.LambdaTest
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #16.#38        // java/lang/Object."<init>":()V
   #2 = InvokeDynamic      #0:#44         // #0:accept:()Ljava/util/function/Consumer;
   #3 = Class              #45            // CompilerTestPackage/Person
   #4 = String             #46            // Lambda
   #5 = Methodref          #3.#47         // CompilerTestPackage/Person."<init>":(Ljava/lang/String;)V
   #6 = InterfaceMethodref #48.#49        // java/util/function/Consumer.accept:(Ljava/lang/Object;)V
   #7 = Fieldref           #50.#51        // java/lang/System.out:Ljava/io/PrintStream;
   #8 = Class              #52            // java/lang/StringBuilder
   #9 = Methodref          #8.#38         // java/lang/StringBuilder."<init>":()V
  #10 = String             #53            // Hello,
  #11 = Methodref          #8.#54         // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #12 = Methodref          #3.#55         // CompilerTestPackage/Person.getName:()Ljava/lang/String;
  #13 = Methodref          #8.#56         // java/lang/StringBuilder.toString:()Ljava/lang/String;
  #14 = Methodref          #57.#58        // java/io/PrintStream.println:(Ljava/lang/String;)V
  #15 = Class              #59            // CompilerTestPackage/LambdaTest
  #16 = Class              #60            // java/lang/Object
  #17 = Utf8               <init>
  #18 = Utf8               ()V
  #19 = Utf8               Code
  #20 = Utf8               LineNumberTable
  #21 = Utf8               LocalVariableTable
  #22 = Utf8               this
  #23 = Utf8               LCompilerTestPackage/LambdaTest;
  #24 = Utf8               main
  #25 = Utf8               ([Ljava/lang/String;)V
  #26 = Utf8               args
  #27 = Utf8               [Ljava/lang/String;
  #28 = Utf8               greeter
  #29 = Utf8               Ljava/util/function/Consumer;
  #30 = Utf8               LocalVariableTypeTable
  #31 = Utf8               Ljava/util/function/Consumer<LCompilerTestPackage/Person;>;
  #32 = Utf8               lambda$main$0
  #33 = Utf8               (LCompilerTestPackage/Person;)V
  #34 = Utf8               p
  #35 = Utf8               LCompilerTestPackage/Person;
  #36 = Utf8               SourceFile
  #37 = Utf8               LambdaTest.java
  #38 = NameAndType        #17:#18        // "<init>":()V
  #39 = Utf8               BootstrapMethods
  #40 = MethodHandle       #6:#61         // 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;
  #41 = MethodType         #62            //  (Ljava/lang/Object;)V
  #42 = MethodHandle       #6:#63         // invokestatic CompilerTestPackage/LambdaTest.lambda$main$0:(LCompilerTestPackage/Person;)V
  #43 = MethodType         #33            //  (LCompilerTestPackage/Person;)V
  #44 = NameAndType        #64:#65        // accept:()Ljava/util/function/Consumer;
  #45 = Utf8               CompilerTestPackage/Person
  #46 = Utf8               Lambda
  #47 = NameAndType        #17:#66        // "<init>":(Ljava/lang/String;)V
  #48 = Class              #67            // java/util/function/Consumer
  #49 = NameAndType        #64:#62        // accept:(Ljava/lang/Object;)V
  #50 = Class              #68            // java/lang/System
  #51 = NameAndType        #69:#70        // out:Ljava/io/PrintStream;
  #52 = Utf8               java/lang/StringBuilder
  #53 = Utf8               Hello,
  #54 = NameAndType        #71:#72        // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #55 = NameAndType        #73:#74        // getName:()Ljava/lang/String;
  #56 = NameAndType        #75:#74        // toString:()Ljava/lang/String;
  #57 = Class              #76            // java/io/PrintStream
  #58 = NameAndType        #77:#66        // println:(Ljava/lang/String;)V
  #59 = Utf8               CompilerTestPackage/LambdaTest
  #60 = Utf8               java/lang/Object
  #61 = Methodref          #78.#79        // 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;
  #62 = Utf8               (Ljava/lang/Object;)V
  #63 = Methodref          #15.#80        // CompilerTestPackage/LambdaTest.lambda$main$0:(LCompilerTestPackage/Person;)V
  #64 = Utf8               accept
  #65 = Utf8               ()Ljava/util/function/Consumer;
  #66 = Utf8               (Ljava/lang/String;)V
  #67 = Utf8               java/util/function/Consumer
  #68 = Utf8               java/lang/System
  #69 = Utf8               out
  #70 = Utf8               Ljava/io/PrintStream;
  #71 = Utf8               append
  #72 = Utf8               (Ljava/lang/String;)Ljava/lang/StringBuilder;
  #73 = Utf8               getName
  #74 = Utf8               ()Ljava/lang/String;
  #75 = Utf8               toString
  #76 = Utf8               java/io/PrintStream
  #77 = Utf8               println
  #78 = Class              #81            // java/lang/invoke/LambdaMetafactory
  #79 = NameAndType        #82:#86        // 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;
  #80 = NameAndType        #32:#33        // lambda$main$0:(LCompilerTestPackage/Person;)V
  #81 = Utf8               java/lang/invoke/LambdaMetafactory
  #82 = Utf8               metafactory
  #83 = Class              #88            // java/lang/invoke/MethodHandles$Lookup
  #84 = Utf8               Lookup
  #85 = Utf8               InnerClasses
  #86 = 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;
  #87 = Class              #89            // java/lang/invoke/MethodHandles
  #88 = Utf8               java/lang/invoke/MethodHandles$Lookup
  #89 = Utf8               java/lang/invoke/MethodHandles
{
  public CompilerTestPackage.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 5: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   LCompilerTestPackage/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:accept:()Ljava/util/function/Consumer;
         5: astore_1
         6: aload_1
         7: new           #3                  // class CompilerTestPackage/Person
        10: dup
        11: ldc           #4                  // String Lambda
        13: invokespecial #5                  // Method CompilerTestPackage/Person."<init>":(Ljava/lang/String;)V
        16: invokeinterface #6,  2            // InterfaceMethod java/util/function/Consumer.accept:(Ljava/lang/Object;)V
        21: return
      LineNumberTable:
        line 8: 0
        line 9: 6
        line 10: 21
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      22     0  args   [Ljava/lang/String;
            6      16     1 greeter   Ljava/util/function/Consumer;
      LocalVariableTypeTable:
        Start  Length  Slot  Name   Signature
            6      16     1 greeter   Ljava/util/function/Consumer<LCompilerTestPackage/Person;>;
}
SourceFile: "LambdaTest.java"
InnerClasses:
     public static final #84= #83 of #87; //Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles
BootstrapMethods:
  0: #40 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:
      #41 (Ljava/lang/Object;)V
      #42 invokestatic CompilerTestPackage/LambdaTest.lambda$main$0:(LCompilerTestPackage/Person;)V
      #43 (LCompilerTestPackage/Person;)V

lambda表达式的invokedynamic指令

lambda的机制总结

编译时

运行时

LambdaMetafactory.metafactory()方法

public static CallSite metafactory(MethodHandles.Lookup caller,
                                       String invokedName,
                                       MethodType invokedType,
                                       MethodType samMethodType,
                                       MethodHandle implMethod,
                                       MethodType instantiatedMethodType)
            throws LambdaConversionException {
        AbstractValidatingLambdaMetafactory mf;
        mf = new InnerClassLambdaMetafactory(caller, invokedType,
                                             invokedName, samMethodType,
                                             implMethod, instantiatedMethodType,
                                             false, EMPTY_CLASS_ARRAY, EMPTY_MT_ARRAY);
        mf.validateMetafactoryArgs();
        return mf.buildCallSite();
    }
metafactory方法的参数

查看匿名实现类

IDEA配置JVM参数
package CompilerTestPackage;

import CompilerTestPackage.LambdaTest;
import CompilerTestPackage.Person;
import java.lang.invoke.LambdaForm;
import java.util.function.Consumer;

final class LambdaTest$$Lambda$1
implements Consumer {
    private LambdaTest$$Lambda$1() {
    }

    @LambdaForm.Hidden
    public void accept(Object object) {
        LambdaTest.lambda$main$0((Person)((Person)object));// 导入lambda$main$0方法
    }
}

invokedynamic指令学习

上一篇下一篇

猜你喜欢

热点阅读