什么是CONSTANT_MethodHandle,CONSTANT_MethodType和CONSTANT_InvokeDynamic? [英] What are CONSTANT_MethodHandle, CONSTANT_MethodType, and CONSTANT_InvokeDynamic?

查看:202
本文介绍了什么是CONSTANT_MethodHandle,CONSTANT_MethodType和CONSTANT_InvokeDynamic?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Java字节码的工作方式,然后从Wikipedia开始。在页面上,重点关注 .class 文件,其中提到了出现在常量池中的11种常量类型。但是,在 Java虚拟机规范(JVMS),它还提到了3个:

I was looking into how the Java bytecode worked, and I started on Wikipedia. On the page focusing on the .class file, it mentions 11 constant types that appear in the Constant Pool. However, in The Java Virtual Machine Specifications (JVMS) it mentions 3 more:


  • CONSTANT_MethodHandle

  • CONSTANT_MethodType

  • CONSTANT_InvokeDynamic

在Wikipedia页面上提到的11似乎很不言自明,我理解他们的目的和用。但是,我对JVMS中描述的3个附加功能感到困惑。它们是什么,它们的用途是什么?

The 11 mentioned on the Wikipedia page seem fairly self-explanatory, and I understand their purpose and use. However, I'm confused by the 3 extra described in the JVMS. What are they, and for what are they used?

推荐答案

MethodHandle基本上是可执行操作的句柄。它可以是方法调用,甚至可以是变量分配。 MethodType是用于调用MethodHandle的调用签名。换句话说,它描述了参数和返回值。可以使用不同的类型签名来处理MethodHandle。

MethodHandle is basically a "handle" to an executable operation. It can be a method call or even a variable assignment. MethodType is the calling signature used to call the MethodHandle. In other words, it describes the parameters and return value. A MethodHandle may be able to process being called with different type signatures.

要在此处进行处理,请查看 java.lang.invoke.MethodHandle java.lang.invoke.MethodType 。这些是在Java 7中引入的。当字节码使用LDC将MethodHandle_info或MethodType_info常量加载到堆栈上时,JVM将分别创建MethodHandle或MethodType类实例。

To play around with there, look at java.lang.invoke.MethodHandle and java.lang.invoke.MethodType. These were introduced in Java 7. When bytecode uses LDC to load a MethodHandle_info or MethodType_info constant onto the stack, the JVM creates a MethodHandle or MethodType class instance, respectively.

invokedynamic字节码指令将调用MethodHandle。 bootstrap方法将在 first 调用上使用,以确定它将调用什么MethodHandle。这将创建一个可以在运行时更改其调用目标的调用指令。这样,当目标代码的某些方面发生变化时,动态代码便无需重新编译。在Java 8中使用它来提供对lambda表达式的支持。

The invokedynamic bytecode instruction will invoke a MethodHandle. The bootstrap method will be used on the first invocation to determine what MethodHandle it will call. This creates an invoke instruction that can change its call target at runtime. This allows for dynamic code that doesn't need to recompile when certain aspects of its target code change. This is used in Java 8 to provide support for lambda expressions.

这篇关于什么是CONSTANT_MethodHandle,CONSTANT_MethodType和CONSTANT_InvokeDynamic?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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