在 Java 字节码中如何翻译 Lambda 表达式 [英] How Lambda Expressions Are Translate In Java Byte Code

查看:21
本文介绍了在 Java 字节码中如何翻译 Lambda 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Java 中的 lambda 表达式创建一个示例,并且我正在使用官方的 JDK8.我的例子运行成功.但是当我试图检查编译器如何将 lambda 表达式转换为字节码时,这让我有些困惑.以下是我的示例代码:-

I am trying to create an example using lambda expression in java and i am using offical JDK8. My example was run successfully. But when i trying to check how the compiler translate lambda expression into byte code, this makes me some confusion.Following is the code of my example:-

public class LambdaTest {
    public Integer lambdaBinaryOpertor(BinaryOperator<Integer> binaryOperator) {
        return binaryOperator.apply(60, 72);
    }

    public static void main(String[] args) {
        LambdaTest test = new LambdaTest();
        BinaryOperator<Integer> binaryOperator = (a, b) -> a*b;
        System.out.println("Additon using Lambda BinaryOperator: "+test.lambdaBinaryOpertor(binaryOperator));
    }
}

在这篇文章中,他们讨论了编译器如何将 lambda 表达式转换为字节码.根据本文档将lambda表达式转换为static方法和lambda表达式声明的位置,参考lambdastatic方法.下面的例子在文章中:

In this Article, they discuss about how compiler translate the lambda expressions into byte code. According to this document the lambda expression convert into static method and the location where lambda expression declare, have reference of lambda static method. The following example is in the article :

//Source code
class A {
    public void foo() {
        List<String> list = ...
        list.forEach( s -> { System.out.println(s); } );
    }
} 

//After compile above code "translate code " 
class A {
    public void foo() {
        List<String> list = ...
        list.forEach( [lambda for lambda$1 as Block] );
    }

    static void lambda$1(String s) {
        System.out.println(s);
    }
}

我的示例运行良好,并为我们提供了适当的结果.但是当我尝试运行 javap 命令来检查类的字节码时,字节码中没有 lambda 的静态方法.

My example run fine and give us the appropriate result. But when i trying to run the javap command for check the byte code of class, there is no static method for lambdas in byte code.

c:>javap LambdaTest
Compiled from "LambdaTest.java"
public class LambdaTest {
public LambdaTest();
public java.lang.Integer lambdaBinaryOpertor(java.util.function.BinaryOperator <java.lang.Integer>);
public static void main(java.lang.String[]);
}

在泛型的情况下,桥接方法是由编译器创建的,我们也会使用 javap 命令检查这个方法,但在 lambdas 的情况下,没有 static方法.这篇文章发表于 2012 年,Java 8 于 2014 年 3 月发布.所以我对 lambda 的跨国有一些疑问:

In the case of generics, bridge method is created by compiler and we will also check this method using javap command, but in the case of lambdas there is no static method. The article was published at 2012 and java 8 was launch at march 2014. So i have some queries regarding transnational of lambda:

  1. 在本文发布后,JDK 8 中是否有任何用于 lambda 的新实现,否则我会在检查 lambda 方法时出错?
  2. 编译器究竟是如何处理 lambda 表达式的?
  3. JVM 如何调用 lambda 表达式?

推荐答案

使用 javap 附加参数打印有关类的完整信息:javap -v -p -s -c

Use javap additional arguments to print full information about class: javap -v -p -s -c

对于您的示例,lambda 的源代码将是:

For your example, source code of lambda will be:

private static java.lang.Integer lambda$main$0(java.lang.Integer, java.lang.Integer);
    descriptor: (Ljava/lang/Integer;Ljava/lang/Integer;)Ljava/lang/Integer;
    flags: ACC_PRIVATE, ACC_STATIC, ACC_SYNTHETIC
    Code:
      stack=2, locals=2, args_size=2
         0: aload_0       
         1: invokevirtual #17                 // Method java/lang/Integer.intValue:()I
         4: aload_1       
         5: invokevirtual #17                 // Method java/lang/Integer.intValue:()I
         8: imul          
         9: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
        12: areturn       
      LineNumberTable:
        line 10: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      13     0     a   Ljava/lang/Integer;
            0      13     1     b   Ljava/lang/Integer;
}

这篇关于在 Java 字节码中如何翻译 Lambda 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