简体   繁体   English

如何比较MethodHandle实例?

[英]How to compare MethodHandle instances?

How do I compare two MethodHandle instances? 如何比较两个MethodHandle实例? I am expecting the function to return true if the handles point to the same method. 我期望如果句柄指向相同的方法,该函数将返回true。 It doesn't look like the class overrides equals() or hashcode() . 看起来该类没有覆盖equals()hashcode() Is it safe to use the equality operator ( == )? 使用相等运算符( == )是否安全?

First of all, MethodHandle s may not only encapsulate a target method but also a behavior . 首先, MethodHandle不仅可以封装目标方法,还可以封装行为 Eg you may have two method handles pointing to the same method but one encapsulating a non-virtual ( super. … ) call and one representing an ordinary virtual invocation. 例如,您可能有两个指向同一个方法的方法句柄,但一个封装了一个非虚拟( super. … )调用,另一个代表了一个普通的虚拟调用。 These handles can't be equal. 这些句柄不能相等。

Besides that, there is no defined equality for MethodHandle s anyway. 除此之外, MethodHandle没有定义的相等性。 They are there to allow invocation but not introspection : 它们在那里允许调用但不能自省

JVMSpec §5.4.3.5. JVMSpec第5.4.3.5节。 Method Type and Method Handle Resolution : 方法类型和方法句柄解析

An implementation of the Java Virtual Machine is not required to intern method types or method handles. 实习方法类型或方法句柄不需要Java虚拟机的实现。 That is, two distinct symbolic references to method types or method handles which are structurally identical might not resolve to the same instance of java.lang.invoke.MethodType or java.lang.invoke.MethodHandle respectively. 也就是说,对结构类型相同的方法类型或方法句柄的两个不同的符号引用可能无法分别解析为java.lang.invoke.MethodTypejava.lang.invoke.MethodHandle的相同实例。


However, starting with Java 8 there is a feature that allows to introspect a direct MethodHandle , Lookup.revealDirect . 但是,从Java 8开始,有一个功能可以自检直接的 MethodHandleLookup.revealDirect For handles for which this operation succeeds, you can use the information in the returned MethodHandleInfo to find out whether two handles point to the same method. 对于成功执行此操作的句柄,可以使用返回的MethodHandleInfo的信息来查找两个句柄是否指向同一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM