MethodHandle - 这是什么一回事? [英] MethodHandle - What is it all about?

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

问题描述

我正在研究JDK 1.7的新功能,我无法得到MethodHandle的设计用途?我理解(直接)静态方法的调用(以及在这种情况下直接使用Core Reflection API)。我也理解(直接)调用虚方法(非静态,非最终)(并使用Core Reflection API,需要通过Class的层次结构 obj.getClass()。getSuperclass())。调用非虚方法可以视为前者的特殊情况。

I am studying new features of JDK 1.7 and I just can't get it what MethodHandle is designed for? I understand (direct) invocation of the static method (and use of Core Reflection API that is straightforward in this case). I understand also (direct) invocation of the virtual method (non-static, non-final) (and use of Core Reflection API that requires going through Class's hierarchy obj.getClass().getSuperclass()). Invocation of non-virtual method can be treated as special case of the former one.

是的,我知道存在重载问题。如果要调用方法,则必须提供确切的签名。你不能以简单的方式检查重载方法。

Yes, I aware that there is an issue with overloading. If you want to invoke method you have to supply the exact signature. You can't check for overloaded method in easy way.

但是,MethodHandle是关于什么的? Reflection API允许您查看对象内部,而无需任何预先假设(如实现接口)。您可以出于某种目的检查对象。但是MethodHandle的设计是什么呢?为什么以及何时应该使用它?

But, what is MethodHandle about? Reflection API allows you to "look on" the object internals without any pre-assumption (like implemented the interface). You can inspect the object for some purpose. But what is MethodHandle is designed too? Why and when should I use it?

更新:我现在正在阅读 http://blog.headius.com/2008/09/first-taste-of-invokedynamic.html 文章。根据它,主要目标是简化在JVM上运行的脚本语言的生命,而不是Java语言本身。

UPDATE: I am reading now this http://blog.headius.com/2008/09/first-taste-of-invokedynamic.html article. According to it, the main goal is to simplify life for scripting languages that runs atop of JVM, and not for Java Language itself.

UPDATE-2:我读完了上面的链接,从那里引用了一些引用:

UPDATE-2: I finish to read the link above, some quotation from there:


JVM将成为构建动态的最佳VM语言,因为它已经是一个动态语言VM。而InvokeDynamic通过向一流的JVM公民推广动态语言,将证明它。

The JVM is going to be the best VM for building dynamic languages, because it already is a dynamic language VM. And InvokeDynamic, by promoting dynamic languages to first-class JVM citizens, will prove it.

使用反射来调用方法效果很好......除了一些问题。方法对象必须从特定类型中检索,并且不能以一般方式创建。< ...>

Using reflection to invoke methods works great...except for a few problems. Method objects must be retrieved from a specific type, and can't be created in a general way.<...>

...反映调用很多比直接调用慢。多年来,JVM已经非常擅长快速反映调用。现代JVM实际上在幕后生成了一堆代码,以避免处理大量旧的JVM。但简单的事实是,通过任意数量的层反射访问总是比直接调用慢,部分原因是完全通用的调用方法必须检查并重新检查接收器类型,参数类型,可见性和其他细节,但也因为参数必须都是对象(所以原语是对象框),并且必须作为数组提供,以涵盖所有可能的arities(所以参数得到数组框)。

...reflected invocation is a lot slower than direct invocation. Over the years, the JVM has gotten really good at making reflected invocation fast. Modern JVMs actually generate a bunch of code behind the scenes to avoid a much of the overhead old JVMs dealt with. But the simple truth is that reflected access through any number of layers will always be slower than a direct call, partially because the completely generified "invoke" method must check and re-check receiver type, argument types, visibility, and other details, but also because arguments must all be objects (so primitives get object-boxed) and must be provided as an array to cover all possible arities (so arguments get array-boxed).

对于执行一些反射调用的库,性能差异可能无关紧要,特别是如果这些调用主要是为了在内存中动态设置静态结构,可以进行正常调用。但是在动态语言中,每次调用必须使用这些机制,这是一个严重的性能损失。

The performance difference may not matter for a library doing a few reflected calls, especially if those calls are mostly to dynamically set up a static structure in memory against which it can make normal calls. But in a dynamic language, where every call must use these mechanisms, it's a severe performance hit.

http://blog.headius.com/2008/09/first-taste-of-invokedynamic。 html

因此,对于Java程序员来说,它基本上是无用的。我对吗?从这个角度来看,它只能被视为Core Reflection API的替代方式。

So, for Java programmer it is essentially useless. Am I right? From this point of view, It can be only considered as alternative way for Core Reflection API.

推荐答案

它是Java 8的前身为MethodHandles提供语言支持。你可以直接引用一个方法,而MethodHandles也会支持它。

It is precursor to Java 8 providing language support for MethodHandles. You will be able to refer to a method directly and MethodHandles will support that.

你可以用MethodHandles做的是咖喱方法,改变参数类型并改变它们的顺序。

What you can do with MethodHandles is curry methods, change the types of parameters and change their order.

方法句柄可以处理方法和字段。

Method Handles can handle both methods and fields.

MethodHandles做的另一个技巧是使用原始直接(而不是通过包装器)

Another trick which MethodHandles do is use primitive direct (rather than via wrappers)

MethodHandles可以比使用反射更快,因为JVM中有更多的直接支持,例如它们可以内联。它使用新的invokedynamic指令。

MethodHandles can be faster than using reflection as there is more direct support in the JVM e.g they can be inlined. It uses the new invokedynamic instruction.

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

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