从lambda对象获取MethodHandle [英] Get MethodHandle from lambda object

查看:161
本文介绍了从lambda对象获取MethodHandle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 java.lang.invoke.LambdaMetafactory


评估lambda表达式的推荐机制是将lambda主体desugar为方法,调用invokedynamic调用站点,其静态参数列表描述功能接口和desugared实现方法的唯一方法,并返回实现目标类型的对象(lambda对象)。

The recommended mechanism for evaluating lambda expressions is to desugar the lambda body to a method, invoke an invokedynamic call site whose static argument list describes the sole method of the functional interface and the desugared implementation method, and returns an object (the lambda object) that implements the target type.

从检查开始,这至少是Oracle JDK的作用。

And from inspection this is at least what Oracle JDK does.

我的问题:给出一个lambda对象有没有办法找到实现方法的名称(或句柄)?或者,给定一个实现方法列表,有没有办法告诉哪一个对应于给定的lambda对象?

My question: given a lambda object is there a way to find the name (or a handle to) the implementation method? Alternately, given a list of implementation methods, is there a way to tell which one corresponds to a given lambda object?

推荐答案

你可以访问lambda类的ConstantPool(即使用 sun.misc.SharedSecrets.getJavaLangAccess()。getConstantPool(class)),然后搜索方法引用。您必须忽略对象构造函数以及autoboxing / unboxing方法引用,其余的方法引用是实现方法。

You could access the ConstantPool of the lambda class (i.e. by using sun.misc.SharedSecrets.getJavaLangAccess().getConstantPool(class)) and then search for the method reference. You have to ignore the object constructor as well as autoboxing/unboxing method references, the remaining method reference is the implementing method.

这篇关于从lambda对象获取MethodHandle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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