在Java中,我能够将多个方法句柄及其每个参数组合在一起,如下所示:
foo( a, bar( 2, b ) )
..通过使用MethodHandles.collectArguments()。
我得到的方法句柄可以这样调用:
myHandle.invokeExact( 5, 6 ); // invokes foo(5, bar(2, 6))
但是现在,我想获得一个方法句柄,将其参数分派到调用树中,如下所示:
MethodHandle myHandle = ...; // foo( *x*, bar( 2, *x* ) )
myHandle.invokeExact( 3 ); // replaces x by 3 in both locations
// this call represents 'foo(3, bar(2, 3));'
我不能全神贯注于如何做到这一点。你能帮助我吗?
像往常一样,对于Java方法句柄,兴趣不大,所以我将为您提供答案:
将MethodHandles :: permuteArguments与MethodType :: dropPatameterTypes()调用结合使用。
就我而言,这仅仅是一个问题:
MethodHandle handle = [...]; // method handle representing f(x1, x2) = x1 + (x2 - 2)
MethodHandle h_permute = MethodHandles.permuteArguments(
handle,
handle.type().dropParameterTypes(1, 2), // new handle type with 1 less param
0,
0);
// h_permute now represents f(x) = x + (x - 2)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句