将MethodHandle转换为方法引用(此处为Function)
MethodType methodType = MethodType.methodType(void.class, ByteBuffer.class); MethodHandle handle = MethodHandles.publicLookup().findConstructor(type, methodType); Function = handle; // ???
是否有可能获得最后的作业? 反转方式不起作用: 是否可以将方法引用转换为MethodHandle?
这是另一个可复制的例子:
new Integer("123"); MethodType methodType = MethodType.methodType(void.class, String.class); MethodHandle handle = MethodHandles.publicLookup().findConstructor(Integer.class, methodType); Function function1 = Integer::new; Function function2 = handle.toLambda(); // ???
«这个答案»包含一个代码示例, MethodHandle
如何使用相同的function将MethodHandle
转换为functioninterface
实现,Java 8的lambda表达式和方法引用使用。
这是关于使用方法句柄,所需的接口以及唯一abstract
方法和所需签名的名称调用LambdaMetafactory.metafactory
。
这两个方法的文档和它的类文档都非常详细。
因此,对于您的请求,示例代码可能如下所示:
MethodType methodType = MethodType.methodType(Integer.class, String.class); MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle handle = lookup.findStatic(Integer.class, "valueOf", methodType); Function f=(Function) 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()
上为第四个参数调用方法.generic()
,它将(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 f=(Function) LambdaMetafactory.metafactory(lookup, "apply", MethodType.methodType(Function.class), methodType.generic(), handle, methodType).getTarget().invokeExact();
但这只是为了完整性,对于Integer
类型,您不应该调用构造函数,而是使用valueOf
方法,最好是。
我想你需要这样的东西:
Function fn = (Function) MethodHandleProxies.asInterfaceInstance(Function.class, handle);
( 通常的免责声明:甚至没有编译它。编译它。似乎工作。)
答案集
没有句柄,只有lambda:
Function 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 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 function3 = (Function) 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 function4 = (Function) MethodHandleProxies.asInterfaceInstance(Function.class, handle); System.out.println(function4.apply("4"));