【问题标题】:Convert MethodHandle to method reference (here Function)将 MethodHandle 转换为方法引用(此处为 Function)
【发布时间】:2014-12-11 07:40:49
【问题描述】:
MethodType methodType = MethodType.methodType(void.class, ByteBuffer.class);
MethodHandle handle = MethodHandles.publicLookup().findConstructor(type, methodType);

Function<ByteBuffer, Object> = handle; // ???

是否有可能获得最后的作业?倒置方式不行:Is it possible to convert method reference to MethodHandle?

这是另一个可复制粘贴的示例:

new Integer("123");

MethodType methodType = MethodType.methodType(void.class, String.class);
MethodHandle handle = MethodHandles.publicLookup().findConstructor(Integer.class, methodType);

Function<String, Integer> function1 = Integer::new;
Function<String, Integer> function2 = handle.toLambda(); // ???

【问题讨论】:

标签: java reflection lambda java-8 invokedynamic


【解决方案1】:

«This answer» 包含一个代码示例,展示了如何使用 Java 8 的 lambda 表达式和方法引用使用的相同功能将 MethodHandle 转换为函数式 interface 实现。

这一切都是关于使用方法句柄、所需接口和唯一abstract 方法的名称和所需签名来调用LambdaMetafactory.metafactory

method’s documentationit’s class documentation 都非常详细。

因此,根据您的要求,示例代码可能如下所示:

MethodType methodType = MethodType.methodType(Integer.class, String.class);
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle handle = lookup.findStatic(Integer.class, "valueOf", methodType);
Function<String,Integer> f=(Function<String,Integer>)
  LambdaMetafactory.metafactory(lookup, "apply",
    MethodType.methodType(Function.class), methodType.generic(),
    handle, methodType).getTarget().invokeExact();

System.out.println(f.apply("123"));

您必须关心这里的签名类型。第四个参数samMethodType指的是原始interface的函数签名的方法类型,所以对于原始类型Function我们必须实现Object apply(Object),而instantiatedMethodType描述的是方法Integer apply(String)。这就是为什么方法 .generic() 会在第四个参数的 methodType 上调用,这会将 (String)Integer 转换为 (Object)Object

这对于构造函数来说更加棘手,因为构造函数将使用 (String)void 类型查找,而 功能类型static 方法案例中的相同。所以对于 static 方法,方法的 MethodType 匹配 MethodType 而对于构造函数,我们必须使用不同的类型进行查找:

MethodType methodType = MethodType.methodType(Integer.class, String.class);
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle handle = lookup.findConstructor(
        Integer.class, MethodType.methodType(void.class, String.class));
Function<String,Integer> f=(Function<String,Integer>)
  LambdaMetafactory.metafactory(lookup, "apply",
    MethodType.methodType(Function.class), methodType.generic(),
    handle, methodType).getTarget().invokeExact();

但这只是为了完整性,对于 Integer 类型,您不应该调用构造函数,而最好使用 valueOf 方法。

【讨论】:

  • 再次感谢!将该解决方案包含在答案收集帖子中。
【解决方案2】:

我想你需要这样的东西:

Function<ByteBuffer,Object> fn = (Function<ByteBuffer,Object>)
    MethodHandleProxies.asInterfaceInstance(Function.class, handle);

通常的免责声明:甚至没有编译它。编译它。似乎工作。)

【讨论】:

  • 这是MethodHandleProxies,而不是MethodHandlesProxy 和Java 7 功能,但除此之外,它应该可以工作。
  • 试过了。为我工作(当在= 之前给出名称时)。
  • @Holger 尝试编译时发现错字。所有MethodHandle 的废话都是@Since Java SE 7。
  • 有效。将其添加到解决方案集合中的“function4”中。
  • @Tom Hawtin - tackline:不是“所有的MethodHandle 废话”。 LambdaMetafactory 产生更高效的 interface 实例并用于 lambda 表达式和方法引用自 Java 8 以来就存在。这同样适用于将直接句柄解码为 MethodHandleInfo 的功能。
【解决方案3】:

答案集合

没有句柄,只有 lambda:

Function<String, Integer> function1 = Integer::new;
System.out.println(function1.apply("1"));

简单(不通用,不精确)的解决方案:

MethodType methodType = MethodType.methodType(void.class, String.class);
MethodHandle handle = MethodHandles.publicLookup().findConstructor(Integer.class, methodType);
Function<String, Integer> function2 = (s) -> {
  try {
    return (Integer) handle.invoke(s);
  } catch (Throwable t) {
    throw new Error(t);
  }
};
System.out.println(function2.apply("2"));

使用来自 Holger 的 LambdaMetafactory

MethodType methodType = MethodType.methodType(Integer.class, String.class);
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle handle = lookup.findConstructor(Integer.class, MethodType.methodType(void.class, String.class));
Function<String,Integer> function3 = (Function<String,Integer>) LambdaMetafactory.metafactory(lookup, "apply", MethodType.methodType(Function.class), methodType.generic(), handle, methodType).getTarget().invokeExact();
System.out.println(function3.apply("3"));

使用来自 Tom Hawtin 的MethodHandleProxies

@SuppressWarnings("unchecked")
Function<String, Integer> function4 = (Function<String, Integer>) MethodHandleProxies.asInterfaceInstance(Function.class, handle);
System.out.println(function4.apply("4"));

【讨论】:

  • 看我的例子。不同之处在于对samMethodType 参数的方法类型的调用.generic()instantiatedMethodType 不同,因为Function&lt;String, Integer&gt; 是通用的,因此原始代码必须实现Object apply(Object),而实际代码将执行Integer apply(String)行动……
猜你喜欢
  • 1970-01-01
  • 2018-07-25
  • 2022-10-22
  • 2011-10-27
  • 2018-12-26
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多