Java Lambda学习笔记之函数式是个什么玩意

废话不说上源码

1.main方法

public static void main(String[] args) {
        Function s = Integer::parseInt;
        s.apply("10");
    }

::具体看下一节

2.反编译一下

javap -v -p StreamTest

结果如下

警告: 二进制文件StreamTest包含com.cui.subject.java.function.StreamTest
Classfile /E:/WorkSpace/subject/java/target/classes/com/cui/subject/java/function/StreamTest.class
  Last modified 2020-6-15; size 1311 bytes
  MD5 checksum 7b5bc92830d2402bc0d88bfe91926d3e
  Compiled from "StreamTest.java"
public class com.cui.subject.java.function.StreamTest
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #6.#24         // java/lang/Object."":()V
   #2 = InvokeDynamic      #0:#30         // #0:apply:()Ljava/util/function/Function;
   #3 = String             #31            // 10
   #4 = InterfaceMethodref #32.#33        // java/util/function/Function.apply:(Ljava/lang/Object;)Ljava/lang/Object;
   #5 = Class              #34            // com/cui/subject/java/function/StreamTest
   #6 = Class              #35            // java/lang/Object
   #7 = Utf8               
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               LocalVariableTable
  #12 = Utf8               this
  #13 = Utf8               Lcom/cui/subject/java/function/StreamTest;
  #14 = Utf8               main
  #15 = Utf8               ([Ljava/lang/String;)V
  #16 = Utf8               args
  #17 = Utf8               [Ljava/lang/String;
  #18 = Utf8               s
  #19 = Utf8               Ljava/util/function/Function;
  #20 = Utf8               LocalVariableTypeTable
  #21 = Utf8               Ljava/util/function/Function;
  #22 = Utf8               SourceFile
  #23 = Utf8               StreamTest.java
  #24 = NameAndType        #7:#8          // "":()V
  #25 = Utf8               BootstrapMethods
  #26 = MethodHandle       #6:#36         // 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;
  #27 = MethodType         #37            //  (Ljava/lang/Object;)Ljava/lang/Object;
  #28 = MethodHandle       #6:#38         // invokestatic java/lang/Integer.parseInt:(Ljava/lang/String;)I
  #29 = MethodType         #39            //  (Ljava/lang/String;)Ljava/lang/Integer;
  #30 = NameAndType        #40:#41        // apply:()Ljava/util/function/Function;
  #31 = Utf8               10
  #32 = Class              #42            // java/util/function/Function
  #33 = NameAndType        #40:#37        // apply:(Ljava/lang/Object;)Ljava/lang/Object;
  #34 = Utf8               com/cui/subject/java/function/StreamTest
  #35 = Utf8               java/lang/Object
  #36 = Methodref          #43.#44        // 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;
  #37 = Utf8               (Ljava/lang/Object;)Ljava/lang/Object;
  #38 = Methodref          #45.#46        // java/lang/Integer.parseInt:(Ljava/lang/String;)I
  #39 = Utf8               (Ljava/lang/String;)Ljava/lang/Integer;
  #40 = Utf8               apply
  #41 = Utf8               ()Ljava/util/function/Function;
  #42 = Utf8               java/util/function/Function
  #43 = Class              #47            // java/lang/invoke/LambdaMetafactory
  #44 = NameAndType        #48:#52        // 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;
  #45 = Class              #53            // java/lang/Integer
  #46 = NameAndType        #54:#55        // parseInt:(Ljava/lang/String;)I
  #47 = Utf8               java/lang/invoke/LambdaMetafactory
  #48 = Utf8               metafactory
  #49 = Class              #57            // java/lang/invoke/MethodHandles$Lookup
  #50 = Utf8               Lookup
  #51 = Utf8               InnerClasses
  #52 = 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;
  #53 = Utf8               java/lang/Integer
  #54 = Utf8               parseInt
  #55 = Utf8               (Ljava/lang/String;)I
  #56 = Class              #58            // java/lang/invoke/MethodHandles
  #57 = Utf8               java/lang/invoke/MethodHandles$Lookup
  #58 = Utf8               java/lang/invoke/MethodHandles
{
  public com.cui.subject.java.function.StreamTest();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."":()V
         4: return
      LineNumberTable:
        line 11: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/cui/subject/java/function/StreamTest;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=2, args_size=1
         0: invokedynamic #2,  0              // InvokeDynamic #0:apply:()Ljava/util/function/Function;
         5: astore_1
         6: aload_1
         7: ldc           #3                  // String 10
         9: invokeinterface #4,  2            // InterfaceMethod java/util/function/Function.apply:(Ljava/lang/Object;)Ljava/lang/Object;
        14: pop
        15: return
      LineNumberTable:
        line 13: 0
        line 14: 6
        line 17: 15
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      16     0  args   [Ljava/lang/String;
            6      10     1     s   Ljava/util/function/Function;
      LocalVariableTypeTable:
        Start  Length  Slot  Name   Signature
            6      10     1     s   Ljava/util/function/Function;
}
SourceFile: "StreamTest.java"
InnerClasses:
     public static final #50= #49 of #56; //Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles
BootstrapMethods:
  0: #26 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:
      #27 (Ljava/lang/Object;)Ljava/lang/Object;
      #28 invokestatic java/lang/Integer.parseInt:(Ljava/lang/String;)I
      #29 (Ljava/lang/String;)Ljava/lang/Integer;

3.invokedynamic

 #2 = InvokeDynamic      #0:#30         // #0:apply:()Ljava/util/function/Function;

3.1 0在这里

BootstrapMethods:
  0: #26 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:
      #27 (Ljava/lang/Object;)Ljava/lang/Object;
      #28 invokestatic java/lang/Integer.parseInt:(Ljava/lang/String;)I
      #29 (Ljava/lang/String;)Ljava/lang/Integer;

BootstrapMethods

每一个invokedynamic指令的实例叫做一个动态调用点(dynamic call site), 动态调用点最开始是未链接状态(unlinked:表示还未指定该调用点要调用的方法), 动态调用点依靠引导方法来链接到具体的方法. 引导方法是由编译器生成, 在运行期当JVM第一次遇到invokedynamic指令时, 会调用引导方法来将invokedynamic指令所指定的名字(方法名,方法签名)和具体的执行代码(目标方法)链接起来, 引导方法的返回值永久的决定了调用点的行为.引导方法的返回值类型是java.lang.invoke.CallSite, 一个invokedynamic指令关联一个CallSite, 将所有的调用委托到CallSite当前的target(MethodHandle)

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();
    }
  1. MethodHandles.Lookup caller : 代表查找上下文与调用者的访问权限, 使用invokedynamic指令时, JVM会自动自动填充这个参数
  2. String invokedName : 要实现的方法的名字, 使用invokedynamic时, JVM自动帮我们填充(填充内容来自常量池InvokeDynamic.NameAndType.Name), 在这里JVM为我们填充为 "apply", 即Consumer.accept方法名.
  3. MethodType invokedType : 调用点期望的方法参数的类型和返回值的类型(方法signature). 使用invokedynamic指令时, JVM会自动自动填充这个参数(填充内容来自常量池InvokeDynamic.NameAndType.Type), 在这里参数为String, 返回值类型为Consumer, 表示这个调用点的目标方法的参数为String, 然后invokedynamic执行完后会返回一个即Consumer实例.
  4. MethodType samMethodType : 函数对象将要实现的接口方法类型, 这里运行时, 值为 (Object)Object 即 Consumer.accept方法的类型(泛型信息被擦除).#67 (Ljava/lang/Object;)V
  5. MethodHandle implMethod : 一个直接方法句柄(DirectMethodHandle), 描述在调用时将被执行的具体实现方法 (包含适当的参数适配, 返回类型适配, 和在调用参数前附加上捕获的参数), 在这里为 #70 invokestatic Lambda.lambda$0:(Ljava/lang/String;)V 方法的方法句柄.
  6. MethodType instantiatedMethodType : 函数接口方法替换泛型为具体类型后的方法类型, 通常和 samMethodType 一样, 不同的情况为泛型:

