将MethodHandle转换为方法引用(此处为Function)

Sor*_*ras 12 java reflection lambda invokedynamic java-8

MethodType methodType = MethodType.methodType(void.class, ByteBuffer.class);
MethodHandle handle = MethodHandles.publicLookup().findConstructor(type, methodType);

Function<ByteBuffer, Object> = handle; // ???
Run Code Online (Sandbox Code Playgroud)

是否有可能让最后的作业有效?反转方式不起作用:是否可以将方法引用转换为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(); // ???
Run Code Online (Sandbox Code Playgroud)

Hol*_*ger 17

«这个答案»包含一个代码示例,演示如何使用相同的功能将Java 8的lambda表达式和方法引用转换MethodHandle为功能interface实现.

这是关于LambdaMetafactory.metafactory使用方法句柄,所需的接口以及唯一abstract方法和所需签名的名称进行调用的全部内容.

这两个方法的文档它的类文档都非常详细.

因此,对于您的请求,示例代码可能如下所示:

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"));
Run Code Online (Sandbox Code Playgroud)

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

这对于构造函数来说甚至更棘手,因为构造函数将使用(String)void类型查找,而函数类型static方法案例中的相同.因此,对于static方法,方法MethodTypeMethodType构造函数的while 匹配,我们必须使用不同的类型进行查找:

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();
Run Code Online (Sandbox Code Playgroud)

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


Tom*_*ine 7

我想你需要这样的东西:

Function<ByteBuffer,Object> fn = (Function<ByteBuffer,Object>)
    MethodHandleProxies.asInterfaceInstance(Function.class, handle);
Run Code Online (Sandbox Code Playgroud)

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

  • 它是`MethodHandleProxies`,而不是`MethodHandlesProxy`和Java 7功能,但除此之外,它应该可以工作. (2认同)