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

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

问题描述

我正在尝试使用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表达式声明的位置,引用lambda static 方法。以下示例位于文章中:

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 命令来检查类的字节代码时,字节代码中没有lambdas的静态方法。

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[]);
}

在泛型的情况下,bridge方法由编译器创建,我们也将使用 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 exression?


推荐答案

使用 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;
}

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

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