4 5 6 三个参数

  #27 (Ljava/lang/Object;)Ljava/lang/Object;
      #28 invokestatic java/lang/Integer.parseInt:(Ljava/lang/String;)I
      #29 (Ljava/lang/String;)Ljava/lang/Integer;
//此处会创建一个内部类
return mf.buildCallSite();

生成的内部类如下
加上这句话

System.setProperty("jdk.internal.lambda.dumpProxyClasses", "E:\\WorkSpace\\subject\\lambda\\");

会在指定目录生成内部类,如下

final class StreamTest$$Lambda$1 implements Function {
    private StreamTest$$Lambda$1() {
    }

    @Hidden
    public Object apply(Object var1) {
        return Integer.parseInt((String)var1);
    }
}

如果是这样的代码

 new Thread(() -> System.out.println("线程:" + Thread.currentThread().getId())).start();

生成的内部类长这样

final class StreamTest$$Lambda$1 implements Runnable {
    private StreamTest$$Lambda$1() {
    }

    @Hidden
    public void run() {
        StreamTest.lambda$main$0();
    }
}

这样呢

 Consumer consumer = Integer::parseInt;

就长这样

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

    @Hidden
    public void accept(Object var1) {
        Integer.parseInt((String)var1);
    }
}

这样呢

 public static void main(String[] args) {
        Func add = (x, y) -> x + y;
        System.out.println(add.exec(1, 2));
    }

    @FunctionalInterface
    interface Func {
        int exec(int x, int y);
    }

就长这样

final class StreamTest$$Lambda$1 implements Func {
    private StreamTest$$Lambda$1() {
    }

    @Hidden
    public int exec(int var1, int var2) {
        return StreamTest.lambda$main$0(var1, var2);
    }
}

3.2 30在这

代表执行哪个方法

#30 = NameAndType        #40:#41        // apply:()Ljava/util/function/Function;

总结

Lambda表达式,使用invokedynamic指令,运行时调用LambdaMetafactory.metafactory动态的生成内部类,实现了接口
内部类里的调用方法块并不是动态生成的,只是在原class里已经编译生成了一个静态的方法,内部类只需要调用该静态方法

你可能感兴趣的:(lambda,java)