当MethodHandle更快时,为什么要使用反射来访问类成员? [英] Why use reflection to access class members when MethodHandle is faster?

查看:269
本文介绍了当MethodHandle更快时,为什么要使用反射来访问类成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着Java 7的发布, MethodHandle ,它允许用户像使用其基础字节码一样调用方法.特别是 MethodHandles.Lookup 类提供了工厂方法来创建用于访问类成员的方法句柄:

With the release of Java 7 came the MethodHandle, which allows a user to invoke a method as if using its underlying bytecode. In particular, the MethodHandles.Lookup class provides factory methods to create method handles to access class members:

Lookup对象上的工厂方法与方法,构造函数和字段的所有主要用例相对应.由工厂方法创建的每个方法句柄在功能上等同于特定字节码行为.

The factory methods on a Lookup object correspond to all major use cases for methods, constructors, and fields. Each method handle created by a factory method is the functional equivalent of a particular bytecode behavior.

从功能上讲,这或多或少等同于使用反射来访问这些相同的类成员,但是 方法句柄比反射 .

Functionally, this is more or less equivalent to using reflection to access these same class members, yet method handles are faster than reflection.

因此,是否有任何理由仍然使用 Field#get(..) / Method.invoke(..) 还是采用更快的方法句柄有效地淘汰了这些方法?

So, is there any reason to still use reflection functionalities like Field#get(..)/Method.invoke(..) or are these methods effectively obsolete with the introduction of the faster method handles?

请注意,尽管方法句柄是在Java 7中引入的,但我的问题主要是关于Java 8的,在该句柄中,它们被优化以达到与直接字段/方法调用大致相等的性能,超过了反射的能力.

推荐答案

反射和方法句柄有不同的用途,并且存在于不同的抽象级别.您应该使用适合您要解决的问题的解决方案.

Reflection and method handles serve different purposes, and exist at different levels of abstraction. You should use the one that is right for the problem you are solving.

Reflection是一种通用的自省机制,它包括方法处理机制所缺少的许多功能,例如枚举类(Class.getMethods())的成员,检查成员的特性(例如其可访问性标志),检查成员等的通用签名.

Reflection is a general-purpose introspection mechanism, which includes many features that the method handle mechanism lacks, such as enumerating the members of a class (Class.getMethods()), inspecting the characteristics of a member such as its accessibility flags, inspecting generic signatures of members, etc.

此外,反射对象可以自由共享,而无需授予对共享对象的访问权限,因为访问检查是在每次调用时进行的.另一方面,共享方法处理授予共享者调用的能力.因此它们也具有不同的安全隐患.

Additionally, reflective objects can be freely shared without granting access to the sharee, because the access checks are made at each invocation. On the other hand, sharing method handles confers to the sharee the capability to invoke. So they also have different security implications.

方法句柄是用于查找,调整和调用方法的低级机制.尽管通过方法句柄进行调用的速度比通过反射进行调用的速度快(尽管到目前为止,直接字节码的调用通常仍比方法句柄调用的速度快),但方法句柄的使用也明显困难得多,因为它们不会自动执行Java用户期望的修改(例如将String参数转换为Object),从而导致链接错误.

Method handles are a low-level mechanism for finding, adapting, and invoking methods. While invocation through method handles is faster than through reflection (though to date, direct bytecode invocation is still generally faster than method handle invocation), method handles are also significantly harder to use, as they do not automatically perform the adaptations Java users would expect (such as converting a String argument to Object), resulting in linkage errors.

反射库针对主流Java用户;方法句柄层更多地面向编译器和语言运行时编写器.选择专为这项工作设计的工具.

The reflection library is aimed at mainstream Java users; the method handle layer is aimed more at compiler and language runtime writers. Pick the tool designed for the job.

这篇关于当MethodHandle更快时,为什么要使用反射来访问类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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